【发布时间】:2018-10-15 14:04:18
【问题描述】:
由于某种原因,我的代码中的边框布局不起作用,当我运行程序时,两个JPanel 组件看起来像this,而我希望两个白色方块在顶部彼此相邻。
我目前正在构建一个程序,我将在其中添加 9 个正方形(面板),然后将图像放入其中,因此它在按钮面板旁边是 3 x 3。但是我刚开始使用两个正方形,我什至无法正确定位它们,它们固定在中心,当我添加另一个 JPanel 时,它会自动放在我已经放在中心的两个旁边.
我对按钮面板的位置感到满意,但我希望我的其他两个面板彼此相邻,这样我就可以继续构建我的程序。
如何创建所需的图像面板布局和对齐方式?
主类
public static void main(String[] args) {
JFrame application = new JFrame ("Call of Traders");
GUI graphicalInterface = new GUI();
application.add(graphicalInterface);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setLocation(200,200);
application.pack();
application.setVisible(true);
application.setResizable(false);
}
}
子类
public class GUI extends JPanel implements ActionListener {
private JButton AddChairBTN = new JButton();
private JButton AddTableBTN = new JButton();
private JButton AddDeskBTN = new JButton();
private JButton NewCalcBTN = new JButton();
private JPanel buttonPanel;
private JPanel imagePanel;
private JPanel imagePanel2;
private Chair customerChair = new Chair();
private Table customerTable = new Table();
private Desk customerDesk = new Desk();
GUI() {
//create button panel
buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(100, 300));
buttonPanel.setOpaque(true);
buttonPanel.setBackground(Color.white);
imagePanel = new JPanel(new BorderLayout());
imagePanel.setPreferredSize(new Dimension(100, 100));
imagePanel.setOpaque(true);
imagePanel.setBackground(Color.white);
imagePanel2 = new JPanel(new BorderLayout());
imagePanel2.setPreferredSize(new Dimension(100, 100));
imagePanel2.setOpaque(true);
imagePanel2.setBackground(Color.white);
AddChairBTN = new JButton();
//add action listener to each button
AddChairBTN.addActionListener(this);
//set button size
AddChairBTN.setPreferredSize(new Dimension(100, 50));
//set text for each button
AddChairBTN.setText("Add Chair");
AddChairBTN.setToolTipText("press to add a Chair");
//add buttons to gui
buttonPanel.add(AddChairBTN);
AddTableBTN = new JButton();
//add action listener to each button
AddTableBTN.addActionListener(this);
//set button size
AddTableBTN.setPreferredSize(new Dimension(100, 50));
//set text for each button
AddTableBTN.setText("Add Table");
AddTableBTN.setToolTipText("press to add a Table");
//add buttons to gui
buttonPanel.add(AddTableBTN);
AddDeskBTN = new JButton();
//add action listener to each button
AddDeskBTN.addActionListener(this);
//set button size
AddDeskBTN.setPreferredSize(new Dimension(100, 50));
//set text for each button
AddDeskBTN.setText("Add Desk");
AddDeskBTN.setToolTipText("press to add a Desk");
//add buttons to gui
buttonPanel.add(AddDeskBTN);
NewCalcBTN = new JButton();
//add action listener to each button
NewCalcBTN.addActionListener(this);
//set button size
NewCalcBTN.setPreferredSize(new Dimension(100, 50));
//set text for each button
NewCalcBTN.setText("Calculate");
NewCalcBTN.setToolTipText("press to find out the total");
//add buttons to gui
buttonPanel.add(NewCalcBTN);
//Add all panels to main containter panel and add that to the window
this.add(buttonPanel);
this.add(imagePanel, BorderLayout.NORTH);
this.add(imagePanel2, BorderLayout.SOUTH);
}
}
【问题讨论】:
-
“我将添加 9 个正方形(面板),然后将图像放入其中,因此它是 3 x 3 旁边的按钮面板” 我倾向于使用 ( 3x3)
GridLayout用于 GUI 的该部分。 -
一般提示:1) 为了尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。 2) 请对代码和代码 sn-ps、HTML/XML 等结构化文档或输入/输出使用代码格式。为此,请选择文本并单击消息发布/编辑表单顶部的
{}按钮。 -
BTW -
this没有使用代码似乎假定的布局。//Add all panels to main containter panel and add that to the window this.add(buttonPanel); this.add(imagePanel, BorderLayout.NORTH); ..假定为BorderLayout。JPanel(默认)使用FlowLayout。 -
@AndrewThompson 感谢您的回复 Andrew,那么您认为最好只做一个 JPanel 并添加我的 9 3 x 3 图像吗?而不是为每个单独的图像做 9 个 JPanel?为什么“this”不使用边框布局?
-
1) 我没有过多考虑是 1 个还是 9 个面板更好,但一个面板可能包含 9 个标签或按钮,每个标签或按钮都有自己的图像图标或文本。 2) 因为面板的默认布局是
FlowLayout。您可以(并且应该)在添加组件之前使用断点或打印系统检查这些假设。或者当然,只需将其明确设置为BorderLayout。 3) 顺便说一句 - 这是basic intent of the layout吗?
标签: java swing user-interface layout-manager border-layout