2014-05-23 65 views
0

我對這些GUI相關領域有點新手。我的問題是當我點擊下面的代碼中提到的JButton時,它不計算。我嘗試了幾種方法,但沒有任何運氣。JButton在點擊時不會計算

任何人都可以對此有所瞭解嗎?

這是驅動程序:

package project; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.TitledBorder; 

public class Driver extends JFrame{ 
    private JTextField jtfDeposit = new JTextField("0.00"); 
    private JTextField jtfWithdraw = new JTextField("0.00"); 
    private JTextField jtfTransfer = new JTextField("0.00"); 
    private JTextField jtfCBalance = new JTextField("0.00"); 
    private JTextField jtfSBalance = new JTextField("0.00"); 

    private JButton jbtDeposit = new JButton("Deposit"); 
    private JButton jbtWithdraw = new JButton("Withdraw"); 
    private JButton jbtTransfer = new JButton("Transfer"); 

    String[] depositStrings = { "Checking", "Savings"}; 
    String[] withdrawStrings = { "Checking", "Savings"}; 
    String[] transferStrings = { "CHK* to SAVA*", "SAVA* to CHK*"}; 

    public Driver(){ 
     JPanel p1 = new JPanel(new GridLayout(3,4)); 
     p1.add(new JLabel("Deposit Into: ")); 
     p1.add(new JComboBox(depositStrings)); 
     p1.add(jtfDeposit); 
     p1.add(jbtDeposit); 

     p1.add(new JLabel("Withdraw From: ")); 
     p1.add(new JComboBox(withdrawStrings)); 
     p1.add(jtfWithdraw); 
     p1.add(jbtWithdraw); 

     p1.add(new JLabel("Transfer From: ")); 
     p1.add(new JComboBox(transferStrings)); 
     p1.add(jtfTransfer); 
     p1.add(jbtTransfer); 

     p1.setBorder(new TitledBorder("WELCOME TO THE BANK OF IS247")); 

     JPanel p2 = new JPanel(new GridLayout(2,2)); 
     p2.add(new JLabel("Checking : ")); 
     p2.add(jtfCBalance); 
     p2.add(new JLabel("Savings : ")); 
     p2.add(jtfSBalance); 

     p2.setBorder(new TitledBorder("YOUR BALANCES")); 

     add(p1, BorderLayout.CENTER); 
     add(p2, BorderLayout.SOUTH); 

     jbtDeposit.addActionListener(new ButtonListener()); 
     jbtWithdraw.addActionListener(new ButtonListener()); 
     jbtTransfer.addActionListener(new ButtonListener()); 
    } 
    class ButtonListener implements ActionListener{ 
     Account cAcc = new CheckingAccount(); 
     Account sAcc = new SavingAccount(); 

     @Override 
     public void actionPerformed(ActionEvent e){ 
      String actionCommand = e.getActionCommand(); 
      switch (actionCommand){ 
       case "Deposit": 
        if("Deposit".equals(depositStrings[0])){ 
         function(1); 
         break; 
        }else if("Deposit".equals(depositStrings[1])){ 
         function(2); 
         break; 
        } 
       case "Withdraw": 
        if("Withdraw".equals(withdrawStrings[0])){ 
         function(3); 
         break; 
        }else if("Withdraw".equals(withdrawStrings[1])){ 
         function(4); 
         break; 
        } 
       case "Transfer": 
        if("Transfer".equals(transferStrings[0])){ 
         function(5); 
         break; 
        }else if("Transfer".equals(transferStrings[1])){ 
         function(6); 
        } 
      } 
     } 
     private void function(int num){ 
      double dAmount = new Double(jtfDeposit.getText().trim()).doubleValue(); 
      double wAmount = new Double(jtfWithdraw.getText().trim()).doubleValue(); 
      double tAmount = new Double(jtfTransfer.getText().trim()).doubleValue(); 
      double cAmount = cAcc.getBalance(); 
      double sAmount = sAcc.getBalance(); 

      switch (num){ 
       case 1: 
        cAmount = cAcc.getDeposit(dAmount); 
        break; 
       case 2: 
        sAmount = sAcc.getDeposit(dAmount); 
        break; 
       case 3: 
        cAmount = cAcc.getWithdrawal(wAmount); 
        break; 
       case 4: 
        sAmount = sAcc.getWithdrawal(wAmount); 
        break; 
       case 5: 
        cAmount = cAcc.getTransfer(tAmount); 
        sAmount = sAcc.getDeposit(tAmount); 
        break; 
       case 6: 
        sAmount = sAcc.getTransfer(tAmount); 
        cAmount = cAcc.getDeposit(tAmount); 
        break; 
      } 
      jtfCBalance.setText(String.valueOf(cAmount)); 
      jtfSBalance.setText(String.valueOf(sAmount)); 
     } 
    } 
    public static void main(String[] args){ 
     Driver frame = new Driver(); 
     frame.setTitle("Bank of IS247"); 
     frame.setSize(500,200); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

這是父類:

package project; 

public abstract class Account{ 
    public abstract double getBalance(); 
    public abstract double getDeposit(double amount); 
    public abstract double getWithdrawal(double amount); 
    public abstract double getTransfer(double amount); 
} 

子類:

package project; 

public class CheckingAccount extends Account{ 
    double balance = 1000.00; 
    double deposit; 
    double withdrawal; 
    double transfer; 

    public CheckingAccount(){ 
     //no-arg constructor 
    } 
    public CheckingAccount(double balance, double deposit, double withdrawal, 
      double transfer){ 
     this.balance = balance; 
     this.deposit = deposit; 
     this.withdrawal = withdrawal; 
     this.transfer = transfer; 
    } 
    @Override 
    public double getBalance(){ 
     return balance; 
    } 
    @Override 
    public double getDeposit(double deposit){ 
     balance += deposit; 
     return balance; 
    } 
    @Override 
    public double getWithdrawal(double withdrawal){ 
     balance -= withdrawal; 
     return balance; 
    } 
    @Override 
    public double getTransfer(double transfer){ 
     balance = getWithdrawal(transfer); 
     return balance; 
    } 
} 

子類:

package project; 

public class SavingAccount extends Account{ 
    double balance = 4500.00; 
    double deposit; 
    double withdrawal; 
    double transfer; 

    public SavingAccount(){ 
     //no-arg constructor 
    } 
    public SavingAccount(double balance, double deposit, double withdrawal, 
      double transfer){ 
     this.balance = balance; 
     this.deposit = deposit; 
     this.withdrawal = withdrawal; 
     this.transfer = transfer; 
    } 
    @Override 
    public double getBalance(){ 
     return balance; 
    } 
    @Override 
    public double getDeposit(double deposit){ 
     balance += deposit; 
     return balance; 
    } 
    @Override 
    public double getWithdrawal(double withdrawal){ 
     balance -= withdrawal; 
     return balance; 
    } 
    @Override 
    public double getTransfer(double transfer){ 
     balance = getWithdrawal(transfer); 
     return balance; 
    } 
} 
+1

爲了更好地幫助越早,張貼[MCVE(http://stackoverflow.com/help/mcve)(最小完備的可驗證的例子)。 –

+2

*「JButton在單擊時不會計算」*您在該源中聲明的3的哪個「JButton」是您指的?不,在第二個想法中,只用一個按鈕發佈一個MCVE。 –

回答

1

有幾個問題。

  1. 我添加了一個jtfCombo成員變量。您需要此參考,以便您可以訪問組合框中的值(檢查,儲蓄)。
  2. 在actionPerformed()中,您應該使用jtfCombo來獲取selectedItem。然後您可以決定要執行哪個功能。
  3. 你需要休息;聲明作爲你的每個案例的最後一個陳述。

這是更新的代碼。我只對存款按鈕進行了更改。我沒有檢查過你的計算結果,但現在正在更新。查找/ *新* /爲增加/修改:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.TitledBorder; 

public class Driver extends JFrame{ 
    private JTextField jtfDeposit = new JTextField("0.00"); 
    private JTextField jtfWithdraw = new JTextField("0.00"); 
    private JTextField jtfTransfer = new JTextField("0.00"); 
    private JTextField jtfCBalance = new JTextField("0.00"); 
    private JTextField jtfSBalance = new JTextField("0.00"); 
    private JComboBox jtfCombo = null; /* NEW */ 

    private JButton jbtDeposit = new JButton("Deposit"); 
    private JButton jbtWithdraw = new JButton("Withdraw"); 
    private JButton jbtTransfer = new JButton("Transfer"); 

    String[] depositStrings = { "Checking", "Savings"}; 
    String[] withdrawStrings = { "Checking", "Savings"}; 
    String[] transferStrings = { "CHK* to SAVA*", "SAVA* to CHK*"}; 

    public Driver(){ 
     JPanel p1 = new JPanel(new GridLayout(3,4)); 
     p1.add(new JLabel("Deposit Into: ")); 

     jtfCombo = new JComboBox(depositStrings); /* NEW */ 
     p1.add(jtfCombo); /* NEW */ 
     p1.add(jtfDeposit); 
     p1.add(jbtDeposit); 

     p1.add(new JLabel("Withdraw From: ")); 
     p1.add(new JComboBox(withdrawStrings)); 
     p1.add(jtfWithdraw); 
     p1.add(jbtWithdraw); 

     p1.add(new JLabel("Transfer From: ")); 
     p1.add(new JComboBox(transferStrings)); 
     p1.add(jtfTransfer); 
     p1.add(jbtTransfer); 

     p1.setBorder(new TitledBorder("WELCOME TO THE BANK OF IS247")); 

     JPanel p2 = new JPanel(new GridLayout(2,2)); 
     p2.add(new JLabel("Checking : ")); 
     p2.add(jtfCBalance); 
     p2.add(new JLabel("Savings : ")); 
     p2.add(jtfSBalance); 

     p2.setBorder(new TitledBorder("YOUR BALANCES")); 

     add(p1, BorderLayout.CENTER); 
     add(p2, BorderLayout.SOUTH); 

     jbtDeposit.addActionListener(new ButtonListener()); 
     jbtWithdraw.addActionListener(new ButtonListener()); 
     jbtTransfer.addActionListener(new ButtonListener()); 
    } 
    class ButtonListener implements ActionListener{ 
     Account cAcc = new CheckingAccount(); 
     Account sAcc = new SavingAccount(); 

     @Override 
     public void actionPerformed(ActionEvent e){ 
      String actionCommand = e.getActionCommand(); 
      switch (actionCommand){ 
      case "Deposit": 
       if(jtfCombo.getSelectedItem().equals(depositStrings[0])){ /* NEW */ 
        System.out.println("depositing to checking..."); /* NEW */ 
        function(1); 
        break; 
       }else if(jtfCombo.getSelectedItem().equals(depositStrings[1])){ 
        System.out.println("depositing to savings..."); /* NEW */ 
        function(2); 
        break; 
       } 
       break; /* NEW */ 
      case "Withdraw": 
       if("Withdraw".equals(withdrawStrings[0])){ 
        function(3); 
        break; 
       }else if("Withdraw".equals(withdrawStrings[1])){ 
        function(4); 
        break; 
       } 
       break; /* NEW */ 
      case "Transfer": 
       if("Transfer".equals(transferStrings[0])){ 
        function(5); 
        break; 
       }else if("Transfer".equals(transferStrings[1])){ 
        function(6); 
       } 
       break; /* NEW */ 
      } 
     } 
     private void function(int num){ 
      double dAmount = new Double(jtfDeposit.getText().trim()).doubleValue(); 
      double wAmount = new Double(jtfWithdraw.getText().trim()).doubleValue(); 
      double tAmount = new Double(jtfTransfer.getText().trim()).doubleValue(); 
      double cAmount = cAcc.getBalance(); 
      double sAmount = sAcc.getBalance(); 

      switch (num){ 
      case 1: 
       cAmount = cAcc.getDeposit(dAmount); 
       break; 
      case 2: 
       sAmount = sAcc.getDeposit(dAmount); 
       break; 
      case 3: 
       cAmount = cAcc.getWithdrawal(wAmount); 
       break; 
      case 4: 
       sAmount = sAcc.getWithdrawal(wAmount); 
       break; 
      case 5: 
       cAmount = cAcc.getTransfer(tAmount); 
       sAmount = sAcc.getDeposit(tAmount); 
       break; 
      case 6: 
       sAmount = sAcc.getTransfer(tAmount); 
       cAmount = cAcc.getDeposit(tAmount); 
       break; 
      } 
      jtfCBalance.setText(String.valueOf(cAmount)); 
      jtfSBalance.setText(String.valueOf(sAmount)); 
     } 
    } 
    public static void main(String[] args){ 
     Driver frame = new Driver(); 
     frame.setTitle("Bank of IS247"); 
     frame.setSize(500,200); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 
1

你有一個邏輯錯誤,你設置depositStrings{"Checking", "Savings"},他們從來沒有改變,但你檢查,看看如果這些String的比賽Deposit的...

if ("Deposit".equals(depositStrings[0])) { 
    function(1); 
    break; 
} else if ("Deposit".equals(depositStrings[1])) { 
    function(2); 
    break; 
} 

我以爲他們假設匹配某種「賬戶」。我也這麼認爲這是其他情況下的問題...