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; } }