2015-10-13 38 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

我很乐意提供帮助。 –