用模态的方式打开自定义JDialog,并获取返回值

本文介绍如何在JAVA中通过模态对话框(JDialog)打开自定义的Fff类实例,并详细讲解如何从该对话框获取返回值,以实现JFrame(jf)与Fff之间的数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JFrame的变量名为jf

JDialog的类名为Fff

btnLoadImage.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Fff myff = new Fff();
				//myff.setModal(false);
				myff.setModalityType(ModalityType.APPLICATION_MODAL);
				myff.setVisible(true);
				
				//如果是点击确定,则文本框中输入的内容显示出来
				String strText;
				int iReturn;
				
				iReturn = myff.f_GetCoseType();
				if (iReturn == 0) {
					lbImage.setText("选择了取消");
				}
				else
				{
					strText = myff.f_GetText();
					lbImage.setText(strText);
				}
				
			}				
		});



Fff的类


package bb;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/** 
* @author 作者 E-mail: 
* @version 创建时间:Sep 18, 2017 2:48:29 PM 
* 类说明 
*/

public class Fff extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textOut;
	private int iCloseType; //点击确定,该值为1,点击取消,该值为0
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Fff dialog = new Fff();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Fff() {
		iCloseType = 0 ;	//默认是点击取消按钮
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			panel.setLayout(new BorderLayout(0, 0));
			{
				textOut = new JTextField();
				panel.add(textOut, BorderLayout.NORTH);
				textOut.setColumns(10);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						//检查用户是否输入了内容
						if(f_chkInput()) {
							iCloseType = 1;	//点击了确定性按钮
							dispose();
						}
					}
				});
				
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						iCloseType = 0;	//点击了取消按钮
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	//检查用户是否输入了文本
	public boolean f_chkInput() {
		 String strText;
		 boolean bRet;
		strText = textOut.getText();
		if ( strText.length()==0 || strText==null) {
			JOptionPane jMsg = new JOptionPane();
			jMsg.showMessageDialog(null, "请输入内容");
			bRet = false;
		}
		else
		{
			bRet = true;
		}
		
		return bRet;
	}
	
	public int f_GetCoseType() {
		return iCloseType;	
	}
	public String f_GetText() {
		String strOut;
		strOut = textOut.getText();
		return strOut;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值