JTabbedPane的布局设置

时间:2014-12-30 05:52:26

标签: java swing layout jtabbedpane

我在java程序中有一个JTabbedPane。我为每个“tab”添加了一个JPanel,但是当我在每个面板(GridBagLayout和BorderLayout)中设置元素时,似乎没有效果。难道我做错了什么?我需要某种方式来控制布局吗?这是代码的一部分:

public static void main(String args[]) {
    // set L&F
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }

    JFrame main = new JFrame();
    JTabbedPane tabs = new JTabbedPane();
    JPanel checkOutPanel = new JPanel();

    Font f = new Font("Header", Font.BOLD, 24);
    Font f2 = new Font("Menu", Font.BOLD, 36);

    Font font = new Font("Menu", Font.BOLD, 24);

    // check out
    JLabel checkOutLabel = new JLabel("Checkout");
    JLabel bookNumLabel = new JLabel("Book Number");
    JLabel personNameLabel = new JLabel("Person Name");

    final JTextField bookNumEntry = new JTextField(20);
    final JTextField personNameEntry = new JTextField(20);
    JButton checkOutButton = new JButton("Check Out");
    checkOutLabel.setFont(font);
    // checkout gui
    GridBagConstraints co = new GridBagConstraints();
    co.gridx = 1;
    checkOutPanel.add(checkOutLabel, co);
    co.gridx = 0;
    co.gridy = 1;
    checkOutPanel.add(bookNumLabel, co);
    co.gridx = 1;
    checkOutPanel.add(bookNumEntry, co);
    co.gridx = 0;
    co.gridy = 2;
    checkOutPanel.add(personNameLabel, co);
    co.gridx = 1;
    checkOutPanel.add(personNameEntry, co);
    co.gridx = 2;
    checkOutPanel.add(checkOutButton, co);

    tabs.addTab("Checkout",checkOutPanel );
    tabs.setTabPlacement(JTabbedPane.LEFT);

    main.add(tabs);
    main.setSize(1300,1100);
    main.setVisible(true);
}

1 个答案:

答案 0 :(得分:3)

您没有为checkOutPanel设置布局管理器,因此它正在使用FlowLayout的默认布局管理器

更改...

JPanel checkOutPanel = new JPanel();

更喜欢......

JPanel checkOutPanel = new JPanel(new GridBagLayout());