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

发布于 2024-12-01 02:45:49 字数 1362 浏览 2 评论 0原文

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

I'm trying to create a graphic interface using JPanel and JButton. So far it's good except when I'm creating the JButton instances, they seem to align within the same line. I want every button to be in one line, not all in the same line.

How do I achieve the desired effect?

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2024-12-08 02:45:49

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

另请参阅:

If you want to stack components, use BoxLayout. Also, make sure to follow the suggestions made by @camickr.

See also:

今天小雨转甜 2024-12-08 02:45:49

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

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


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

  1. 它有时使用常见的“驼峰式”命名法,但其他时候则不使用。
  2. 有一个名为Interface 的具体类。
  3. 有一个名为menu 的面板。
  4. 该框架出现在屏幕上,标题为 Panel
  5. 下次尝试用埃塞俄比亚语发表评论。我读过那篇文章,就像我读过法语一样。
  6. 所有这些与不一致的缩进相结合,导致代码难以阅读和使用。理解。

这是穷人的混淆视听吗?


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

Use a nested layout. The yellow and blue panels each have their own layout, and are then placed in constraints of the larger layout.

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);
    }
}

For a more comprehensive example of using nested layouts, see NestedLayoutExample.java.


BTW - That is some confusingly written code!

  1. It uses the common 'camel case' nomenclature sometimes, but not others.
  2. There is a concrete class with name Interface.
  3. There is a panel named menu.
  4. The frame appears on-screen titled Panel.
  5. Try putting comments in Ethiopian next time. I read that about as well as I read French.
  6. That all combined with the inconsistent indentation, results in code that is hard to read & understand.

Is this a poor man's form of obfuscation?


Note that Swing GUIs should be created on the EDT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文