Java Swing:主类,直到关闭JFrame [英] Java Swing: main class wait until JFrame is closed

查看:115
本文介绍了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.我希望主要的班级等到关闭框架,但我不知道该怎么做.我是第一次使用秋千.

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.

推荐答案

我希望主类等待直到框架关闭,但我不希望 知道该怎么做.我是第一次使用秋千.

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并在其父级为JFrameJDialog中执行参数请求.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆