簡體   English   中英

單擊按鈕時更換面板

[英]Changing panels when a button is clicked

所以這是我的問題:

我想創建一個新面板,要求用戶輸入銀行帳戶信息(帳戶名和帳號),當用戶單擊“登錄”按鈕時,它將面板更改為“提款/存款”面板,並顯示您的帳戶名稱在頂部。 我已經完成了“提款/存款面板”,但是對於如何創建“信息面板”並使之顯示在“提款/存款面板”之前感到困惑。

這是我的代碼:

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類。 然后,您可以以編程方式決定框架中顯示的面板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM