1

我有一个带有内容窗格的 JFrame。JMenuBar 停靠在窗格的北部,而 JLabel(各种状态栏)停靠在南部。

中间是一个 JTabbedPane。每个选项卡都是一个“文档”。它的视口中包含一个 JScrollBar 和一个 JPanel。

它一直在继续(视口的 JPanel 有更多的 JPanel,可以有更多的 JPanel,等等......),但对于这个例子,让我们说 JPanel(在视口中)可以或不适合窗口空间(因此它不能或可以强制在屏幕上显示滚动条)。

当它适合窗口时,一切都很好,但是一旦我将它的高度设置为太高而无法放入窗口中,JMenuBar 就会在顶部被挤压。

我想防止这种情况发生(不必指定 JMenuBar 的绝对高度,它可能会起作用,但它有点便宜),因为它不应该首先发生。

这里是SCCE(不是很短,但你只需要看第37到117行,我已经用//TODO标记了所有与布局有关的行)。此外,要查看问题何时发生或何时不发生,请在 2000 和 200 之间更改第 88 行的高度值。当然,您还需要一个 MiG 布局库。

这是代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import net.miginfocom.swing.MigLayout;

class Menu extends JMenuBar
{
    private static final long serialVersionUID = 1L;

    public Menu()
    {
        JMenu fileMenu = new JMenu("file"); 
        this.add(fileMenu);
    }
}

class DisplayGUI
{
    JTabbedPane documentSelector;

    void addNewDocument(String name)
    {
        Document newDocument = new Document();
        newDocument.addChapter(new Chapter(), 1);
        documentSelector.add(newDocument, name);
    }

    public DisplayGUI()
    {   
        JFrame masterWindow = new JFrame("name");

        masterWindow.setSize(1100, 800);
        masterWindow.setMinimumSize(new Dimension(400, 400));
        masterWindow.setLocationRelativeTo(null);
        masterWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel rootPanel = new JPanel();
        rootPanel.setLayout(new MigLayout()); //TODO Here is layout set for the content pane of the main JFrame

        Menu menuBar = new Menu();
        rootPanel.add(menuBar, "span, north"); //TODO Here is menu bar added to the JFrame, it's docked north

        JLabel statusBar = new JLabel("Welcome to PLabScript editor! Press File>New to create a new file or go to File>Open to open an existing one.");
        statusBar.setOpaque(true);
        statusBar.setBorder(BorderFactory.createLoweredSoftBevelBorder());
        rootPanel.add(statusBar, "span, south"); //TODO Here is status bar added to the JFrame, it's docked south

        documentSelector = new JTabbedPane(JTabbedPane.NORTH); //TODO JTabbedPane set so the tab chooser is on the top
        rootPanel.add(documentSelector, "grow, push"); //TODO setup so it will take up all the remaining space


        addNewDocument("Brand new document");       


        masterWindow.setContentPane(rootPanel);
        masterWindow.setVisible(true);
    }
}

class Document extends JScrollPane
{
    private static final long serialVersionUID = 1L;
    JPanel basePanel;

    //methods
    void addChapter(Chapter chapter, int index)
    {
        basePanel.add(chapter, "grow, push, h 2000", index-1); //TODO this here adds a chapter to the basePanel of the JScrollPane which is the a representative of a single document
        //TODO it height is set to 2000 (and the problem occurs), but if you reduce it enough so it fits the window, problem will dissaper 
    }

    //constructors
    public Document()
    {
        super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        getVerticalScrollBar().setUnitIncrement(20);

        basePanel = new JPanel();
        basePanel.setBackground(Color.RED);
        basePanel.setLayout(new MigLayout("insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components

        setViewportView(basePanel);
    }

}

class Chapter extends JPanel
{
    private static final long serialVersionUID = 1L;

    //constructors
    Chapter()
    {
        setLayout(new MigLayout("insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components
        setBackground(Color.MAGENTA);
    }
}


public class Main
{
    public static ResourceBundle language;

    static boolean setUpLAF()
    {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
        {
            if ("Nimbus".equals(info.getName()))
            {
                try
                {
                    UIManager.setLookAndFeel(info.getClassName());
                }
                catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)

                {
                    return false;
                }

                break;
            }
        }
        return true;
    }


    public static void main(String[] args)
    {       
        //SetUpLookAndFeel
        setUpLAF(); 

        //Display actual GUI
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new DisplayGUI();
            }
        });
    }
}
4

1 回答 1

1

第 88 行应为:

    basePanel.add(chapter, "grow, push", index-1); //TODO this here adds a chapter to the basePanel of the JScrollPane which is the a representative of a single document

第 100 行应为:

    basePanel.setLayout(new MigLayout("fill,insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components

尝试这个。

于 2013-01-18T19:44:55.110 回答