1

プロトタイプをコーディングしていますが、GUI に問題があります。JPanel pCustomerを中央に配置したいのですが、そうすると完全に消えてしまいます。たとえば、SOUTHに置くと、すべて問題ありません。

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

センターに置いても機能します。

4

2 に答える 2

2

BorderLayoutの中央に2つの異なるパネルを追加しようとしました。

最初に追加します

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

そして数行後にあなたはします:

pContent.add(pEnq, BorderLayout.CENTER);

したがって、pEnqはpCustomerの上にあります!

于 2011-12-29T14:47:35.823 に答える
2

ボーダーレイアウトについて読んでください。基本的には 5 つの位置 (NORTH、EAST、SOUTH、WEST、CENTER) があり、それらの位置の 1 つにコンポーネントを配置すると、その位置に既にあるコンポーネントが置き換えられます。

したがってpContent.add(pEnq, BorderLayout.CENTER);、 に置き換えpCustomerられpEnqます。

両方のパネルを中央に配置する場合は、中間パネルを中央に配置してから、他のパネルをそのパネルに追加するか、別のレイアウト マネージャーを使用する必要があります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
于 2011-12-29T14:46:29.590 に答える