2013-07-27 2 views
1

GUI로 은행 계좌를 만들려고하지만 setText 메소드로 인해 버튼이 작동하지 않습니다. 텍스트 영역에는 아무 것도 나타나지 않습니다.버튼이 작동하지 않음 (GUI)

Account 클래스

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(); 
    } 
} 

그래픽 사용자 인터페이스 방법

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()); 
} 

나는 그것을 해결하기 위해 시도에서 발생,하지만 난 돈 ' 프로그램의 원인을 알지 못한다.

이해하기 쉽게 계정 클래스에 추천을 추가했습니다.

저를 해결책으로 안내해주십시오.

대단히 감사합니다.

+4

:

당신은 같은 것으로 선언을 변경해야합니까? – Mac

+3

AWT와 Swing 구성 요소를 불필요하게 섞지 마십시오. – trashgod

+0

MAC 고맙습니다. "trash"이 문제는 6 번째 업데이트에서 해결되었으므로 어떤 문제없이 혼합 할 수 있습니다. 당신이 다른 것을 의미한다면, 이해할 수 있도록 설명하십시오. – user2626051

답변

2

변수 a이 선언되었지만 초기화되지 않았습니다. 따라서 값은 null입니다. 텍스트를 설정할 때와 마찬가지로 a.toString()으로 전화하면 NullPointerException이 표시됩니다. 당신은`A`를 초기화 한

Account a = new Account(50.0); 
+0

나는 돼지 실수를했다고 생각했습니다 ^^. 고맙습니다. – user2626051

관련 문제