如何使用 InvokeLater 在 JFrame 中显示我的所有组件?

问题描述

我正在尝试使用 JSplitPane 显示不同的 JPanel。

我使用 SwingUtilities.invokelater显示所有组件以避免 UI 缓慢。

但是在我调整窗口大小之前,底部的 JPanel 是 PinkPane 不会显示

我尝试使用多个线程在底部显示 JPanel:

-我使用了 2-3 个线程。一个来初始化类。一种用于重新验证 splitPane。最后一个SwingUtilities.invokelater(new Runnable...)(希望能解决问题)

这是我的代码

    package changing;
    
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    public class splitPaneFrame {
        
        private static JFrame mainFrame = new JFrame();
        private static JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        private static JSplitPane middle = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        
        private int width = 1500;
        private int height = 860;
        
        public splitPaneFrame() {
            // Todo Auto-generated constructor stub
            mainFrame.setSize(width,height);
            mainFrame.setVisible(true);
            mainFrame.setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE);
            initialization();
            mainFrame.add(splitPane);
        }
        
        private static void initialization() {
            JSplitPane mostRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    
            JPanel cyanPane = new JPanel();
            cyanPane.setBackground(Color.CYAN);
    
            JPanel orangePane = new JPanel();
            orangePane.setBackground(Color.ORANGE);
            
            mostRight.setDividerSize(0);
            mostRight.setDividerLocation(400);
            mostRight.setLeftComponent(cyanPane);
            mostRight.setRightComponent(orangePane);
            mostRight.setonetouchExpandable(false);
            
            JPanel yellowPane = new JPanel();
            yellowPane.setBackground(Color.YELLOW);
            
            yellowPane.setSize(new Dimension(600,800));
            yellowPane.setMaximumSize(yellowPane.getSize());
            yellowPane.setPreferredSize(yellowPane.getSize());
            middle.setDividerSize(0);
            middle.setDividerLocation(600);
            middle.setLeftComponent(yellowPane);
            middle.setRightComponent(mostRight);
            middle.setonetouchExpandable(false);
            
            JPanel magentaPane = new JPanel();
            magentaPane.setBackground(Color.magenta);
            
            JSplitPane mostLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            
            mostLeft.setDividerLocation(250);
            mostLeft.setDividerSize(0);
            mostLeft.setLeftComponent(magentaPane);
            mostLeft.setRightComponent(middle);
            mostLeft.setonetouchExpandable(false);
            
            // vertically medium panes end here
            ////////////////////////////////////////////////////////////////////////////////
            
            // Top and Bottom Horizontal Panes start here
            // splitPane on the most bottom
            JSplitPane bottom = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            
            JPanel pinkPane = new JPanel();
            pinkPane.setBackground(Color.PINK);
            
            pinkPane.setSize(new Dimension(1500,30));
            pinkPane.setPreferredSize(new Dimension(1500,30));
            pinkPane.setMaximumSize(pinkPane.getPreferredSize());
            
            bottom.setDividerLocation(800);
            bottom.setDividerSize(0);
            bottom.setTopComponent(mostLeft);
            bottom.setBottomComponent(pinkPane);
            bottom.setonetouchExpandable(false);
            bottom.setVisible(true);
            pinkPane.setVisible(true);
            
            JPanel redPane = new JPanel();
            redPane.setBackground(Color.RED);
            redPane.setSize(new Dimension(1500,30));
    
            // main splitPane
            splitPane.setDividerLocation(30);
            splitPane.setDividerSize(0);
            splitPane.setTopComponent(redPane);
            splitPane.setBottomComponent(bottom);
            splitPane.setonetouchExpandable(false);
        }
        
        public static void main(String[] args) {
            // Todo Auto-generated method stub
            SwingUtilities.invokelater(new Runnable() {
                
                public void run() {
                    // Todo Auto-generated method stub
                    splitPaneFrame frame = new splitPaneFrame();
                }
            });
        }
    
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...