将多个jPanel添加到jFrame

编程入门 行业动态 更新时间:2024-10-27 09:38:32
本文介绍了将多个jPanel添加到jFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将两个jPanel并排添加到JFrame中。两个框是jpanels,外框是jframe

I want to add two jPanels to a JFrame side by side. the two boxes are jpanels and the outer box is a jframe

我有这些代码行。我有一个名为seatinPanel的类,它扩展了JPanel,在这个类中我有一个构造函数和一个名为utilityButtons的方法,它返回一个JPanel对象。我希望utilityButtons JPanel在右侧。我这里的代码只在运行时显示utillityButtons JPanel。

I have these lines of code. I have one class called seatinPanel that extends JPanel and inside this class I have a constructor and one method called utilityButtons that return a JPanel object. I want the utilityButtons JPanel to be on the right side. the code I have here only displays the utillityButtons JPanel when it runs.

public guiCreator() { setTitle("Passenger Seats"); //setSize(500, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = getContentPane(); seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons contentPane.add(seatingPanel1); //adding the seats contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents setVisible(true); }

推荐答案

最灵活的LayoutManager我会推荐是 BoxLayout 。

The most flexible LayoutManager I would recommend is BoxLayout.

您可以执行以下操作:

JPanel container = new JPanel(); container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS)); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); //panel1.set[Preferred/Maximum/Minimum]Size() container.add(panel1); container.add(panel2);

然后将容器添加到框架组件的对象。

then add container to object to your frame component.

更多推荐

将多个jPanel添加到jFrame

本文发布于:2023-08-06 03:33:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   jPanel   jFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!