JFrame问题不知道为什么不工作

时间:2017-05-06 16:16:56

标签: java jframe

我尝试了所有但是它不起作用 错误行1是

selfDestructButton.addActionListener(new ActionListener() {

错误行第二个是

frame.setContentPane (new gui().panel1);

错误说:

Exception in thread "main" java.lang.NullPointerException
at gui.<init>(gui.java:34)
at gui.main(gui.java:44)

代码

import java.awt.Dimension;

public class gui {
    private JPanel panel1;
    private JTabbedPane tabbedPane1;
    private JCheckBox wTapCheckBox;
    private JCheckBox aimAssistCheckBox;
    private JCheckBox velocityCheckBox;
    private JCheckBox reachCheckBox;
    private JCheckBox autoclickerCheckBox;
    private JButton selfDestructButton;
    private JCheckBox deleteFilesCheckBox;
    private JCheckBox deleteLastActivityLogsCheckBox;
    private JComboBox comboBox1;
    private JSpinner spinner1;
    private JSpinner spinner2;
    private JSpinner spinner3;
    private JSpinner spinner4;
    private JSpinner spinner5;
    private JSpinner spinner6;
    private JSpinner spinner7;
    private JSpinner spinner8;
    private JSpinner spinner9;
    private JSpinner spinner10;
    private JSpinner spinner11;
    private JSpinner spinner12;
    private JSpinner spinner13;
    private JSpinner spinner14;

    public gui() {
        selfDestructButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String args[]){
        JFrame frame = new JFrame("Cheestar 0.2");
        frame.setContentPane (new gui().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        Dimension d=new Dimension();

        d.setSize(820,420);

        frame.setSize(d);

    }
}

1 个答案:

答案 0 :(得分:0)

添加

selfDestructButton = new JButton();

作为添加动作侦听器之前构造函数中的第一行。

实际上,您尚未实例化任何组件。您必须实例化它们才能使用它们。