首页 > 解决方案 > 如何使用 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();
                }
            });
        }
    
    }

标签: javaswingjsplitpaneinvokelater

解决方案


推荐阅读