【问题标题】:Java Gui return itemJava Gui 返回项
【发布时间】:2012-11-07 20:59:47
【问题描述】:

我想从 gui 实例中获得回报 我运行创建 GUI 的代码:

JFrame frame = new JFrame();
frame.getContentPane().add(new ChatPopup());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);

我的GUI(ChatPopUp代码如下:

public class ChatPopup extends javax.swing.JPanel {
private JButton cancelButton;
private JTextField textFieldchatRoomName;
private JLabel jLabel1;
private JButton okButton;

public ChatPopup() {
    super();

    initGUI();
}

private void initGUI() {
    try {
        this.setPreferredSize(new java.awt.Dimension(294, 85));
        {
            jLabel1 = new JLabel();
            this.add(jLabel1);
            jLabel1.setText("Please enter the new chat room name:");
        }
        {
            textFieldchatRoomName = new JTextField();
            this.add(textFieldchatRoomName);
            textFieldchatRoomName.setPreferredSize(new java.awt.Dimension(263, 22));
        }
        {
            cancelButton = new JButton();
            this.add(cancelButton);
            cancelButton.setText("Cancel");
            cancelButton.setPreferredSize(new java.awt.Dimension(84, 22));
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("Cancel PRESSED");
                }
            });
        }
        {
            okButton = new JButton();
            this.add(okButton);
            okButton.setText("Ok");
            okButton.setPreferredSize(new java.awt.Dimension(60, 22));
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("OK PRESSED");
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

这是一个非常简单的 GUI,它有一个文本字段和 2 个按钮,一个是“Ok”,一个是“Chancel”。 当我单击“确定”时,我希望将 textField 值发送到最初运行 GUI 实例的类。

任何想法如何做到这一点??

【问题讨论】:

    标签: java swing user-interface textfield


    【解决方案1】:

    您发布的JPanel 应添加到模态JDialog 内容窗格中。在同一个类中,您可以提供一些方法来返回用户在文本字段中输入的值。

    在原始窗口中,打开对话框。

    SomeDialog dialog = new SomeDialog(parent);
    dialog.setVisible(true);
    

    setVisible() 之后的代码只会在模态对话框关闭后执行。此时你可以调用我上面提到的方法来获取文本字段的值。

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2021-04-26
      • 2020-12-03
      相关资源
      最近更新 更多