MiG Layout,JScrollBar和JTabbedPane

时间:2013-01-18 17:36:50

标签: java swing layout jscrollpane jtabbedpane

我有一个JFrame及其内容窗格。 JMenuBar停靠在窗格的北面和南边的JLabel(各种状态栏)。

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

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

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

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

这是SCCE(它不是很短,但你只需要查看37到117行,我已经用// TODO标记了所有与布局有关的行)。此外,要查看问题何时发生或何时不发生,请在第88行中更改高度值,介于2000和200之间。当然,还需要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();
            }
        });
    }
}

1 个答案:

答案 0 :(得分: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

试试这个。