我對這些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;
}
}
爲了更好地幫助越早,張貼[MCVE(http://stackoverflow.com/help/mcve)(最小完備的可驗證的例子)。 –
*「JButton在單擊時不會計算」*您在該源中聲明的3的哪個「JButton」是您指的?不,在第二個想法中,只用一個按鈕發佈一個MCVE。 –