简体   繁体   English

框架显示不正确

[英]Frame doesn't show correctly

I made a program that opens a database and makes an update, how this update is very long, I've tried to make a new frame where the user could see the state of the update. 我制作了一个程序,该程序可以打开数据库并进行更新,该更新的时间非常长,我试图制作一个新的框架,使用户可以看到更新的状态。

The problem is: When I launch the new Thread the structure of the window is created but doesn't show anything else. 问题是:当我启动新线程时,将创建窗口的结构,但不显示其他任何内容。 When the update has finished, then, the window finished to load completely all the content. 更新完成后,该窗口将完全加载所有内容。

What am I doing wrong ? 我究竟做错了什么 ?

public class finestra extends Thread{
    @Override
    public void run(){
        label1.setText(getMissatge1());
        label1.setHorizontalAlignment(JLabel.CENTER);
        label2.setText(getMissatge2());
        label2.setHorizontalAlignment(JLabel.CENTER);
        label3.setHorizontalAlignment(JLabel.RIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new GridLayout(0,1));
        frame.setSize(300, 100);
        frame.add(label1);
        frame.add(label2);
        frame.add(label3);
        //frame.pack();
        frame.setVisible(true);
        int temps = 0;
        boolean ok = false;
        while(ok == false){
            temps++;
            try{
                Thread.sleep(1000);
                label1.setText(getMissatge1());
                label2.setText(getMissatge2());
                label3.setText("Working " + String.valueOf(temps));
            }catch (Exception a){

            }
        }
    }
}

And this is the way that i launch the thread: 这就是我启动线程的方式:

finestra Finestra = new finestra(); finestra Finestra =新的finestra(); Finestra.start(); Finestra.start();

Thanks a lot! 非常感谢!

You're attempting to make gui updates outside of the event dispatch thread. 您正在尝试在事件分发线程之外进行gui更新。 This is not recommended and will cause the behaviour you're seeing... 不建议这样做,这会导致您看到的行为...

see How to update java GUI from Thread? 请参阅如何从线程更新Java GUI? for techniques about how to ensure the updates hapeen in the event dispatch thread. 有关如何确保在事件分发线程中进行更新的技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM