BorderLayout.CENTER“消失”

发布于 2024-12-23 06:01:56 字数 3433 浏览 3 评论 0 原文

我正在编写原型,但在 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);

我什至可以将它放在中心并且它可以工作。

I'm coding a prototype, but got problems with the GUI.
I want the JPanel pCustomer to be centered, but doing so it disappears completely. If I put it for example in the SOUTH, everything is fine.

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);
    }
}

If I remove these 3 lines:

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

I can even put it in the Centre and it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も让我眼熟你 2024-12-30 06:01:56

阅读有关边框布局的内容。基本上,您有 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

Read up about border layout. Basically you have 5 positions (NORTH, EAST, SOUTH, WEST, CENTER) and whenever you put a component into one of those positions any component already in that position is replaced.

Thus pContent.add(pEnq, BorderLayout.CENTER); will replace pCustomer with pEnq.

If you want both panels in the center you either need to put an intermediate panel into the center and then add the other panels to that one or use another layout manager, e.g. MiGLayout.

With MiGLayout your pContent layout might look like this:

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
赤濁 2024-12-30 06:01:56

您尝试将两个不同的面板添加到 BorderLayout 的中心。

首先添加

pContent.add(pCustomer, BorderLayout.CENTER); // EAST, SOUTH, WEST works, but I want it to be centered.

几行,然后添加:

pContent.add(pEnq, BorderLayout.CENTER);

所以 pEnq 位于 pCustomer 之上!

You try to add two different Panels to the Center of the BorderLayout.

First you add

pContent.add(pCustomer, BorderLayout.CENTER); // EAST, SOUTH, WEST works, but I want it to be centered.

And a few Lines later you do:

pContent.add(pEnq, BorderLayout.CENTER);

So pEnq lays over pCustomer!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文