我正在嘗試使用JDialog
作爲String
的輸入。但是我得到的文本來自我點擊按鈕之前。使用JDialog作爲輸入
這是我的對話框:
public class AddMemberDialog extends JDialog {
private JTextField name;
public AddMemberDialog() {
super(new JFrame("Add Member"), "Add Member");
this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
this.setMinimumSize(new Dimension(500, 500));
this.name = new JTextField();
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
close();
}
});
this.setLayout(new GridLayout(2, 1, 5, 5));
this.add(name);
this.add(add);
this.pack();
}
private void close(){ this.dispose(); }
public String getName(){ return this.name.getText(); }
}
,這裏是我使用去String
什麼:
AddMemberDialog input = new AddMemberDialog();
input.setLocationRelativeTo(this);
input.setVisible(true);
String txt = input.getName();
您需要爲您的按鈕製作一個ActionListener,然後僅在按下該按鈕後才執行代碼。我有一個類似的問題與我的代碼[這裏](http://stackoverflow.com/questions/26198989/how-to-wait-for-user-input-when-using-jtextfields) – DreadHeadedDeveloper 2014-10-27 18:16:39
或者說,做另一個按鈕處理,因爲我看到這個按鈕是關閉 – DreadHeadedDeveloper 2014-10-27 18:17:11
在你的構造函數,添加「setModal(true);」 – ControlAltDel 2014-10-27 18:18:03