如何解决:非静态变量,不能从静态上下文中引用

问题描述

我遇到以下错误

frontend.java:12: error: non-static variable this cannot be referenced from a static context button.addActionListener(this); 1 error

这是主要和操作执行方法的代码

public static void  main(String []args){
    JFrame frame = new JFrame("new Screen");
    JPanel panel = new JPanel();
    JLabel label = new JLabel(new ImageIcon("appBackgroundIMG.png"));
    JButton button = new JButton("Encrypt");
    button.addActionListener(this); //this line has error
    button.setBounds(290,300,120,30);
    label.add(button);
    label.setBounds(0,700,500);
    panel.add(label);
    panel.setLayout(new FlowLayout());
    frame.add(panel);
    frame.setBounds(0,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    System.out.println("Hello");
}

请注意,我不是非常有经验,所以如果有一些愚蠢的错误,请不要生气…… 我要去哪里错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)