2015-10-13 35 views
2

我必須創建一段代碼,將一個按鈕塊排列在另一個下面。最終的結果應該是這樣的: Correct Code enter image description here按鈕佈局的兩組按鈕在Java中

我到目前爲止是這樣的: My Code enter image description here

我需要移動在右邊的長線以下按鈕塊按鈕,但我嘗試擰緊按鈕的長行。任何意見,將不勝感激!

public class LayoutManagement extends JComponent { 

public LayoutManagement() { 
    setLayout(new FlowLayout()); 

    JPanel main = new JPanel(new GridLayout(1,3,20,0)); 
    main.setBorder(new EmptyBorder(10,10,10,10)); 

    final JLabel message = new JLabel("Default Message"); 

    String[] labels = "This class will give you practice creating and laying out containers".split("\\s"); 
    for (final String label : labels) { 
     JButton button = new JButton(label); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       message.setText(label); 
      } 
     }); 
     add(button); 
    } 

    main.add(message, BorderLayout.NORTH); 

    JPanel column1 = new JPanel(new GridLayout(6,1,0,5)); 
    JPanel column2 = new JPanel(new GridLayout(2,1,0,5)); 
    JPanel column3 = new JPanel(new GridLayout(5,1,0,5)); 
    JPanel col1 = new JPanel(new BorderLayout()); 
    JPanel col2 = new JPanel(new BorderLayout()); 
    JPanel col3 = new JPanel(new BorderLayout()); 
    JPanel col1ex = new JPanel(new GridBagLayout()); 
    JPanel col2ex = new JPanel(new GridBagLayout()); 
    JPanel col3ex = new JPanel(new GridBagLayout()); 
    main.add(col1, BorderLayout.SOUTH); 
    main.add(col2, BorderLayout.SOUTH); 
    main.add(col3, BorderLayout.SOUTH); 

    final JLabel message1 = new JLabel(); 
    message1.setPreferredSize(new Dimension(125, 50)); 
    message1.setBackground(Color.PINK); 
    message1.setOpaque(true); 
    message1.setBorder(new TitledBorder("Message 1")); 
    message1.setHorizontalAlignment(0); 

    final JLabel message2 = new JLabel(); 
    message2.setPreferredSize(new Dimension(125,50)); 
    message2.setBackground(Color.PINK); 
    message2.setOpaque(true); 
    message2.setBorder(new TitledBorder("Message 2")); 
    message2.setHorizontalAlignment(0); 

    String[] label1 = "This class will give you practice".split("\\s"); 
    for(final String label : label1) { 
     JButton button = new JButton(label); 
     button.setBackground(Color.CYAN); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       message1.setText(label); 
      } 
     }); 
     column1.add(button); 
    } 

    String[] label2 = "creating and laying out containers".split("\\s"); 
    for(final String label : label2) { 
     JButton button = new JButton(label); 
     button.setBackground(Color.CYAN); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       message2.setText(label); 
      } 
     }); 
     column3.add(button); 
    } 

    col1ex.add(column1); 
    col1.add(col1ex); 
    col2ex.add(column2); 
    column2.add(message1); 
    column2.add(message2); 
    col2.add(col2ex); 
    col3ex.add(column3); 
    col3.add(col3ex); 

    add(main); 
} 

/** 
* @param args the command line arguments -- not used 
*/ 
public static void main(String[] args) { 
    JFrame frame = new JFrame("Layout Management Lab"); 
    frame.add(new LayoutManagement()); 

    frame.pack(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 

}

+0

什麼佈局管理器,你想在這裏使用?你的'main'面板有一個'GridLayout',但你用'BorderLayout'常量添加東西。 – gla3dr

+0

我正在嘗試使用GridLayout。我對這些事情不熟悉,這就是爲什麼我無法實現它的原因......我嘗試將總體佈局設置爲GridLayout,但是這只是搞砸了按鈕行。 –

回答

0

這裏是頂得上的按鈕的一種方式,和它下面其他的東西:,你需要把按鈕放在一個容器中,而不是將它們添加一個使用BorderLayout 還由一個頂部的JComponent。

我修改你的代碼,注意到我的評論中所指出的差異:

public class LayoutManagement extends JComponent { 

public LayoutManagement() { 
    setLayout(new BorderLayout());//CHANGE use BorderLayout instead of FLowlayout 

    JPanel main = new JPanel(new GridLayout(1,3,20,0)); 
    main.setBorder(new EmptyBorder(10,10,10,10)); 

    final JLabel message = new JLabel("Default Message"); 

    JPanel pnlNorth = new JPanel();//CHANGE add the buttons to a panel 
    pnlNorth.setLayout(new FlowLayout());//CHANGE set its layout 
    String[] labels = "This class will give you practice creating and laying out containers".split("\\s"); 
    for (final String label : labels) { 
     JButton button = new JButton(label); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       message.setText(label); 
      } 
     }); 
     pnlNorth.add(button);//CHANGE add buttons to the panel 
    } 
    add(pnlNorth, BorderLayout.NORTH);//CHANGE add the panel to the north section 

    main.add(message, BorderLayout.NORTH); 

    JPanel column1 = new JPanel(new GridLayout(6,1,0,5)); 
    JPanel column2 = new JPanel(new GridLayout(2,1,0,5)); 
    JPanel column3 = new JPanel(new GridLayout(5,1,0,5)); 
    JPanel col1 = new JPanel(new BorderLayout()); 
    JPanel col2 = new JPanel(new BorderLayout()); 
    JPanel col3 = new JPanel(new BorderLayout()); 
    JPanel col1ex = new JPanel(new GridBagLayout()); 
    JPanel col2ex = new JPanel(new GridBagLayout()); 
    JPanel col3ex = new JPanel(new GridBagLayout()); 
    main.add(col1, BorderLayout.SOUTH); 
    main.add(col2, BorderLayout.SOUTH); 
    main.add(col3, BorderLayout.SOUTH); 

    final JLabel message1 = new JLabel(); 
    message1.setPreferredSize(new Dimension(125, 50)); 
    message1.setBackground(Color.PINK); 
    message1.setOpaque(true); 
    message1.setBorder(new TitledBorder("Message 1")); 
    message1.setHorizontalAlignment(0); 

    final JLabel message2 = new JLabel(); 
    message2.setPreferredSize(new Dimension(125,50)); 
    message2.setBackground(Color.PINK); 
    message2.setOpaque(true); 
    message2.setBorder(new TitledBorder("Message 2")); 
    message2.setHorizontalAlignment(0); 

    String[] label1 = "This class will give you practice".split("\\s"); 
    for(final String label : label1) { 
     JButton button = new JButton(label); 
     button.setBackground(Color.CYAN); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       message1.setText(label); 
      } 
     }); 
     column1.add(button); 
    } 

    String[] label2 = "creating and laying out containers".split("\\s"); 
    for(final String label : label2) { 
     JButton button = new JButton(label); 
     button.setBackground(Color.CYAN); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       message2.setText(label); 
      } 
     }); 
     column3.add(button); 
    } 

    col1ex.add(column1); 
    col1.add(col1ex); 
    col2ex.add(column2); 
    column2.add(message1); 
    column2.add(message2); 
    col2.add(col2ex); 
    col3ex.add(column3); 
    col3.add(col3ex); 

    add(main, BorderLayout.CENTER); //CHANGE put the other stuff in the center section 
} 
+0

謝謝!唯一的問題是,頂部按鈕的消息在下面,但我重新排列,現在它工作正常。我現在明白了BorderLayout的使用,謝謝! –

+0

我很樂意提供幫助。 –