Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按钮赢了';我什么也不做_Java_Swing_User Interface_Action_Jbutton - Fatal编程技术网

Java 按钮赢了';我什么也不做

Java 按钮赢了';我什么也不做,java,swing,user-interface,action,jbutton,Java,Swing,User Interface,Action,Jbutton,我已经检查了我的代码一段时间了,找不到为什么我的按钮不能工作。我的“开始按钮”在单击时工作,但我的“帮助按钮”不工作。当我点击按钮时,它不会运行println代码。任何帮助都将不胜感激 public class test extends JFrame{ private JLabel label; private JButton button; private ImageIcon bgi; private JLabel bgl; public stat

我已经检查了我的代码一段时间了,找不到为什么我的按钮不能工作。我的“开始按钮”在单击时工作,但我的“帮助按钮”不工作。当我点击按钮时,它不会运行println代码。任何帮助都将不胜感激

public class test extends JFrame{

    private JLabel label;
    private JButton button;

    private ImageIcon bgi;
    private JLabel bgl;

    public static Rectangle gameSquare;


    private JButton startButton;
    private JButton helpButton;
    private final Action action = new SwingAction();


    public static void main(String[] args) throws MalformedURLException, IOException {
        test gui = new test ();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
        gui.setSize(902, 305);
        gui.setVisible(true);
        gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
    }

    public test() throws MalformedURLException, IOException{

        bgi = new ImageIcon(getClass().getResource("tu.png"));
        getContentPane().setLayout(null);

        BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
        //ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
        startButton = new JButton("");
        startButton.setIcon(new ImageIcon(img));
        startButton.setBounds(22, 186, 114, 50);


        getContentPane().add(startButton);

        BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
        final JButton helpButton = new JButton("");
        helpButton.setIcon(new ImageIcon(img2));
        helpButton.setBounds(192, 186, 114, 50);

        getContentPane().add(helpButton);

        bgl = new JLabel (bgi);
        bgl.setBounds(0, 0, 886, 272);
        getContentPane().add(bgl);

        Events e = new Events();
        startButton.addActionListener(e);
        helpButton.addActionListener(e);
    }

    public class Events implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == startButton) {
               label.setText("Searching");

               try {
                Unfollow();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }
            else if (e.getSource() == helpButton){
                System.out.println("gottem");
            }
    }

    }

test
构造函数中隐藏
JButton
变量
helpButton
,从而与
ActionListener
中的
null
引用进行比较。替换

JButton helpButton = new JButton("");


旁白:Java命名约定显示类以大写字母开头,例如
Test

您正在跟踪
Test
构造函数中的
JButton
变量
helpButton
,从而与
ActionListener
中的
null
引用进行比较。替换

JButton helpButton = new JButton("");


旁白:Java命名约定显示类以大写字母开头,例如
Test

如果执行
startButton.addActionListener(new Events())是否有效
帮助按钮.addActionListener(新事件())?如果执行
startButton.addActionListener(new Events())操作,它是否有效
帮助按钮.addActionListener(新事件())?是的,就是这样!谢谢大家!@卢克渥廷:如果他的答案对你有帮助,那就投票吧,并接受答案。是的,就是这样!谢谢大家!@卢克渥廷:如果他的答案对你有帮助,那就投票吧,并接受答案。