我试图将两个面板添加到另一个面板中,每个面板都有自己的组件,但是当程序执行两个面板时,它们彼此融合。请检查我的代码。

import javax.swing.*;

import java.awt.*;

/*
 * 2 Labels for withdraw/deposit.
* 2 Textfields for entering withdrawal/deposit amount.
* 1 Label for balance, 1 non-editable textfield for balance.
* Event listener on the calculate button.
* Action performed: balance minors withdrawal plus deposit
*/

/ **
* This class manages the input numbers. User will input withdrawal/deposit amount
* and the program will display the balacne after transfers.
* @author Administrator
*
*/

public class DataPanel extends JPanel {

public final int TEXTFIELD_LENGTH = 10;
public final int LAYOUT_VGAP = 25;
public final int LAYOUT_HGAP = 15;


/**
 * Constructor sets layout, creates labels/fields.
 */

public JPanel pane1 = new JPanel();
public JPanel pane2 = new JPanel();
public JPanel pane = new JPanel(new GridLayout(1,2));

    //Create necessary fields and labels
    JLabel withdrawalLabel = new JLabel("Withdrawal: ");
    JLabel depositLabel = new JLabel("deposit: ");
    JTextField withdrawalText = new JTextField(TEXTFIELD_LENGTH);
    JTextField depositText = new JTextField(TEXTFIELD_LENGTH);

    JLabel balanceLabel = new JLabel("Balance: ");
    JTextField balanceTextField = new JTextField(TEXTFIELD_LENGTH);

    JLabel totalSavingLabel = new JLabel("Recently You Saved: ");
    JTextField totalSavingTextField = new JTextField(TEXTFIELD_LENGTH);

    JLabel totalSpentLabel = new JLabel("Recently You Spent: ");
    JTextField totalSpentTextField = new JTextField(TEXTFIELD_LENGTH);

    //set some textfields non editable.


    //set layout, 3 rows + 2 columns.

    public DataPanel()
    {
        setLayout(new GridLayout(2,1));

        buildPane1();

        buildPane2();

        buildPane();

        add(pane);


    //add components to panel DataPanel.

}

    public void buildPane()
    {
        pane.add(pane1);
        pane.add(pane2);
    }

    public void buildPane1()
    {
        setLayout(new GridLayout(3,2));

        setBorder(BorderFactory.createLineBorder(Color.PINK, 2, true));

        balanceTextField.setEditable(false);

        pane1.add(withdrawalLabel);
        pane1.add(withdrawalText);
        pane1.add(depositLabel);
        pane1.add(depositText);
        pane1.add(balanceLabel);
        pane1.add(balanceTextField);
    }

    public void buildPane2()
    {
        setLayout(new GridLayout(2,2));

        setBorder(BorderFactory.createLineBorder(Color.black, 2, true));

        totalSavingTextField.setEditable(false);
        totalSpentTextField.setEditable(false);

        pane2.add(totalSavingLabel);
        pane2.add(totalSavingTextField);
        pane2.add(totalSpentLabel);
        pane2.add(totalSpentTextField);

    }
}


JFrame类:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


public class MainFrame extends JFrame {
public final int WIDTH = 1000;
public final int HEIGHT = 800;

public DataPanel dataPane;
public InfoPanel infoPane;
public Menu menu;

public JTabbedPane tabbedPane = new JTabbedPane();

//tab #1
public JPanel spendingPane;

//tab #2
public JPanel personalPane;

//tab #3
public JPanel socialPane;

//tab #4
public JPanel chatPane;



/**
 * Constructor.
 */
public MainFrame()
{
    setTitle("Personal Banking");
    setSize(WIDTH, HEIGHT);
    setLayout(new BorderLayout());

    buildMenu();
    buildPanel();

    add(tabbedPane);

    pack();
    setVisible(true);
}

/**
 * This methods builds all the components.
 */

private void buildPanel()
{
    //build main panel (Spending  panel).
    dataPane = new DataPanel();



    //add numbers panel to the tabbed panel.
    tabbedPane.addTab("Numbers", dataPane);

    //build personal information panel.
    infoPane = new InfoPanel();
    personalPane = new JPanel();
    personalPane.add(infoPane);

    //add personal information panel to the tabbed panel.
    tabbedPane.addTab("Personal", personalPane);


}

/**
 * This function builds menu bar and menu items.
 */
private void buildMenu()
{
    menu = new Menu();
    setJMenuBar(menu.mainMenuBar);
}



public static void main(String args[])
{
    new MainFrame();
}
}


我尝试了几种方法,但是它们实际上没有用。

***好吧,我显然两次粘贴了第一堂课。现在,我修复了它。对于那个很抱歉。

最佳答案

根据您的示例(每个窗格的背景色稍有变化),您可以看到已添加了它们



但是这个

public void buildPane1()
{
    setLayout(new GridLayout(3,2));

    setBorder(BorderFactory.createLineBorder(Color.PINK, 2, true));


和这个...

public void buildPane2()
{
    setLayout(new GridLayout(2,2));

    setBorder(BorderFactory.createLineBorder(Color.black, 2, true));


看起来非常可疑,因为它们被应用于DataPanel而不是(正如我怀疑的那样)应用于各个面板...

因此,将它们更改为更像...

public void buildPane1() {

    pane1.setLayout(new GridLayout(3, 2));
    pane1.setBorder(BorderFactory.createLineBorder(Color.PINK, 2, true));

//...

public void buildPane2() {
    pane2.setLayout(new GridLayout(2, 2));
    pane2.setBorder(BorderFactory.createLineBorder(Color.black, 2, true));


给我...

07-25 21:03