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

时间:2020-10-08 13:41:53

标签: java swing events awt action

我遇到以下错误

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,0,700 , 500);
    panel.add(label);
    panel.setLayout(new FlowLayout());
    frame.add(panel);
    frame.setBounds(0,0,700 , 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");
}

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

0 个答案:

没有答案