激活ActionPerformed时出错

时间:2013-08-14 02:41:32

标签: java swing runtime-error

我有JPanel的按钮,按下时应该将我发送到另一个屏幕,但是我一直收到错误。我试过谷歌搜索它,但没有任何关于它的信息。

完整代码

import javax.swing.JFrame;
import javax.swing.JTextField; 
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import javax.swing.ButtonGroup;//to put radio buttons together

public class Graphics extends JFrame {//this class is to display the GUI on the screen

private JButton enter;//enter on login
private JButton exit; //exit program
private JButton button1; //enter on menu
private JButton button2; //enter on withdraw
private JButton button3;  //enter on deposit
private JButton back;//button to return to main menu from other options

private JRadioButton balance; //menu for operation selection
private JRadioButton withdraw;
private JRadioButton deposit;
private ButtonGroup buttonMenu;//to put radio buttons together

private JTextField loginBox; //to enter username
private JPasswordField password; 
private JTextField input1;//to enter withdraw amount
private JTextField input2;//deposit amount

private JPanel loginPanel;//first screen
private JPanel menuPanel;
private JPanel withdrawPanel;
private JPanel depositPanel;
private JPanel balancePanel;
private JPanel setPanel; //to hold the other panels
private CardLayout cl;//to set the different panels

private int login1;
private int password1;
private double login2;
private double password2;


ATM atmObject = new ATM();

public Graphics(){ //constructor

super("My ATM");

//ATM object = new ATM();//object to call methods in ATM
                       //to access information stored

CardLayout cl = new CardLayout();
setPanel =  new JPanel();//panel to store the other panels
setPanel.setLayout(cl);//create the different set of windows

/***define and initialize all buttons, labels etc.***/
JLabel login = new JLabel("Login"); //to put next to login field
JLabel passcode = new JLabel("Password");   

loginBox = new JTextField(6);//to enter login
password = new JPasswordField(6);//to enter password
input1 = new JTextField(6);
input2 = new JTextField(6);

enter = new JButton("Enter");
back = new JButton("GO Back");

exit = new JButton("Exit");
button1 = new JButton("Enter");//button for main menu
button2 = new JButton("Enter");//button for withdraw
button3 = new JButton("Enter");//button for deposit

balance =  new JRadioButton("balance inquiry", false);//menu for operation selection
withdraw = new JRadioButton("withdraw from account", false);
deposit =  new JRadioButton("deposit to account", false);
/***end of definitions***/

/*****set-up login screen****/
loginPanel = new JPanel();

loginPanel.add(login);//label
loginPanel.add(loginBox);//textbox
loginPanel.add(passcode);//label
loginPanel.add(password);//input password
loginPanel.add(enter);

loginPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(loginPanel, "LoginScreen");//add login screen to panel and set 
                                          //items orientation to the left
//here add code to check password on file
//obtain password as set of characters
//int cool = Integer.parseInt(loginBox.getText()); in a single step
//String inputA = loginBox.getText();//obtain input from user as a string
//JTextField inputB = new JTextField();

//login1 = Integer.parseInt(inputA); //convert it to int so it can be used

//char[] passcode2 = password.getPassword(); //create array of chars to get password
//password1 = Integer.parseInt(String.valueOf(passcode2));//changing characters to int

/****setup main menu***/
menuPanel = new JPanel();
buttonMenu = new ButtonGroup();
menuPanel.add(balance);
menuPanel.add(withdraw);
menuPanel.add(deposit);
//make all radio buttons connected
buttonMenu.add(balance);
buttonMenu.add(withdraw);
buttonMenu.add(deposit);

menuPanel.add(button1);//add entebr button
menuPanel.add(exit);

menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(menuPanel, "MainMenu");//add window to display

/***setup balance inquiry screen***/

balancePanel = new JPanel();
JLabel balance = new JLabel("the balance in your account is :");
JTextField balanceDisplay = new JTextField(5);//to display balance
balanceDisplay.setEditable(false);

//here code to call balance from ATM
balancePanel.add(balance);
balancePanel.add(balanceDisplay);
balancePanel.add(back);
balancePanel.add(exit);

balancePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(balancePanel, "Blance");//add window to display
/**end of balance inquiry**/

/****setup screen to withdraw money***/
withdrawPanel = new JPanel();
JLabel message = new JLabel("enter amount to withdraw");
withdrawPanel.add(message);//display message
withdrawPanel.add(input1);
withdrawPanel.add(button2);
withdrawPanel.add(back);
withdrawPanel.add(exit);

withdrawPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
//here code to call method to withdraw money
setPanel.add(withdrawPanel);//add window to display

/***setup screen to deposit***/

depositPanel = new JPanel();
JLabel message2 = new JLabel("Enter amount to deposit");
depositPanel.add(message2);
depositPanel.add(input2);
depositPanel.add(back);
depositPanel.add(exit);

depositPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(depositPanel, "Deposit");
/***end deposit panel***/


getContentPane().add(setPanel);

//create event handler and make buttons events
theHandler handler = new theHandler();//builds an action listener object
enter.addActionListener(handler);
exit.addActionListener(handler);
back.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);

//add the frame to the window


//boolean access = atmObject.Info(id, password1);
//int i;
//  if(access == true){
    //enter.addActionListener(handler);//if login is correct call the event handler


 }
 //class inside class
 //inner class inherits everything from outer class
private class theHandler implements ActionListener{//this class handles the events
                                             //waits for an event to happen and execute the code

    public void actionPerformed(ActionEvent event){//actionPerformed is a built-in method
                                                  //ActionEvent is also built-in
        //String string = "";

        boolean info = false;//to check if the login and password are correct

        if(event.getSource()== enter){//getSource = where the event occurred 
            JOptionPane.showMessageDialog(null, "Hello");

            //String inputA = loginBox.getText();//obtain input from user as a string

        //  login1 = Integer.parseInt(inputA); //convert it to int so it can be used

            //char[] passcode2 = password.getPassword(); //create array of chars to get password
            //password1 = Integer.parseInt(String.valueOf(passcode2));//changing characters to int


            //info = atmObject.Info(login1, password1);

        //  if(info==true)

            cl.show(setPanel, "MainMenu");

        }


    }
}

我放置弹出窗口以查看按钮是否有问题,但弹出窗口显示按下按钮的时间。因此事件处理适用于按钮。我按下按钮时收到的消息。

Graphics$theHandler.actionPerformed
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)  
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

就是这样。我有另一个程序的类似代码,但它没有给我任何错误。为什么说未知来源?

这是我的主要课程

import javax.swing.JFrame;

 public class Display {//this class is to call the other methods


  public static void main(String[] args) {


    Graphics atmObject = new Graphics();
    atmObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    atmObject.setSize(350, 100);
    atmObject.setVisible(true);

}
}

我有另一个类,但是我还没有在我的代码中使用它。我为我的其他ATM类创建了一个对象,但它还没有在代码中使用,所以它不应该干扰代码。

2 个答案:

答案 0 :(得分:1)

如果没有更多信息,就不可能100%,但我相信你正在掩盖你的变数。

我的假设基于以下内容......

// Here, you create a local copy of the CardLayout
CardLayout cl = new CardLayout();
// But here it seems you are creating a class instance of the setPanel...
setPanel =  new JPanel();//panel to store the other panels
setPanel.setLayout(cl);//create the different set of windows

JButton enter = new JButton("Enter");
JPanel loginPanel = new JPanel();

loginPanel.add(enter);

和...

// You create a instance of some handler (presumably a implementation of ActionListener)
// which suggests that the handler is not (at least) an anonymous inner class...
theHandler handler = new theHandler();//builds an action listener object
enter.addActionListener(handler)
loginPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(loginPanel, "LoginScreen");

最后......

if(event.getSource()== enter){//getSource = where the event occurred 
    JOptionPane.showMessageDialog(null, "Hello");
    // You can reference "cl", which suggests that the program
    // can compile, meaning the "cl" must be declared some where
    // and based on the method you are accessing, is probably
    // a CardLayout...which hasn't being initialised...
    cl.show(setPanel, "MainMenu"); <-----here is the problem

根据整个代码进行更新

好的,在您Graphics课程中,您声明cl类型为CardLayout的实例变量....

public class Graphics extends JFrame {//this class is to display the GUI on the screen

    /*...*/
    private CardLayout cl;//to set the different panels

然后在你的构造函数中......

public Graphics(){ //constructor
    /*...*/
    CardLayout cl = new CardLayout();

您正在声明它...这意味着构造函数中的cl仅在构造函数中有效且可行。一旦构造函数存在,引用就不再有效。这使实例变量cl未初始化... OR null

然后......在您的actionPerformed方法中,您引用了cl的实例变量null ...

private class theHandler implements ActionListener{//this class handles the events
                                         //waits for an event to happen and execute the code
    public void actionPerformed(ActionEvent event){//actionPerformed is a built-in method
                                                  //ActionEvent is also built-in
        /*...*/
        // Then you try and reference the instance variable which is null
        cl.show(setPanel, "MainMenu");

    }
}

在构造函数中,更改...

    CardLayout cl = new CardLayout();

要...

    cl = new CardLayout();

也...

  • Java类使用CamelCase约定。我建议你看一下Code Conventions for the Java Programming Language。它将使您的代码更具可读性。
  • Graphics已经是java.awt中定义的类。我强烈建议您不要自己使用它,因为从长远来看它只会让您和其他人感到困惑......

答案 1 :(得分:1)

Graphics$theHandler.actionPerformed
  1. 类名应以大写字母开头。 “theHandler”应该是“TheHandler”(实际上它应该是一个更好的描述性名称)。

  2. 看起来您的班级名称是“图形”。这已经是JDK类名。使用其他名称。

  3. 怀疑这些建议将解决您的问题。为此,我们需要SSCCE