Help with event handling - beginner
This is my code, i am construction a GUI straight from code:
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.
Re: Help with event handling - beginner
Re: Help with event handling - beginner
Quote:
Originally Posted by
KevinWorkman
I have read this already but failed to understand it. I figured the best way to learn how to implement handlers in practice is by seeing an example of my code with an event handler.
For example i do not know where or how to declare an event handler class, and whether or not that means actually creating a new class file to do so and then to reference it in my frame. Im clueless, yet it would help me in understanding this hugely if somebody could be so kind as to show this to me in practice in my own code.
Re: Help with event handling - beginner
You can do it a number of ways. Declare it as a separate class, or implement it using the same class, or instantiate an anonymous inner class. Try one, post what you've tried, and we'll go from there. The tutorial pretty much contains all the code you need.
Re: Help with event handling - beginner
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("sumbit");
add(button);
button.setSize(20, 50);
button.setLocation(400,200);
button.setVisible(true);
button.addActionListener(new ActionListener(){ //EVENT HANDLER
public void actionPerformed(ActionEvent ae){
System.out.println("working?");
}
});
}
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("Star Alliance");
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);
}
}
This is one method i used, it works OK but i do not know if this is the proper way of doing it. And to be honest i copy and pasted that part from here -> java blog for beginners: Java swing event handling example programs for beginners so i dont actually understand how it links my button with that particular event, perhaps you could help me break it down and understand it? Thank you for your patience.
I suppose the way it understands to link the event wıth that particular button is because the event happens in the addlistener bloc for the button, right? Why does that code end with this though "}); "? as opposed to just "}".
Re: Help with event handling - beginner
For more info on the underlying workings of Events, read the tutorials: Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing)
And just look at how your parenthesis and brackets match up. Put each one on a separate line if you have to, with a comment explaining what is beginning or ending.
Re: Help with event handling - beginner
Believe me, i've read all of that, to no avail!
Thanks for your help.
Re: Help with event handling - beginner
Quote:
Originally Posted by
kyasuleyman
Believe me, i've read all of that, to no avail!
Then you need to post exactly what you don't understand.
Re: Help with event handling - beginner
Quote:
Originally Posted by
KevinWorkman
Then you need to post exactly what you don't understand.
Well as you saw i eventually figured it out. But i need to know how i can use different events:
Code :
button.addActionListener(new ActionListener(){ //EVENT HANDLER
public void actionPerformed(ActionEvent ae){
System.out.println("working?");
}
});
I mean this is action performed, so basically when the button is clicked. What would i put for a key pressed event for example?
Re: Help with event handling - beginner
Quote:
Originally Posted by
kyasuleyman
I mean this is action performed, so basically when the button is clicked. What would i put for a key pressed event for example?
I've asked you several times to read the tutorial. It contains information on many kinds of listeners, including KeyListeners and MouseListeners.
Re: Help with event handling - beginner
Also, the first result for googling "java key pressed event" is a part of the tutorial I already linked you.