gpt4 book ai didi

java - 添加到第三个面板时,我的两个面板连接在一起

转载 作者:行者123 更新时间:2023-12-01 12:48:45 24 4
gpt4 key购买 nike

我试图将两个面板添加到另一个面板上,其中每个面板都有自己的组件,但是当程序执行时,这两个面板只是相互合并。请查看我的代码。

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

我尝试了几种方法,但实际上都不起作用。

***显然我把第一个类粘贴了两次。现在我修好了。对于那个很抱歉。

最佳答案

根据您的示例(并对每个 Pane 的背景颜色进行一些更改),您可以看到它们已添加

Example

但是,这...

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

给我...

Example

关于java - 添加到第三个面板时,我的两个面板连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24421603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com