该按钮不起作用(GUI)

时间:2013-07-27 17:49:30

标签: java swing settext

我正在尝试将银行帐户设置为GUI,但由于setText方法,按钮不起作用。 textarea中没有任何内容。

帐户类

public class Account {
    //attribute for balance amount 
    private double balance;

    // constructor to inital the  balance attribute
    public Account(double nitialBalance){

    if(nitialBalance > 0.0)
        balance=nitialBalance;
    }

    // to add money method
    public void set_add_Balance(Double balance1){
        balance+=balance1;
    }        

   //  the withdrawn amount from the account
   public void depit(double debit){
        double f=0;        
        balance=balance-debit;

       if (balance <= 0){
           f=debit;
           System.out.println("f=debit");
       }
       if(balance <= 0){
           balance = balance + f;
           System.out.println("Debit amount exceeded account balance ");
       }
   }

   // to cheack the amount you have  
   public double getBalance( ){
        return balance;
   }

   // tostring for get balance
   public String toString(){
       return "Your Balance is : "+getBalance();
   }
}

GUI方法

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

public class GUI extends JFrame {
    Account a;

    JButton j1=new JButton("Your Balance");
    JButton j2=new JButton("Add Money");
    JButton j3=new JButton("Withdrow Money");
    JButton j4=new JButton("Exit");

    TextArea t1=new TextArea("");
    Container cont = getContentPane();

    public GUI(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          
        setTitle("Bank Account");
        setSize(500,300);

        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2,2));
        p.add(j1);  p.add(j3);
        p.add(j2);  p.add(j4);

        cont.add(p,"South");
        cont.add(t1,"Center");

        j1.addActionListener(new buttons());
        j2.addActionListener(new buttons());
        j3.addActionListener(new buttons());
        j4.addActionListener(new buttons());
    }

    private class buttons implements ActionListener {        
        public void actionPerformed (ActionEvent e){        
            Object c =e.getSource();

            if(c==j1) {
                t1.setText( "Your Balance is: "+a.toString());
            }        
            if(c==j2) {
                a.set_add_Balance(50.0);
                t1.setText( "Your Balance is: "+a.toString());
            }       
            if(c==j4){        
                System.exit(0);
            }
        }// end of actionPerformed        
    }

    public static void main(String[] args) {
        GUI j=new GUI();
        j.setVisible(true);
    }        
}

按钮1和2中出现问题

if(c==j1) {
    t1.setText( "Your Balance is: "+a.toString());
}    
if(c==j2){    
    a.set_add_Balance(50.0);
    t1.setText( "Your Balance is: "+a.toString());
}

我试图修复它,但我不知道程序的原因

我在帐户类中添加了一个表达,以便于理解。

请引导我解决问题。

非常感谢你。

1 个答案:

答案 0 :(得分:2)

您的变量a已声明,但从未初始化。因此它具有值null。如果您随后拨打a.toString(),就像设置文字一样,您会收到NullPointerException

您应该将声明更改为:

Account a = new Account(50.0);