I'm making a new program from scratch and adding an action listener to my program really confuses me. This program is just for experimenting and the visual interface is not that pleasant. Just disregard it for now.
I tried but.addActionListener(this); but it still gave me an error.
./Inventory.java:31: error: non-static variable this cannot be referenced from a static context
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Inventory extends JFrame implements ActionListener {
public Inventory() {
startgui();
}
public static void startgui() {
JFrame frame = new JFrame("Inventory");
frame.setLayout(null);
frame.setSize(500, 480);
frame.setLocation(540, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JLabel label = new JLabel("Welcome To My Program");
label.setBounds(10, 10, 310, 60);
label.setVisible(true);
frame.add(label);
JButton but = new JButton("Well Played");
but.setBounds(20, 20, 70, 30);
frame.add(but);
but.addActionListener(/* what do I insert here? */);
}
public void actionPerformed(ActionEvent e) {
System.out.print("yess");
}
}