如果对话框是模式对话框,则代码将被阻止,直到对话框关闭:
dialog.setVisible(true);
// blocked here until the dialog is closed. The dialog stores the input in a
// field when OK is clicked in the dialog
if (dialog.getTextInputtedByTheUser() != null) {
...
Run Code Online (Sandbox Code Playgroud)
如果对话框不是模态的,那么您需要在验证发生时使其调用回调方法。这就是 MyFrame 将包含的内容
private void showDialog(
MyDialog dialog = new MyDialog(this);
dialog.setVisible(true);
}
public void userHasInputSomeText(String text) {
// do whatever you want with the text
System.out.println("User has entered this text in the dialog: " + text);
}
Run Code Online (Sandbox Code Playgroud)
在我的对话框中:
private MyFrame frame;
public MyDialog(MyFrame frame) {
super(frame);
this.frame = frame;
}
...
private void okButtonClicked() {
String text = textField.getText();
frame.userHasInputSomeText(text);
}
Run Code Online (Sandbox Code Playgroud)