如何以这种方式对齐JPanel中的Button

时间:2011-08-24 00:17:23

标签: java swing layout jpanel jbutton

我正在尝试使用JPanelJButton创建图形界面。到目前为止它很好,除非我在创建JButton实例时,它们似乎在同一行内对齐。我希望每个按钮都在一行中,而不是全部在同一行。

如何达到预期效果?

public class Interface  extends JFrame{

    public  Interface ()
    {
    //frame 
        super("Panel");
        pack();
        setSize(500,400);
        setVisible(true);

    //declaration container
    Container c;
    c=getContentPane();
    c.setLayout(new BorderLayout());
    //declaration des panel avec leurs caracteristiques

    JPanel menu =new JPanel();
    JPanel MessageList =new JPanel();
    JPanel about=new JPanel();

    menu.setLayout(new FlowLayout());
    MessageList.setLayout(new FlowLayout());
    about.setLayout(new FlowLayout());

    menu.setBackground(Color.blue);
    MessageList.setBackground(Color.cyan);
    about.setBackground(Color.cyan);

    c.add(menu,BorderLayout.WEST);
    c.add(MessageList,BorderLayout.EAST);
    c.add(about,BorderLayout.SOUTH);
    //--------Button---------------------
    JButton button1=new JButton("button1");
    JButton button2=new JButton("Button2");

    menu.add(button1);
    menu.add(button2);
    //-----------------------------
        }

    public static void main(String args[])
    {
        Interface Message=new Interface();
        Message.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2 个答案:

答案 0 :(得分:2)

如果要堆叠组件,请使用BoxLayout。另外,请务必遵循@camickr提出的建议。

另见:

答案 1 :(得分:2)

使用嵌套布局。黄色和蓝色面板每个都有自己的布局,然后放置在较大布局的约束中。

Message GUI

import java.awt.*;
import javax.swing.*;

public class Interface  extends JFrame{

    public  Interface ()
    {
        //frame
        super("Panel");

        //declaration container
        Container c;
        c=getContentPane();
        c.setLayout(new BorderLayout());
        c.setBackground(Color.white);
        //declaration des panel avec leurs caracteristiques

        JPanel menu =new JPanel(new GridLayout(0,1,3,3));
        JPanel messageList =new JPanel(new FlowLayout());
        JPanel about=new JPanel(new FlowLayout());

        menu.setBackground(Color.blue);
        messageList.setBackground(Color.cyan);
        messageList.add(new JLabel("'messageList' padder"));
        about.setBackground(Color.green);
        about.add(new JLabel("'about' padder"));

        JPanel menuConstrain = new JPanel(new BorderLayout());
        menuConstrain.setBackground(Color.yellow);

        menuConstrain.add(menu,BorderLayout.NORTH);
        c.add(menuConstrain,BorderLayout.WEST);
        c.add(messageList,BorderLayout.EAST);
        c.add(about,BorderLayout.SOUTH);
        //--------Button---------------------
        JButton button1=new JButton("button1");
        JButton button2=new JButton("Button2");

        menu.add(button1);
        menu.add(button2);
        //-----------------------------

        pack();
        setSize(300,150);
        setVisible(true);
    }


    public static void main(String args[])
    {
        Interface Message=new Interface();
        Message.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

有关使用嵌套布局的更全面示例,请参阅NestedLayoutExample.java


BTW - 这是一些容易混淆的代码!

  1. 它有时使用常见的“驼峰案例”命名法,但不使用其他命名法。
  2. 有一个名为Interface的具体类。
  3. 有一个名为menu的小组。
  4. 框架显示在屏幕上,标题为Panel
  5. 下次尝试在埃塞俄比亚发表评论。我读到的内容和我读法文一样。
  6. 这一切都与不一致的缩进相结合,导致难以阅读的代码和理解。
  7. 这是一个穷人的混淆形式吗?


    请注意,应在EDT上创建Swing GUI。