栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

如果在用户按下后对话框消失,请确认:

  • 你希望有对话的行为如同一个 模态 的JDialog,那么它很容易,因为你知道在代码中你的程序将尽快用户完成是处理对话框-这将是你打电话后立即
    setVisible(true)
    对对话。因此,您只需在调用
    setVisible(true)
    对话框后立即在代码行中查询对话框对象的状态即可。
  • 如果您需要处理非模式对话框,则需要在该对话框的窗口不可见时向该对话框添加一个WindowListener,以便得到通知。

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

  • 然后,您可能应该使用上面建议的PropertyChangeListener。要么为此对话框对象提供一个公共方法,该方法允许外部类将ActionListener添加到确认按钮。

有关更多详细信息,请向我们显示代码的相关部分,或者更好的是sscce。

例如,要允许JDialog类接受外部侦听器,可以给它一个JTextField和一个JButton:

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

以及允许外部类向按钮添加ActionListener的方法:

public void addConfirmListener(ActionListener listener) {  /confirm/iBtn.addActionListener(listener);}

然后,外部类可以简单地调用`add/confirm/iListener(…)方法以将其ActionListener添加到/confirm/iBtn中。

例如:

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/i");   public MyDialog(Jframe frame, String title) {      super(frame, title, false);      JPanel panel = new JPanel();      panel.add(textfield);      panel.add(/confirm/iBtn);      add(panel);      pack();      setLocationRelativeTo(frame);   }   public String getTextFieldText() {      return textfield.getText();   }   public void addConfirmListener(ActionListener listener) {      /confirm/iBtn.addActionListener(listener);   }}

注意事项:除非绝对必要,否则我建议不要继承Jframe或JDialog。为了简洁起见,在这里进行此操作。我本人也更喜欢使用模式对话框来解决此问题,并且仅在需要时重新打开对话框。

编辑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/i");   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(/confirm/iBtn);      add(panel);      pack();      setLocationRelativeTo(frame);      ActionListener confirmListener = new ConfirmListener();      /confirm/iBtn.addActionListener(/confirm/iListener); // 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(/confirm/iBtn,       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;   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/453362.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号