Java BorderLayout 不起作用,JPanel 不会按照我希望的方式移动

标签 java swing user-interface layout-manager border-layout

由于某种原因,我的代码中的边框布局不起作用,当我运行程序时,两个 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);
    }
}

最佳答案

从表面上看,您希望实现这样的目标:

我将使用下面标题边框中描述的布局组合。或者更具体地说,下面的标题边框表示用于实现上面看到的布局的布局组合。

enter image description here

关于Java BorderLayout 不起作用,JPanel 不会按照我希望的方式移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50185853/

相关文章:

java - 我需要导入什么才能让 "Visitable"接口(interface)在 java 中工作?

java - 如何提取 Java 电子邮件正文而不将每行自动换行为 76 个字符

java - 如何使用java绘制动态树?

java - 获取调用 ActionListener 的对象的实例

java - 在 gwt 中创建自定义按钮

javascript - React Native 模态选择器中的全屏

java - 使用 Maven 打包 Java 代码时出现类、接口(interface)或枚举预期错误

java - keyPressed 和 keyReleased 不起作用

java - 如何在 Java Swing 中设置当前屏幕尺寸?

javascript - 网络自动化工具