摆动窗口被冻结,不显示内容 [英] swing window getting frozen ,not displaying content

查看:108
本文介绍了摆动窗口被冻结,不显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Main extends JFrame{

JLabel lb1,lb2,lb3,lb4;
Button b,b1;
public Main()
{
    setLayout(null);
    Container c=getContentPane();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.red);

    setVisible(true);





    b1=new Button("CONTINUE");
    b1.setFont(new Font("Algerian", 1, 20));
    b1.setForeground(Color.black);  
    b1.setBounds(550, 480, 200,40);
    b1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent ae)
      {  setVisible(false);       
           try {
            new Frame();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


      }
    });




    c.add(b1);

    setSize(1290,900);

}

public static void main(String...s) throws InterruptedException
{
    new Main();


}
}

这是main class..and它调用的框架类..

this is the main class..and this the frame class that it calls..

public class Frame extends JFrame{


Frame() throws InterruptedException
{ 
JFrame f=new JFrame();
f.setVisible(true);
f.setLayout(null);
f.setBounds(0,0,200,200);

Container p=f.getContentPane();

p.setBackground(Color.GREEN);
f.setResizable(false);

Thread.sleep(5000);

    setVisible(false);
    new Frame1();

}

}

主要班级调用帧类...需要保持一段时间然后移动到另一帧..
但是主要类调用它会发生框架,但框架中没有内容。没有显示任何内容。然后它移动到frame1()//

the main class calls the frame class..that needs to hold for a certain time and then move to another frame.. but what happens is that the main class calls it, the frame appears , but there is no content in it..nothing is displayed.then it moves to frame1()//

但如果我像


new Frame();

new Frame();

然后它保持,显示内容,然后移动..

then it holds ,shows content , and then moves..

那么为什么当Main()调用Frame()时它不起作用?

so why doesnt it work when Frame() is called by Main() ?

即使这段代码也不起作用。 of Thread.sleep()...这样我就不会阻止任何线程......我是吗?

even this code doesnt' work ..instead of Thread.sleep()...this way i am not blocking any even thread..am i ?

for(double i=0;i<30;i++)
{


    for(double j=0;j<10000;j++)
    {


    }
}

new Frame1();

new Frame1();

推荐答案


  1. Swing做它的GUI渲染单个线程内的任务称为事件调度线程(EDT)。你做的任何可能需要一段时间的事情都会阻止EDT,你的挥杆应用程序将被冻结。你正在通过以下陈述做这件事:

  1. Swing does it's GUI rendering task inside a single thread known as Event Dispatch Thread(EDT). Any thing you do which might take a while will block the EDT and your swing application will be frozen. You are doing such thing by one of your statement:

Thread.sleep(5000);


  • 始终建议不要使用 NullLayout 。了解Swing开发人员为我们创建的正确布局管理员他们的辛勤工作。让我们为他们的努力赋予一些价值。

  • This is always suggested not to work with NullLayout. Learn proper layout mangers that Swing developer has created for us with their hard work. Let us give some value to their effort.

    放置你的GUI渲染任务,包括 frame 's setVisible(true)在EDT中使用 SwingUtilities.invokeLater 函数。例如:

    Put your GUI rendering task including frame's setVisible(true) inside EDT by using SwingUtilities.invokeLater function. For example:

    SwingUtilities.invokeLater(new Runnable() {
    
            @Override
            public void run() {
               new MyWindow().setVisible(true);
            }
        });
    


  • 您正在使用多帧。 也禁止 由Swing系列Stack Overflow提供。通过家庭使用卡布局显示了许多工作,您可以轻松采用这些工作来避免不必要地使用多个帧。在给定链接的 Andrew Thompson 的答案中给出了一些示例。

  • You are working with Multiple Frame. It is also prohibited by the Swing family of Stack Overflow. There are lots of work around shown using Card Layout by the family which you can easily adopt for avoiding use of multiple frame unnecessarily. Some example are given in the answer of Andrew Thompson with given link.

    这篇关于摆动窗口被冻结,不显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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