繁体   English   中英

JFrame组件的这种奇怪延迟是什么?

[英]What is this odd delay with JFrame Components

编辑:虽然我在今天之前从未使用过(甚至不知道) SwingUtilities.InvokeLater() ,但实现它并没有解决问题。 JFrame的可见性与JLabel的外观之间的1秒延迟仍然是以下更新的代码。

public class DelayTest {

public static void main(String [] args) {
    new DelayTest();
}

public DelayTest() {
     SwingUtilities.invokeLater(new Runnable() 
        {
          public void run()
          {
              long start = System.currentTimeMillis();
            JFrame frame = new JFrame("Demo");
            Container content = frame.getContentPane();

            //sizing
            Dimension dimm = new Dimension(500, 200);
            frame.setPreferredSize(dimm);
            frame.setResizable(false);

            //content
            content.add(new JLabel("Hello, Delayed World"));
            System.out.println("Added Label at: " + 
                    (System.currentTimeMillis() - start) + " Milliseconds");

            //end
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            System.out.println("Finished execution at: " + 
                    (System.currentTimeMillis() - start) + " Milliseconds");
          }
        });
    }
}

到目前为止,我已完成以下操作以尝试解决错误:

  • 更新了我的电脑(Mac OS X 10.11.6)
  • 更新的Java(java版“1.8.0_111”)
  • 删除和重新安装的Eclipse(Java Neon.2)
  • 导出jar,只是为了看看Eclipse之外是否存在问题
  • 使用SwingUtilities.InvokeLater()进行测试,如上所示(希望我正确使用它)

我在整个过程中多次重启,但似乎没有什么能解决它。 我希望你能帮助我在我的代码中找到延迟标签的错误(我认为不存在),或者验证我的代码在您的计算机上运行。

事实证明,我的问题是通过为content Container指定LayoutManager立即修复的。 我现在假设它是一个奇怪的Java bug,也许只在Mac上。 以下代码解决了我的问题:

public class DelayTest {

public static void main(String [] args) {
    new DelayTest();
}

public DelayTest() {
     SwingUtilities.invokeLater(new Runnable() 
        {
          public void run()
          {
              //long start = System.currentTimeMillis();
            JFrame frame = new JFrame("Demo");
            Container content = frame.getContentPane();

            //NEW CODE
            content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
            //END NEW CODE

            //sizing
            Dimension dimm = new Dimension(500, 200);
            frame.setPreferredSize(dimm);
            frame.setResizable(false);

            //content
            content.add(new JLabel("Hello, Delayed World"));

            //end
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

          }
        });
    }
}

拥有一个不使用一个布局管理器或其他布局管理器的GUI几乎完全没有意义,这就解释了为什么我以前从未见过这个问题。

TL; DR:即使你只需要在窗口中显示一个组件,也总是使用LayoutManager (和SwingUtilities.invokeLater() )。

删除对frame.setPreferredSize(dimm);的调用frame.setPreferredSize(dimm); 并在frame.setLocationRelativeTo(null); 添加以下内容:

frame.setSize(dimm);

暂无
暂无

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

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