gpt4 book ai didi

java - 单击按钮时更改面板

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:16 25 4
gpt4 key购买 nike

这是我的问题:

我想创建一个新面板,要求用户输入银行帐户信息(帐户名称和帐号),当用户单击“登录”按钮时,它将面板更改为“取款/存款面板”,并在顶部显示您的帐户名称。我已经完成了提款/存款面板,但我对如何创建信息面板并使其出现在提款/存款面板之前等感到困惑。

这是我的代码:

public class MyFrame extends JFrame {

private JPanel panel;
private JLabel wordsLabel;
private JLabel balanceLabel;
private JLabel choiceLabel;
private JTextField transactionAmount;
private JRadioButton depositButton;
private JRadioButton withdrawButton;
private double balance;

public MyFrame() {
final int FIELD_WIDTH = 5;
balance = 500;
panel = new JPanel();
wordsLabel = new JLabel();
balanceLabel = new JLabel();
choiceLabel = new JLabel();
transactionAmount = new JTextField(FIELD_WIDTH);
JPanel buttonPanel = new JPanel();
ButtonGroup myGroup = new ButtonGroup();
depositButton = new JRadioButton("Deposit");
withdrawButton = new JRadioButton("Withdraw");
transactionAmount.setText("0");
wordsLabel.setText("Welcome to Wes Banco! Your current balance is: ");
balanceLabel.setText(String.format("%10.2f", balance));
choiceLabel.setText("How much would you like to deposit/withdraw? ");
panel.setLayout(new GridLayout(4, 4, 5, 10));
panel.add(wordsLabel);
panel.add(balanceLabel);
panel.add(choiceLabel);
panel.add(transactionAmount);
myGroup.add(depositButton);
myGroup.add(withdrawButton);
buttonPanel.add(depositButton);
buttonPanel.add(withdrawButton);
ButtonListener myListener = new ButtonListener();
depositButton.addActionListener(myListener);
withdrawButton.addActionListener(myListener);
panel.add(buttonPanel);
this.add(panel);
}

class ButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent event) {
double amount = Double.parseDouble(transactionAmount.getText());
if (amount == 0) {
JOptionPane.showMessageDialog(null,
"You cannot deposit or withdraw nothing!");
JOptionPane.showMessageDialog(null,
"Please enter a valid amount.");
} else {
if (event.getSource() == depositButton) {
JOptionPane.showMessageDialog(null,
"You have deposited: " + amount);
balance += amount;
} else if (event.getSource() == withdrawButton) {
if (balance < amount) {
JOptionPane.showMessageDialog(null,
"You do not have sufficient funds to complete this transaction.");
JOptionPane.showMessageDialog(null,
"Please enter a valid amount.");
} else {
JOptionPane.showMessageDialog(null,
"You have withdrawn: " + amount);
balance -= amount;
}
}
balanceLabel.setText(String.valueOf(balance));
}
}
}
}

最佳答案

我的建议是:不要在 JFrame 构造函数中创建面板。创建一个 InfoPanel 类和一个 WithdrawPanel 类。然后您可以通过编程方式决定在框架中显示哪个面板。

关于java - 单击按钮时更改面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17826905/

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