【问题标题】:How to align Button in a JPanel this way如何以这种方式在 JPanel 中对齐 Button
【发布时间】:2011-08-24 00:17:23
【问题描述】:

我正在尝试使用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);
    }
}

【问题讨论】:

标签: java swing layout jpanel jbutton


【解决方案1】:

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

另见:

【讨论】:

    【解决方案2】:

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

    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


    顺便说一句 - 这是一些令人困惑的代码!

    1. 它有时使用常见的“驼峰式”命名法,但有时不使用。
    2. 有一个名为Interface 的具体类。
    3. 有一个名为menu的面板。
    4. 框架出现在屏幕上,标题为Panel
    5. 下次尝试将 cmets 放入埃塞俄比亚语。我读的和读的法语差不多。
    6. 所有这些都与不一致的缩进相结合,导致代码难以阅读和理解。

    这是一个穷人的混淆形式吗?


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 2017-03-14
      • 2021-07-15
      • 2021-06-28
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2014-02-14
      相关资源
      最近更新 更多