BorderLayout.CENTER“消失”
我正在编写原型,但在 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);
我什至可以将它放在中心并且它可以工作。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读有关边框布局的内容。基本上,您有 5 个位置(北、东、南、西、中心),每当您将组件放入其中一个位置时,该位置中已有的任何组件都会被替换。
因此,
pContent.add(pEnq, BorderLayout.CENTER);
将用pEnq
替换pCustomer
。如果您希望两个面板都位于中心,则需要将一个中间面板放入中心,然后将其他面板添加到该面板,或者使用其他布局管理器,例如
MiGLayout
。使用
MiGLayout
,您的pContent
布局可能如下所示: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 replacepCustomer
withpEnq
.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
yourpContent
layout might look like this:您尝试将两个不同的面板添加到 BorderLayout 的中心。
首先添加
几行,然后添加:
所以 pEnq 位于 pCustomer 之上!
You try to add two different Panels to the Center of the BorderLayout.
First you add
And a few Lines later you do:
So pEnq lays over pCustomer!