【问题标题】:JFrame not coming on top of other window [duplicate]JFrame没有出现在其他窗口之上[重复]
【发布时间】:2012-12-31 12:00:49
【问题描述】:

可能重复:
JDialog Stops execution of parent JFrame

我必须使用 JFrame,它没有装饰,并且只有一个进度条的图像......但是当我想向服务器发送请求时,我希望它显示进度条并且它没有显示它,而是我的窗口已加载..我尝试过 jDialog 但它正在停止执行我的代码..

这是我的代码

private void LoginBtnActionPerformed(java.awt.event.ActionEvent evt) {
    ProgressBar pb = new ProgressBar();
    pb.setVisible(true);
    List<BasicNameValuePair> postPairs = new ArrayList<BasicNameValuePair>();
    AsyncService asyncService = new AsyncService();

    postPairs.add(new BasicNameValuePair("PATH","authenticateUser.idoc"));
        postPairs.add(new BasicNameValuePair("user_email",email));
        postPairs.add(new BasicNameValuePair("user_password",password));


        JSONArray jArray = asyncService.sendRequest(postPairs);
        pb.setVisible(true);
}

我的进度条框架中除了一个 gif 动画图像和一个标签之外什么都没有... 编辑 1 */

  class WorkerProgressBar extends SwingWorker<Integer, Integer>
  {
    private JFrame pb = new JFrame();
    JDialog dialog = new JDialog();
     JPanel panel = new JPanel();
     JLabel label = new JLabel();
     public WorkerProgressBar(){
             panel = new JPanel();
                panel.setBackground(new Color(114,114,114));
                JLabel imageLabel = new JLabel(new ImageIcon(getClass().getResource("/icons/progress_bar.gif")));
                imageLabel.setBorder(BorderFactory.createLineBorder(Color.black));
                panel.add(imageLabel);
                dialog.getContentPane().add(panel,BorderLayout.CENTER);

                dialog.pack();
                dialog.setLocationRelativeTo(null);
     }
     protected Integer doInBackground() throws Exception
 {
        dialog.setVisible(true);
        return 0;
     }
     protected void done()
{
    try
    {
        dialog.setVisible(false);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}   

【问题讨论】:

    标签: java multithreading swing


    【解决方案1】:

    如果窗口要位于另一个 Swing 顶级窗口之上,您应该使用 JDialog,但必须确保在将模态对话框设置为可见之前调用所有要运行的代码。但关键是您应该在后台线程(例如 SwingWorker 提供的线程)上运行长时间运行的任务。这个网站上有很多个这样的例子,因为你的问题在这里已经被问过很多次了。

    【讨论】:

    • 你的意思是像this one? ;)
    • @Andrew:或者this onethis one
    • 我在 Swing Worker 中完成了它,但它向我显示白框..如果我不发送异步请求而不是向我显示正确的进度图像..?
    • @HaseebWali: "I have done it in swing worker but..." -- 那么你做错了,但在你向我们展示代码之前,我们不知道你做错了什么。
    • @Haseeb:您需要阅读SwingWorker tutorial 并查看我们上面链接到的示例,因为您通过从 doInBackground 方法调用 Swing 而不是调用,完全向后使用它通过此方法执行的长期任务。
    猜你喜欢
    • 2010-12-27
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 2010-12-24
    • 2021-07-15
    相关资源
    最近更新 更多