如何以这种方式对齐 JPanel 中的按钮
我正在尝试使用 JPanel
和 JButton
创建图形界面。到目前为止,一切都很好,除了当我创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要堆叠组件,请使用
BoxLayout
。另外,请务必遵循 @camickr 提出的建议。另请参阅:
If you want to stack components, use
BoxLayout
. Also, make sure to follow the suggestions made by @camickr.See also:
使用嵌套布局。黄色和蓝色面板各自有自己的布局,然后放置在较大布局的约束中。
有关使用嵌套布局的更全面示例,请参阅
NestedLayoutExample.java
。顺便说一句 - 这是一些令人困惑的代码!
Interface
的具体类。menu
的面板。Panel
。这是穷人的混淆视听吗?
请注意,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.
For a more comprehensive example of using nested layouts, see
NestedLayoutExample.java
.BTW - That is some confusingly written code!
Interface
.menu
.Panel
.Is this a poor man's form of obfuscation?
Note that Swing GUIs should be created on the EDT.