2011-12-29 20 views
1

我編碼一個原型,但得到了GUI問題。 我想要的JPanel pCustomer爲中心,但它這樣做完全消失。如果我把它放在南方,一切都很好。BorderLayout.CENTER「消失」

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

如果我刪除這些三線:

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

我甚至可以把它的中心和它的作品。

+0

另外檢查http://stackoverflow.com/questions/8660751/java-swing-jframe-layout這是一個非常類似的問題 – Robin 2011-12-29 14:52:45

回答

2

閱讀了有關邊界佈局。基本上你有5個職位(北部,東部,南部,西部,中心),每當你把一個組件放到其中一個位置時,已經在那個位置的任何組件都被替換了。

因此pContent.add(pEnq, BorderLayout.CENTER);將用pEnq代替pCustomer

如果你想在中心,你要麼需要把中間面板爲中心,然後其他面板添加到一個或使用其他佈局管理器,例如兩個面板MiGLayout

隨着MiGLayoutpContent佈局可能是這樣的:

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 
2

您嘗試兩種不同的面板添加到的BorderLayout的中心。

首先,添加

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

而且幾行後,你做的事:

pContent.add(pEnq, BorderLayout.CENTER); 

所以pEnq奠定了pCustomer!

+0

即使我設置pEnq不可分? – 2011-12-29 14:57:06

+0

是的,沒關係,因爲pCustomer被pEnq取代。也請看Thomas答案,因爲他發佈了一個解決方案。 – bembii 2011-12-29 15:00:19