This is my code, i am construction a GUI straight from code:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Menu extends JFrame { private ImageIcon image; //variables private JLabel label1; Menu(){ //constructor setLayout(new FlowLayout()); image = new ImageIcon(getClass().getResource("menu.png")); label1 = new JLabel(image); label1.setLocation(0, 0); add(label1); Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dim = toolkit.getScreenSize(); label1.setPreferredSize(new Dimension(dim.width-100, dim.height-100)); Button button = new Button("submit"); //THIS IS THE BUTTON I WISH TO HAVE AN ACTION PERFORMED EVENT FOR add(button); button.setSize(20, 50); button.setLocation(400,200); button.setVisible(true); } public static void main (String args[]){ //main entry point of program Menu gui = new Menu(); gui.pack(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setVisible((true)); gui.setAlwaysOnTop(true); gui.setTitle("TITLE"); gui.setResizable(false); Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dim = toolkit.getScreenSize(); // Determine the new location of the window int w = gui.getSize().width; int h = gui.getSize().height; int x = (dim.width-w)/2; int y = (dim.height-h)/2; // Move the window gui.setLocation(x, y); } }
I am having troubles understanding how to implement event handlers for my button "submit" or where to place the code. Can somebody give me some direction so that i might learn how to do this? I am struggling with it at the moment despite attempting multiple online tutorials and documentation. Much help is appreciated, please be gentle to the beginner!
EDIT: I have commented the specific code that i wish to have an event attached to.