【问题标题】:Return value from custom JOptionPane, Java从自定义 JOptionPane,Java 返回值
【发布时间】:2013-10-09 22:03:49
【问题描述】:

我正在创建一个返回 JFrame 的自定义类,然后我将其传递给 JOptionPane,因为我需要 JOptionPane 中的两个 TextField 而不是一个。有什么方法可以在按下 OK 时获得返回值?

 public static JFrame TwoFieldPane(){

 JPanel p = new JPanel(new GridBagLayout());
    p.setBackground(background);
    p.setBorder(new EmptyBorder(10, 10, 10, 10) );
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    p.add(new JLabel(field1), c);
    c.gridx = 0;
    c.gridy = 1;
    p.add(new JLabel(field2), c);
    //p.add(labels, BorderLayout.WEST);
    c.gridx = 1;
    c.gridy = 0;
    c.ipadx = 100;
    final JTextField username = new JTextField(pretext1);
    username.setBackground(foreground);
    username.setForeground(textcolor);
    p.add(username, c);
    c.gridx = 1;
    c.gridy = 1;
    JTextField password = new JTextField(pretext2);
    password.setBackground(foreground);
    password.setForeground(textcolor);
    p.add(password, c);
    c.gridx = 1;
    c.gridy = 2;
    c.ipadx = 0;
    JButton okay = new JButton("OK");
    okay.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            f.setVisible(false);
            //RETURN VALUE HERE
        }
    });
    p.add(okay, c);

    f.add(p);
    f.pack();
    f.setLocationRelativeTo(null);

    f.setVisible(true);
    return f;
}

这就是我创建它的地方:

try{
    JOptionPane.showInputDialog(Misc.TwoFieldPane("Server ip: ", "" , "Port: ", ""));
    }catch(IllegalArgumentException e){e.printStackTrace(); }

【问题讨论】:

    标签: java swing return joptionpane


    【解决方案1】:

    您的代码有点不寻常。让我提出建议:

    • 不要将 JFrame 用于您的 JOptionPane,这有点古怪。
    • 避免过度使用静态方法。 OOP 是必经之路。
    • 创建一个为您创建 JOptionPane 的 JPanel 并具有实际实例字段的类。
    • 提供类 getter 方法,允许您在 JOptionPane 返回后查询其状态。
    • 创建您的 JOptionPane 并为其提供一个从您上面的类创建的 JPanel。
    • 在 JOptionPane 返回后,查询您放置在其中的对象的字段状态。

    即,一个过于简单的例子......

    public class MyPanel extends JPanel {
      private JTextField field1 = new JTextField(10);
      // .... other fields ? ...
    
      public MyPanel() {
         add(new JLabel("Field 1:");
         add(field1);
      }
    
      public String getField1Text() {
        return field1.getText();
      }
    
      // .... other getters for other fields
    }
    

    ...在另一个班级的其他地方...

    MyPanel myPanel = new MyPanel();
    int result = JOptionPane.showConfirmDialog(someComponent, myPanel);
    if (result == JOptionPane.OK_OPTION) {
      String text1 = myPanel.getField1Text(); 
      // ..... String text2 = ...... etc .....
      // .... .use the results here
    }
    

    顺便说一句,不要使用 JTextField 或字符串作为密码,除非您的应用程序不关心安全性。请改用 JPasswordField 和 char 数组。

    【讨论】:

    • +1 不错的新建议..但我不确定我是否会使用一个 joption 窗格,也许只是一个 jdialog
    • @nachokk:哦,我一直以这种方式使用 JOptionPanes。它们可以容纳非常复杂的 GUI,并以一种很好的紧凑模式方式显示对话框。
    • @nachokk A JOptionPane 提供了许多您必须自己编写代码的功能,例如按钮,并提供了非常灵活的 API 来提供非常可定制的解决方案。有时您可能别无选择,只能使用 JDialog,但还有更多 JOptionPane 更有用的地方 - 恕我直言
    • 谢谢,很有用。我在解决方案中添加的另一件事是,我没有向面板添加 getter 和 setter,而是添加了 getResult() 方法,该方法返回了一个自定义类,其中包含面板中所有必需的数据,因此更容易传递这些数据到其他方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多