猿问

Java的ActionListenr总是安装不成功无法正常运行

用cmd运行,给出提示“Exception in thread "main"java.lang.NullPointerException”。

为什么安装的监听器老是不成功呢?


import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



import javax.swing.*;


public class task3 extends JFrame implements ActionListener

{

    private static JFrame frame;

    private static JPanel panel1;//下方

    private static JPanel panel2;//图片

    JLabel tips;//用户正误提示处

    JButton check;//

    JTextField answer;//输入答案

    int newNum = 10;//初始图片数量

    public task3()

    {

        this.setTitle("Welcom to Jungle Party");

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLayout(new BorderLayout());

        this.getContentPane().add(panel1,BorderLayout.SOUTH);

    

        this.setBounds(120,120,1200,600);

        panel1 = new JPanel();

        panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));

        answer = new JTextField(10);

        tips = new JLabel("Hello");

        check = new JButton("check");

        panel1.add(tips);

        panel1.add(answer);

        panel1.add(check);

        simpleListener ourListener = new simpleListener();

        check.addActionListener(ourListener);

        this.setVisible(true);

    }

private class simpleListener implements ActionListener

{

    public void actionPerformed(ActionEvent e)

    {

        String buttonName = e.getActionCommand();

        if(buttonName.equals("check"));

        {

                tips.setText("hahaha successful");

        }

    }

}

public static void main(String[] args)

{

task3 abc = new task3();

}

}


慕先生7453236
浏览 1475回答 0
0回答
随时随地看视频慕课网APP

相关分类

Java
我要回答