问题描述
我想将两个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我会推荐是。
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!