JFrame显示为空

时间:2014-08-20 13:16:51

标签: java swing jframe jpanel

在方法执行过程中,我只有一个简单的框架,只有一个面板有两个组件。

所以这是我的代码:

public void sarPPRuning() {
    JProgressBar progressBar = new JProgressBar();
    progressBar.setIndeterminate(true);

    sarppRun = new JFrame("Please wait");
    JPanel pan = new JPanel(new GridBagLayout());
    pan.add(new JLabel("SARPP is running, please wait"),
            new GridBagConstraints(0, 0, 1, 1, 1, 1,
                    GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),
                    0, 0));
    pan.add(progressBar, new GridBagConstraints(0, 1, 1, 1, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets(3, 3, 3, 3), 0, 0));
    sarppRun.getContentPane().add(pan, BorderLayout.CENTER);
    sarppRun.setSize(400, 100);
    sarppRun.setVisible(true);
    sarppRun.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

此方法在此处调用:

_vet.sarPPRuning();
execSARPP()

其中_vet是扩展ViewGuiDialog

的类的实例

我的JFrame出现,但在execSARPP功能完成之前它是空的!!!

1 个答案:

答案 0 :(得分:1)

当我接受你的代码并自行运行时,

public static void main(String[] args) {
    JProgressBar progressBar = new JProgressBar();
    progressBar.setIndeterminate(true);

    JFrame sarppRun = new JFrame("Please wait");
    JPanel pan = new JPanel(new GridBagLayout());
    pan.add(new JLabel("SARPP is running, please wait"),
            new GridBagConstraints(0, 0, 1, 1, 1, 1,
                    GridBagConstraints.CENTER,
                    GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),
                    0, 0));
    pan.add(progressBar, new GridBagConstraints(0, 1, 1, 1, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets(3, 3, 3, 3), 0, 0));
    sarppRun.getContentPane().add(pan, BorderLayout.CENTER);
    sarppRun.setSize(400, 100);
    sarppRun.setVisible(true);
    sarppRun.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

它按预期工作,我得到了带有移动进度条的JFrame, SARPP JFrame