java - BorderLayout.CENTER "disappears"

标签 java swing border-layout

我正在编写一个原型(prototype),但遇到了 GUI 问题。 我希望 JPanel pCustomer 居中,但这样做它会完全消失。例如,如果我把它放在南方,一切都很好。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JPanel implements ActionListener {

    private JPanel pTop = new JPanel();
    private JPanel pMenue = new JPanel();
    private JPanel pContent = new JPanel();
    private JPanel pCustomer = new JPanel();
    private JPanel pEnq = new JPanel();
    private JPanel pCustomerMenue = new JPanel();
    private JTextField tf1 = new JTextField();
    private JButton bCustomer = new JButton("Customer");
    private JButton bEnq = new JButton("Product");
    private JButton bCNew = new JButton("New Customer");
    private JLabel lCustomer = new JLabel("Customer");
    String[] customerString = {"--- SELECT -- ", "New Customer", "Edit Customer", "Delete Customer"};
    private JComboBox cb1 = new JComboBox(customerString);
    private JLabel lRes = new JLabel();
    String[] productString = {"--- SELECT -- ", "Sell Product", "Enquire Product", "Complain Product"};
    private JLabel lWelcome = new JLabel("Welcome to our System!");
    private JLabel lNo = new JLabel("Customer Number:   ");
    private JLabel lEnq = new JLabel("Enquiry");

    public Test() {
        this.setLayout(new BorderLayout());

        // pTop
        this.add(pTop, BorderLayout.NORTH);
        pTop.setLayout(new BorderLayout());
        pTop.add(lNo, BorderLayout.WEST);
        pTop.add(tf1, BorderLayout.CENTER);

        // pMenue
        this.add(pMenue, BorderLayout.WEST);
        pMenue.setLayout(new GridLayout(5, 1));
        pMenue.add(bCustomer);
        pMenue.add(bEnq);

        // pContent        
        this.add(pContent, BorderLayout.CENTER);
        pContent.add(lWelcome);
        pContent.setLayout(new BorderLayout());

        pContent.setBackground(Color.GREEN);

        // pCustomer
        pContent.add(pCustomer, BorderLayout.CENTER); // EAST, SOUTH, WEST works, but I want it to be centered.
        pCustomer.add(cb1);
        pCustomer.add(lRes);
        pCustomer.setVisible(false);
        pCustomer.setBackground(Color.blue);

        // pCustomerMenue
        pContent.add(pCustomerMenue, BorderLayout.NORTH);
        pCustomerMenue.add(bCNew);
        pCustomerMenue.setVisible(false);
        pCustomerMenue.setBackground(Color.red);

        // pEnq
        pContent.add(pEnq, BorderLayout.CENTER);
        pEnq.add(lEnq);
        pEnq.setVisible(false);

        // ---

        bCustomer.addActionListener(this);
        bEnq.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        lWelcome.setVisible(false);

        if (source == bCustomer) {
            init();
            pCustomer.setVisible(true);
            pCustomerMenue.setVisible(true);
            bCustomer.setEnabled(false);
        }
        if (source == bEnq) {
            init();
            pEnq.setVisible(true);
            bEnq.setEnabled(false);
        }
    }

    public void init() {
        pCustomer.setVisible(false);
        pCustomerMenue.setVisible(false);
        pEnq.setVisible(false);
        bCustomer.setEnabled(true);
        bEnq.setEnabled(true);
    }
}

如果我删除这 3 行:

pContent.add(pEnq, BorderLayout.CENTER);
pEnq.add(lEnq);
pEnq.setVisible(false);

我什至可以将它放在中心,而且效果很好。

最佳答案

阅读有关边框布局的信息。基本上你有 5 个位置(北、东、南、西、中心),每当你将一个组件放在这些位置之一时,任何已经在该位置的组件都会被替换。

因此 pContent.add(pEnq, BorderLayout.CENTER); 将用 pEnq 替换 pCustomer

如果您希望两个面板都位于中心,您需要将一个中间面板放在中心,然后将其他面板添加到该面板,或者使用另一个布局管理器,例如MiGLayout .

使用 MiGLayout,您的 pContent 布局可能如下所示:

pContent.setLayout(new MiGLayout());

pContent.add(pCustomerMenue, "pushx, wrap"); //fill the available width, new line after this component
pContent.add(pCustomer, "pushx, wrap"); //fill the available width, new line after this component
pContent.add(pEnq, "pushx"); //fill the available width

关于java - BorderLayout.CENTER "disappears",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8669162/

相关文章:

java - 在 OOP 中更新字段

java - 抛出 vs Java 字节码

java - 为什么下面的代码中 FirstThread 总是先于 SecondThread 运行?

java - 使用 GridBagLayout 在 JTextArea 中添加 JScrollPane

java - 什么相当于 JavaFX 中的 JPanel

java - 使 BorderLayout 中的项目占据正确的大小

java - 如何在边框布局中添加多个按钮

java - 如何通过网络将数据从一个HDFS集群迁移到另一个集群?

java - 我需要能够通过另一个类中的按钮更改java中框架的标题

java - 使用 BorderLayout.centre 将 JPanel 放置在中心,在所有 4 个方向上间隔 50 像素