JAVA - 向按钮添加动作

时间:2016-11-20 20:05:40

标签: java user-interface button jframe actionlistener

首先,这是一个正在进行的家庭作业项目,旨在征服JAVA(我感觉非常可怕!)。这也是第一周推出图形用户界面,它已成为冒险之旅。由于这个令人敬畏的社区的帮助,我终于能够让GUI看起来像这个例子。但是,我现在已经陷入了一个难题,因为我似乎在添加动作时打破了GUI。

我得到了无数的非法表达"警报。我已经清理了代码,但我可以在某个地方,我错过了一些东西。只需按钮和面板位置,程序就可以编译,如下所示:Correct ATM GUI look.

我知道我已经接近让这个工作正常但是在这一点上我需要一套经验丰富的眼睛告诉我我需要去哪里以及我做错了什么。我发布了下面不正确的ATM.java代码。我还发布了其他三个类(我让它们编译!)按钮应该调用它们来完成它们的操作。

ATM.java这个很大,我应该在某个地方打破这个吗?我正在使用我之前构建并知道的程序作为示例,它只是一个文件。我在想也许

//import statements
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ATM extends JFrame{

Account c,s;
public ATM(Account checking, Account savings){
    initComponents();
    c = checking;
    s = savings;
}

//create varialbes to add to JPanel
private JRadioButton radio1 = new JRadioButton("Checking");
private JRadioButton radio2 = new JRadioButton("Savings");
private JButton withdrawBtn = new JButton("Withdraw");
private JButton depositBtn = new JButton("Deposit");
private JButton transferBtn = new JButton("Transfer");
private JButton balanceBtn = new JButton("Balance");
private JTextField txtAmount = new JTextField();
//end variables declaration

public ATM(){
    super("ATM");
    setLayout(new BorderLayout());

    //add buttons to JPanel
    JPanel buttonsPanel1 = new JPanel();
    buttonsPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
    buttonsPanel1.add(withdrawBtn);
    buttonsPanel1.add(balanceBtn);

    JPanel buttonsPanel2 = new JPanel();
    buttonsPanel2.add(transferBtn);
    buttonsPanel2.add(depositBtn);
    add(buttonsPanel2, BorderLayout.CENTER);

    JPanel buttonsPanel3 = new JPanel();
    buttonsPanel3.setLayout(new BorderLayout());
    buttonsPanel3.add(radio1, BorderLayout.WEST);
    buttonsPanel3.add(radio2, BorderLayout.EAST);
    buttonsPanel3.add(txtAmount, BorderLayout.SOUTH);
    add(buttonsPanel3, BorderLayout.SOUTH);

//creates action listener and assoicates it with Withdraw button
event withdraw = new event();
withdrawBtn.addActionListener(withdraw);
event deposit = new event();
depositBtn.addActionListener(deposit);
event transfer = new event();
transferBtn.addActionListener(transfer);

private class event implements ActionListener{
    public void actionPerformed (ActionEvent withdraw){
        try{
        String amm = txtAmount.getText();
        int withdrawMoney = Integer.parseInt(amm);
        if(withdrawMoney%20 != 0){
            JOptionPane.showMessageDialog(null, "Amount must be in       increments of $20", "Error",0);
        }
        else{
            if(radio2.isSelected()){
                if(c.withdraw(withdrawMoney){
                    JOptionPane.showMessageDialog(null, "Amount Withdrawn Successful", "Message",1);
                }
            }
            else{
                if(s.withdraw(withdrawMoney)){
                    JOptionPane.showMessageDialog(null, "Amount Withdrawn Successful", "Message",1);
                }
            }
        }
        }catch(IllegalArgumentException e){
             JOptionPane.showMessageDialog(null, "Invalid Entry", "Error",0);
            }
        }
}

public class event implements ActionListener{
    public void actionPerformed (ActionEvent deposit){
        try{
        String amm = txtAmount.getText();
        int depositMoney = Integer.parseInt(amm);
        if(radio2.isSelected()){
            s.deposit(depositMoney);
        }else{
            c.deposit(depositMoney);
        }
            JOptionPane.showMessageDialog(null, "Deposit Succesful", "Message",1);
        }catch(IllegalArgumentException e){
             JOptionPane.showMessageDialog(null, "Invalid Entry", "Error",0);
            }
        }
}

public class event implements ActionListener{
    public void actionPerformed (ActionEvent transfer){
        try{
            String amm = txtAmount.getText();
            if(radio2.isSelected()){
                if(s.transfer(transferMoney, c))
                    JOptionPane.showMexxageDialog(null, "Transfer Successful", "Message",1);
            }else{
                if(c.transfer(transferMoney, c))
                    JOptionPane.showMessageDialog(null, "Transer Successful", "Message",1);
            }
        }catch(IllegalArguementException e){
            JOptionPane.showMessageDialog(null, "Invalid Entry", "Error",0);
        }
    }
}

public class event implements ActionListener{
    public void actionPerformed (ActionEvent balance){
        if(radio2.isSelected()){
            JOptionPane.showMessageDialog(null, "Balance is : "+s.balance()+"$","Balance",1);
        }else{
            JOptoinPane.showMessageDialog(null, "Balance is : "+c.balance()+"$","Balance",1);
        }
    }
}
//method will adjust size of the container to contain all controls
pack();
}
}

Main.java

public class Main {

public static void main(String[] args) {

   Account a=new Account(12000,"Checking");
   Account b=new Account(10000,"Saving");

   ATM atm = new ATM(a,b);
   atm.setVisible(true);
}

}

InsufficientFunds.java

public class InsufficientFunds extends Exception{

String msg;

public InsufficientFunds() {
    msg="Insufficient Fund";
}

public String toString()
{
    return msg;
}
}

Account.java

public class Account {

int currentBalance;
String typeOfAccount;

public Account(int currentBalance, String typeOfAccount) {
    this.currentBalance = currentBalance;
    this.typeOfAccount = typeOfAccount;
}

public int getCurrentBalance() {
    return currentBalance;
}

public void setCurrentBalance(int currentBalance) {
    this.currentBalance = currentBalance;
}

public Account() {
}

public boolean withDraw(int ammount)
{
    try{
    if(currentBalance<ammount)
    {
        throw new InsufficientFunds();
    }
    else
    {
        currentBalance=currentBalance-ammount;
    }
    return true;
}catch(InsufficientFunds e)
{
    JOptionPane.showMessageDialog(null,e.toString(),"Error",0);

}
    return false;
}

public int balance()
{
    return currentBalance;
}

public void deposit(int ammount)
{
    currentBalance=currentBalance+ammount;
}

public boolean transfer(int ammount,Account sec)
{
  try
  {if(ammount>currentBalance)
    {
      throw new InsufficientFunds();  
    }
    sec.setCurrentBalance(sec.getCurrentBalance()+ammount);
    currentBalance=currentBalance-ammount;
    return true;
 }catch(InsufficientFunds e)
 {
     JOptionPane.showMessageDialog(null,e.toString(),"Error",0);
 }
  return false;
}
}

0 个答案:

没有答案