2014-12-30 40 views
1

我在java程序中有一个JTabbedPane。我为每个“选项卡”添加了一个JPanel,但是当我在每个面板(GridBagLayout和BorderLayout)中设置样式时,似乎没有任何效果。难道我做错了什么?有什么方法可以控制布局吗?下面的代码的一个部分:JTabbedPane的布局设置

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

你是什么意思样式元素没有eff ECT?他们根本没有出现,或只出现在你不期望的地方? –

+0

他们没有出现在正确的地方。 – noobProgrammer

回答

3

您还没有设置checkOutPanel布局管理器,所以它用它的FlowLayout

更改的默认布局管理器...

JPanel checkOutPanel = new JPanel(); 

的东西更喜欢...

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