由于某种原因,我的代码中的边框布局不起作用,当我运行程序时,两个 JPanel
组件看起来像 this当我希望两个白色方 block 位于彼此相邻的顶部时。
我目前正在构建一个程序,我将在其中添加 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);
}
}
最佳答案
关于Java BorderLayout 不起作用,JPanel 不会按照我希望的方式移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50185853/