无法使ActionListener正常工作

时间:2013-04-25 10:18:37

标签: java swing user-interface jframe actionlistener

这是代码,Account是一些不做任何特殊操作的基本类。我已经尝试了一切让这个动作听众工作,我知道我错过了一些愚蠢的事情。我在jigloo制作了GUI,我认为我的问题在于实际的主要问题?我需要添加一些东西吗?或者我错了。从来没有做过Gui的东西。这是我参加的第一个编程课,教授似乎认为我应该能够毫无问题地做到这一点。有任何想法吗?

import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


import java.awt.event.ActionListener;




@SuppressWarnings("serial")
public class Button extends javax.swing.JFrame implements ActionListener
{
    public JButton LoginButton;
    public JTextField bankinfoButton;
    public JButton transgerButton;
    public JButton jButton1;
    public JTextField moneytextfield;
    public JTextField sendto;
    public JEditorPane account1;
    public JTextField accountpw;
    public JTextField accountname;
    public JButton BalanceButton;
    public JButton WithdrawButton;
    public JButton DepositButton;
    public JButton LogoutButton;
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


    public Account theAc = new Account();
    public Account[] accountList = new Account[10]; 
    public String tempAccountName;
    public String tempAccountPw;
    public int tempIndex;
    public double tempBalance;
    public Transfer theT = new Transfer();
    public Transfer[] transferList = new Transfer[10];



    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Button inst = new Button();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public Button() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            {
                LoginButton = new JButton();
                getContentPane().add(LoginButton);
                LoginButton.setText("Login");
                LoginButton.setBounds(12, 12, 120, 44);
            }
            {
                LogoutButton = new JButton();
                getContentPane().add(LogoutButton);
                LogoutButton.setText("Log Out");
                LogoutButton.setBounds(137, 12, 120, 44);
            }
            {
                bankinfoButton = new JTextField();
                getContentPane().add(bankinfoButton);
                bankinfoButton.setText("Displays bank account information");
                bankinfoButton.setBounds(336, 215, 277, 189);
            }
            {
                DepositButton = new JButton();
                getContentPane().add(DepositButton);
                DepositButton.setText("Deposit");
                DepositButton.setBounds(12, 150, 120, 50);
            }
            {
                WithdrawButton = new JButton();
                getContentPane().add(WithdrawButton);
                WithdrawButton.setText("Withdraw");
                WithdrawButton.setBounds(151, 150, 120, 50);
            }
            {
                BalanceButton = new JButton();
                getContentPane().add(BalanceButton);
                BalanceButton.setText("Balance");
                BalanceButton.setBounds(12, 223, 120, 52);
            }
            {
                transgerButton = new JButton();
                getContentPane().add(transgerButton);
                transgerButton.setText("Transfer");
                transgerButton.setBounds(12, 298, 120, 52);
            }
            {
                accountname = new JTextField();
                getContentPane().add(accountname);
                accountname.setText("Please Enter account name");
                accountname.setBounds(12, 76, 193, 23);
            }
            {
                accountpw = new JTextField();
                getContentPane().add(accountpw);
                accountpw.setText("Please Enter Account Password");
                accountpw.setBounds(12, 115, 193, 23);
            }
            {
                account1 = new JEditorPane();
                getContentPane().add(account1);
                account1.setText("Please enter account you would like to transfer funds to");
                account1.setBounds(0, 370, 313, 34);
            }
            {
                moneytextfield = new JTextField();
                getContentPane().add(moneytextfield);
                moneytextfield.setText("Enter cash here");
                moneytextfield.setBounds(163, 258, 99, 23);
            }
            {
                jButton1 = new JButton();
                getContentPane().add(jButton1);
                jButton1.setText("Create Account");
                jButton1.setBounds(268, 12, 130, 44);
            }
            pack();
            this.setSize(651, 460);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }



    }


    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == jButton1)
         {
             bankinfoButton.setText("Hello");
         }
        if(e.getActionCommand().equals("Create Account")) 
        {
            if(theAc==null)
            {
                bankinfoButton.setText("You must first Log in");
            }


            if(accountpw.getText() != null)
            {
            theAc.setPassword(accountpw.getText());
            bankinfoButton.setText("New Account Created!");
            accountList[Account.getNumAccount()-1] = theAc;

            bankinfoButton.setText("Account Created!");
            bankinfoButton.setText(theAc.toString());
            }
            else
            {
            bankinfoButton.setText("You must enter a password to create an account.");
            }


    }

        if(e.getActionCommand().equals("Print Account List")){
            for(int i = 0; i <accountList.length;i++)
            {

                bankinfoButton.setText(accountList[i].toString());
            }
        }
        if(e.getActionCommand().equals("Deposit")) 
        {
            if(theAc==null)
            {
                bankinfoButton.setText("You must first Log in");
            }
            theAc.deposit(Double.parseDouble(moneytextfield.getText()));
            bankinfoButton.setText("Your new balance is: " + String.valueOf(theAc.getBalance()));
        }
        if(e.getActionCommand().equals("Withdrawl")) 
        {
            if(theAc==null)
            {
                bankinfoButton.setText("You must first Log in");
            }

            //theAc.withdraw(Double.parseDouble(moneytextfield.getText()));
            bankinfoButton.setText("Your new balance is: " + String.valueOf(theAc.getBalance()));


    }
        if(e.getActionCommand().equals("Balance")) 
        {
            if(theAc==null)
            {
                bankinfoButton.setText("You must first Log in");
            }

            bankinfoButton.setText("Your balance is: " + String.valueOf(theAc.getBalance()));

    }
        if(e.getActionCommand().equals("Login")) 
        {
            if(theAc==null)
            {
                bankinfoButton.setText("You must first Log in");
            }
            for(int i = 0;i<10;i++)
            {
                if(accountname.getText().equals(accountList[i].getName()))
                        {
                            tempAccountName = accountList[i].getName();

                        }
            for(int n = 0;i<10;i++)
            {

                if(accountpw.getText().equals(accountList[i].getPassword()))
                {
                    tempAccountPw = accountList[i].getPassword();
                    if(i==n)
                     n= tempIndex;
                    else
                    {
                        bankinfoButton.setText("Invalide username or password");
                    }
                }

            }
            theAc = accountList[tempIndex];
            }
        if(e.getActionCommand().equals("Log Out"))
        {
            if(theAc==null)
            {
                bankinfoButton.setText("You must first Log in");
            }
            theAc = null;
            bankinfoButton.setText("You have logged off");

        }
        if(e.getActionCommand().equals("Transfer"))
        {
            theT = transferList[tempIndex];
            theT.setHistory(accountname.getText(), sendto.getText(), Double.parseDouble(moneytextfield.getText()));
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

您永远不会在ActionListener注册任何组件:

jButton1.addActionListener(this);

完全不同的注意事项考虑使用layout manager来管理定位&amp;组件的大小。