Java Swing: main class wait until JFrame is closed(Java Swing:主类等到JFrame关闭)
问题描述
我需要一个简单的 java 应用程序的帮助,它使用两个 jframe 来获取一些输入参数.这是我的代码草图:
I need some help with a simple java application which makes use of two jframe to get some input parameters. Here's a sketch of my code:
//second jframe, called when the button OK of the first frame is clicked
public class NewParamJFrame extends JFrame{
...
}
//first jframe
public class StartingJFrame extends JFrame{
private static NewParamJFrame newPFrame = null;
private JTextField gnFilePath;
private JButton btnOK;
public StartingJFrame(){
//..
initComponents();
}
private void initComponents(){
btnOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
EventQueue.invokeAndWait(new Runnable(){
public void run() {
try {
newPFrame = new NewParamJFrame();
newPFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
catch(InvocationTargetException e2) {}
catch(InterruptedException e1){}
dispose();
}
}
public String getText(){
return gnFilePath.getText();
}
}
public class Main {
private static StartingJFrame begin = null;
public static void main(String[] args) {
try{
EventQueue.invokeAndWait(new Runnable(){
public void run() {
try {
begin = new StartingJFrame();
begin.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
catch(InvocationTargetException e) {}
catch(InterruptedException e1){}
String s= begin.getText();
//...use s ...
}
}
对 getText() 的调用会导致 NullPointerException.我希望主课等到框架关闭,但我不知道该怎么做.我是第一次使用swing.
The call to getText() causes a NullPointerException. I want the main class to wait until the frames are closed but I don't know how to do. I'm using swing for the first time.
推荐答案
我希望主类等到框架关闭,但我不这样做知道该怎么做.我是第一次使用swing.
I want the main class to wait until the frames are closed but I don't know how to do. I'm using swing for the first time.
如果我正确理解您的问题,您需要 StartingJFrame 一直等到 NewParamJFrame 关闭,然后继续执行.如果是这种情况,那么它就不会发生,因为 JFrame 不支持模态.但是 JDialog 可以,所以你可以只有一个 JFrame 并在 JDialog 中进行参数请求,其父级是这个 JFrame代码>.
If I understand your problem correctly, you need StartingJFrame to stay waiting until NewParamJFrame is closed and then continue its execution. If this is the case then it won't happen because JFrame doesn't support modality. But JDialog does, so you can have just one JFrame and do the parameters request in a JDialog whose parent is this JFrame.
有关模态的更好解释,请阅读 如何在对话框中使用模态.
For a better explanation about modality, take a read to How to Use Modality in Dialogs.
还可以看看这个主题:The使用多个 JFrame,好/坏做法?
在任何情况下你都可能面临一个新问题:如果用户在没有输入任何参数的情况下关闭/取消对话框,JFrame 应该怎么做?这个 JFrame 怎么会知道在那个对话框中刚刚发生了什么?这个答案中描述了一种方法.您将看到该示例是关于登录对话框的,但问题与此类似:对话框如何向其父框架通知进程如何进行?
In any case you'll probably face a new problem: what should the JFrame do if the user closes/cancels the dialog withouth input any parameter? How could this JFrame know what just happened in that dialog? One approach is described in this answer. You'll see the example is about a login dialog but the problem is similar to this one: How could a dialog notify to its parent frame on how the process went?
这篇关于Java Swing:主类等到JFrame关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java Swing:主类等到JFrame关闭
基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01
