JDialog的动作监听器,用于单击按钮

我有主要的应用程序在哪里是值与表。 然后,我点击“添加”按钮,新的CUSTOM(我自己制作)JDialog类型弹出窗口出现。 在那里我可以输入值,做一些滴答并点击“确认”。 所以我需要从对话框中读取该输入,因此我可以将此值添加到主应用程序中的表中。 当按下“确认”按钮时我该怎么听,所以我可以在那之后读取该值?

addISDialog = new AddISDialog(); addISDialog.setVisible(true); addISDialog.setLocationRelativeTo(null); //somekind of listener... //after "Confirm" button in dialog was pressed, get value value = addISDialog.ISName; 

如果用户按下确认后对话框将消失:

  • 并且你希望对话框的行为类似于模态 JDialog,那么它很简单,因为一旦用户完成对话,你就会知道你的程序在代码中的位置 – 它会在你调用setVisible(true)之后立即知道setVisible(true)在对话框上。 因此,只需在对话框中调用setVisible(true)后立即在对话框中查询对话框对象的状态。
  • 如果需要处理非modal dialog,则需要在对话框窗口变为不可见时向对话框添加WindowListener以进行通知。

如果在用户按下确认后对话框保持打开状态:

  • 然后你应该使用上面建议的PropertyChangeListener。 或者给对话框对象一个公共方法,允许外部类能够将ActionListener添加到确认按钮。

有关更多详细信息,请向我们显示您的代码的相关位,甚至更好的sscce 。

例如,要允许JDialog类接受外部侦听器,您可以为其提供JTextField和JButton:

 class MyDialog extends JDialog { private JTextField textfield = new JTextField(10); private JButton confirmBtn = new JButton("Confirm"); 

和一个允许外部类将ActionListener添加到按钮的方法:

 public void addConfirmListener(ActionListener listener) { confirmBtn.addActionListener(listener); } 

然后外部类可以简单地调用`addConfirmListener(…)方法将其ActionListener添加到confirmBtn。

例如:

 import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class OutsideListener extends JFrame { private JTextField textField = new JTextField(10); private JButton showDialogBtn = new JButton("Show Dialog"); private MyDialog myDialog = new MyDialog(this, "My Dialog"); public OutsideListener(String title) { super(title); textField.setEditable(false); showDialogBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!myDialog.isVisible()) { myDialog.setVisible(true); } } }); // !! add a listener to the dialog's button myDialog.addConfirmListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = myDialog.getTextFieldText(); textField.setText(text); } }); JPanel panel = new JPanel(); panel.add(textField); panel.add(showDialogBtn); add(panel); } @Override public Dimension getPreferredSize() { return new Dimension(400, 300); } private static void createAndShowGui() { JFrame frame = new OutsideListener("OutsideListener"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class MyDialog extends JDialog { private JTextField textfield = new JTextField(10); private JButton confirmBtn = new JButton("Confirm"); public MyDialog(JFrame frame, String title) { super(frame, title, false); JPanel panel = new JPanel(); panel.add(textfield); panel.add(confirmBtn); add(panel); pack(); setLocationRelativeTo(frame); } public String getTextFieldText() { return textfield.getText(); } public void addConfirmListener(ActionListener listener) { confirmBtn.addActionListener(listener); } } 

注意事项:除非绝对必要,否则我不建议inheritanceJFrame或JDialog。 这只是为了简洁起见。 我自己也更喜欢使用modal dialog来解决这个问题,并在需要时重新打开对话框。

编辑2
使用模态对话框的示例:

 import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class OutsideListener2 extends JFrame { private JTextField textField = new JTextField(10); private JButton showDialogBtn = new JButton("Show Dialog"); private MyDialog2 myDialog = new MyDialog2(this, "My Dialog"); public OutsideListener2(String title) { super(title); textField.setEditable(false); showDialogBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!myDialog.isVisible()) { myDialog.setVisible(true); textField.setText(myDialog.getTextFieldText()); } } }); JPanel panel = new JPanel(); panel.add(textField); panel.add(showDialogBtn); add(panel); } @Override public Dimension getPreferredSize() { return new Dimension(400, 300); } private static void createAndShowGui() { JFrame frame = new OutsideListener2("OutsideListener"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class MyDialog2 extends JDialog { private JTextField textfield = new JTextField(10); private JButton confirmBtn = new JButton("Confirm"); public MyDialog2(JFrame frame, String title) { super(frame, title, true); // !!!!! made into a modal dialog JPanel panel = new JPanel(); panel.add(new JLabel("Please enter a number between 1 and 100:")); panel.add(textfield); panel.add(confirmBtn); add(panel); pack(); setLocationRelativeTo(frame); ActionListener confirmListener = new ConfirmListener(); confirmBtn.addActionListener(confirmListener); // add listener textfield.addActionListener(confirmListener ); } public String getTextFieldText() { return textfield.getText(); } private class ConfirmListener implements ActionListener { public void actionPerformed(ActionEvent e) { String text = textfield.getText(); if (isTextValid(text)) { MyDialog2.this.setVisible(false); } else { // show warning String warning = "Data entered, \"" + text + "\", is invalid. Please enter a number between 1 and 100"; JOptionPane.showMessageDialog(confirmBtn, warning, "Invalid Input", JOptionPane.ERROR_MESSAGE); textfield.setText(""); textfield.requestFocusInWindow(); } } } // true if data is a number between 1 and 100 public boolean isTextValid(String text) { try { int number = Integer.parseInt(text); if (number > 0 && number <= 100) { return true; } } catch (NumberFormatException e) { // one of the few times it's OK to ignore an exception } return false; } } 
 //Why is this working so well even without the ActionListener interface, and all I'm //getting is minor format errors at the end brackets? Know how to fix it? final JButton newButton = new JButton("Send"); newButton.setActionCommand("Send"); textPane.add(newButton); newButton.setEnabled(true); newButton.addActionListener(new ActionListener(); public void actionPerformed(ActionEvent e){ // display/center the jdialog when the button is pressed JDialog digFree = new JDialog(digFree, "Hello", true); digFree.setLocationRelativeTo(newButton); digFree.setFocusable(true); digFree.setVisible(true); digFree.setBounds(20, 20, 100, 120); } 

你为什么不检查你的jDialog是否可见?

 yourJD.setVisible(true); while(yourJD.isVisible())try{Thread.sleep(50);}catch(InterruptedException e){} 

这也有效。

 import javax.swing.JOptionPane; 

或者如果你已经摆动了

 import javax.swing.*; 

会帮你的。

在条件触发JOptionPane后发送您的警告或任何模态消息:

  JOptionPane.showMessageDialog( null, "Your warning String: I can't do that John", "Window Title", JOptionPane.ERROR_MESSAGE); 

检查您的JOptionPane。*选项以确定消息类型。