2013-07-24 77 views
1

時,因此,這裏是我的問題:改變面板的按鈕被點擊

我想創建要求用戶輸入銀行賬戶信息(開戶名稱和賬號)的新面板和用戶點擊時在登錄按鈕上,它將面板更改爲提取/存款面板,並在頂部顯示您的帳戶名稱。我已經抽取/存款面板全部完成,但我難倒如何創建信息面板,並使它看起來抽取/存款面板之前,等

這裏是我的代碼:

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)); 
      } 
     } 
    } 
} 
+1

請考慮使用[CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html),如[示例]中所示(http://stackoverflow.com/a/ 9349137/1057230) –

回答

1

我的建議是:不要在JFrame構造函數中創建面板。創建一個InfoPanel類和一個WithdrawPanel類。然後,您可以通過編程方式決定在您的框架中顯示哪個面板。

+0

包括我不會基本上重做整個事情嗎? –

+0

我喜歡這種方式,涉及__分離的關注點___ +1 :-) –

+0

@vVvSintherius它包括必須做的整件事情,但它會更容易閱讀和維護。另外,從舊類到新類的複製/粘貼都非常多。 –