swing window getting frozen ,not displaying content

前端 未结 1 1973
野的像风
野的像风 2020-12-22 05:02
public class Main extends JFrame{

JLabel lb1,lb2,lb3,lb4;
Button b,b1;
public Main()
{
    setLayout(null);
    Container c=getContentPane();
    setDefaultCloseOpe         


        
相关标签:
1条回答
  • 2020-12-22 05:42
    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);
      
    2. 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.

    3. 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);
              }
          });
      
    4. 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.

    0 讨论(0)
提交回复
热议问题