BoxLayout 显示不正确

BoxLayout Displaying Incorrectly

我正在尝试创建一个非常简单的应用程序,它在状态栏面板上方有一个 JSplitPane(分为 JTabbedPane 和 JPanel)。我想使用一个简单的布局(即 BoxLayout、FlowLayout 或 BorderLayout),但我试过了,它们都给我同样的错误。我已尽可能简化代码以显示错误。

错误是主框布局(框架)中应该只有 2 个区域:顶部(带有黑色边框的 JSplitPane)和底部(带有 JPanel 状态栏)。但是,当我添加状态栏时,会在左上角创建一个不包含任何内容的第三个区域。关于如何摆脱它的任何想法?

public static void main(String[] args) {
    JFrame frame = new JFrame("Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

    // Create left side of the application
    JTabbedPane tabby = new JTabbedPane(JTabbedPane.LEFT);

    // Create right side of the application
    JPanel rightPanel = new JPanel(new BorderLayout());

    // Create the status bar at the bottom
    JPanel statusBar = new JPanel(new BorderLayout());
    JPanel statusBarPanel = new JPanel();
    statusBarLabel = new JLabel("Status Bar");
    statusBarPanel.add(statusBarLabel);
    parent.add(statusBarPanel);

    JSplitPane mainPain = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tabby, rightPanel);
    frame.add(mainPain);
    frame.add(statusBar);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true); 

I'm trying to create a pretty simple application that has a JSplitPane (which is divided into a JTabbedPane and a JPanel) above a status bar panel.

通常你会使用框架的默认 BorderLayout 然后做:

frame.add(splitPane, BorderLayout.CENTER);
frame.add(statusBar, BorderLayout.PAGE_END);

状态栏通常是一个或多个显示信息的标签,因此它们以固定大小显示在底部。

另一个面板将包含应用程序的主要组件。然后,这些组件将在框架调整大小时获得任何额外的 space 可用。

but I've tried and they all give me the same error

parent.add(statusBarPanel);

变量"parent"不存在。摆脱它。将状态栏添加到框架中,如上所示。

不确定是不是这样,但看起来,就像您在添加状态栏时添加了 2 个面板一样。 您有一个添加到 "parent" 的 statusBarPanel 和一个添加到框架本身的 statusBar。也许那是你的第三个小组。