Hello all, I am new to JAVA programming, so I started a few days ago with my first program.
I found an example at YouTube with the code attached below.
However, the following message appears:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot use "this" in a static context
I have searched on the internet, but unfortunately without any success.
If I change "this" to "null", then I can run the program , however nothing happens if I hit the button.
Any idea where I should find this problem?
Thanks in advance.
button.addActionListener(this);
public class Main implements ActionListener { public static void main(String[] args) { int top=500, left=130, bottom=130, right=500; JFrame frame = new JFrame(); JButton button = new JButton("Click me"); button.addActionListener(this); JLabel label = new JLabel("Number of clicks"); JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right)); panel.setLayout(new GridLayout(0,1)); panel.add(button); panel.add(label); frame.add(panel, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Title"); frame.pack(); frame.setVisible(true); } // End Static Main