-1

这是代码,Account 是一些基本类,没有做任何特别的事情。我已经尽一切努力让这个动作监听器工作,我知道我错过了一些愚蠢的东西。我在 jigloo 中制作了 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()));
        }

    }
}

}
4

1 回答 1

0

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

jButton1.addActionListener(this);

在完全不同的情况下,考虑使用布局管理器来管理组件的定位和大小。

于 2013-04-25T13:11:43.000 回答