【问题标题】:Java Swing : creating frame in ActionPerformed() of different classJava Swing:在不同类的 ActionPerformed() 中创建框架
【发布时间】:2016-05-17 21:10:57
【问题描述】:

我有一个包含按钮的第一帧。按下按钮时,我调用不同类的 actionPerformed() 方法。

JButton compress = new JButton("Submit");
compress.addActionListener(new Action1(inp,out,frame1)); // inp,out are textboxes and frame1 is 1st frame containing textboxes and JButton

在 Action1 类的 actionPerformed() 中。我在那里创建了另一个框架,如下所示

static class Action1 implements ActionListener {        

        JTextField input_path,out_path;
        JFrame prev;

        public Action1(JTextField inp,JTextField out,JFrame jf)
        {
            input_path = inp;
            out_path = out;
            prev = jf;
        }

        public void actionPerformed (ActionEvent e) {           
            prev.dispose();
            try{
                drawFrame();
                // launch the compression job
                launchJob(input_path.getText(),out_path.getText());
            }
            catch(IOException io){
                io.printStackTrace();
            }

        }
        public void drawFrame()
        {
            JFrame frame2 = new JFrame("New Frame");
            JPanel panel = new JPanel();

            frame2.setSize(400,300);
            frame2.setLocation(500, 300);
            JLabel label = new JLabel(" in Progress...");

            panel.add(label);
            frame2.add(panel);
            frame2.setVisible(true);
        }
    }

但是在 actionPerformed() 中,frame2 的内容在方法 launchJob() 执行后变得可见。我想在我的函数 launchJob() 开始执行之前显示(使可见)frame2 的内容。您能否建议我哪里出错或其他替代方法。谢谢你。

【问题讨论】:

标签: java swing user-interface awt java-7


【解决方案1】:

我刚刚验证了您的框架被设置为可见您的方法 launchJob() 被调用。

考虑添加一些打印语句进行调试,例如在drawFrame()方法的最后,添加如下代码:

frame2.setVisible(true);
System.out.println("frame visible");

在启动作业方法开始时做同样的事情:

System.out.println("doing launch job");

你会看到输出是:

frame visible
doing launch job

这将验证 JFrame 在您的启动作业方法之前实际上是可见的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
相关资源
最近更新 更多