JSplitPane 不绘画更正

JSplitPane not painting correcting

在我的应用程序中,我有一个 JFrame 显示 JSplitPane,拆分为 VERTICAL_SPLIT。上面显示一个JLabel,下面显示一个JInternalFrame。出现两个问题。

  1. 显示JLabel,显示JInternalFrame

    2。我必须调整应用程序的大小才能完全显示 JSplitPane

我认为这都与 JSplitPane 的错误使用有关。但是,我一直无法弄清楚是什么。我可以帮忙解决这个问题吗?

p.s。我有 运行 测试以确保 GUIWindow.getInsideFrame() 没有返回 null。最后的 instanceof 检查表明窗格中的两个组件都存在并且属于该类型。非常感谢您的帮助:

protected static void newWindow(GUIFrame window) {
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
                JInternalFrame intFrame = window.getInsideFrame();
                pane.setRightComponent(intFrame);
                pane.setLeftComponent(new JLabel(window.getDescription()));
                synchronized(lock){
                    frame.remove(currentPane);
                    frame.add(pane);
                }
                synchronized(lock){
                    frame.revalidate();
                    pane.setVisible(true);
                    frame.repaint();
                    if(window instanceof ColourFrameShower) return;
                    currentWindow = window;
                    currentPane = pane;
                    currentFrame = intFrame;
                }
                if(pane.getLeftComponent() instanceof JLabel) System.out.println("JLabel exists!");
                else System.out.println("JLabel does not exist!");
                
                if(pane.getRightComponent() instanceof JInternalFrame) System.out.println("JInternalFrame exists!");
                else System.out.println("JInternalFrame does not exist!");
            }
            
        });
    }

编辑:我通过在第二个 synchronised(lock) 块的开头调用 frame.revalidate() 解决了问题 2。这已包含在代码中。

尝试设置resizeWeight:pane.setResizeWeight(0.5);

正如我在评论中告诉你的,你只需要在你的 JInternalFrame 上使用 setVisible(true),否则你的 JSplitPane 不会考虑它。

这是 java 秋千上的一个非常常见的错误!

很高兴对您有所帮助 ;)