由于某些原因,我的代码中的边框布局不起作用,当我运行程序时,当我希望两个白色方块位于旁边的顶部时,两个JPanel
组件看起来像this彼此。
我目前正在构建一个程序,我将添加9个方块(面板),然后将图像放入其中,因此在我的按钮面板旁边是3 x 3。然而,我刚刚开始使用两个正方形,我甚至无法正确定位它们,它们固定在中心位置,当我添加另一个JPanel
时,它会自动进入我已放入的两个正方形旁边中心。
我对按钮面板的位置感到满意,但是我希望我的其他两个面板彼此相邻,这样我就可以继续构建我的程序了。
如何创建所需的布局&对齐图像面板?
MAIN CLASS
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);
}
}
SUB CLASS
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);
}
}