问题
I have written a small code in java for simpleGUI.
package guidemo1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GuiDemo1 implements ActionListener{
JButton button;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GuiDemo1 gui=new GuiDemo1();
gui.go();
}
public void go()
{
JFrame frame=new JFrame();
button=new JButton();
frame.getContentPane().add(button);
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
button.setText("I've been clicked");
}
}
I am newbie to JAVA.I have few questions related to this program.
Can some one explain how actionPerformed method gets executed with out any call?
Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?
Thanks..
回答1:
Can some one explain how actionPerformed method gets executed with out any call?
The GUI framework Swing runs action handling code in the background. Whenever a button is pressed or the user interacts with the GUI in some other way, Swing will notify your application through one of many Listener
interfaces. In order to receive these events, your class needs to implement the proper Listener
interface and be registered as a listener on each component it is interested in.
Your class implements the ActionListener
interface and calls addActionListener
to register itself to the button. When a button is clicked, Swing will attempt to notify all registered ActionListener
s by calling their actionPerformed
method. That's how the "magic" happens.
Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?
You add
the button to the frame's content pane - this puts the button inside the frame in your layout, not in your code. Because you declare button
as an instance variable by putting JButton button;
outside any method, it is still accessible from any (non-static
) method in that class.
回答2:
Welcome to an event driven environment.
In this environment, you register "listeners" that wait until something happens and then report back to you via a well defined interface structure.
Basically, what's a happening, is you registered yourself as an interested party to action events that may occur on a button. You've done this by implementing the ActionListener
interface. This allows the button, when an action is performed to call back to your actionPerformed
method at some time in the future.
This is commonly known as an observer pattern.
You might find Writing Event Listeners a useful read
回答3:
Your class implements ActionListener
, so you have to define the actionPerformed
method. You also call button.addActionListener(this)
which registers the current object as the listener for the button press.
When a button press event is triggered, all the listeners will be notified, including the object that you have registered as a listener.
回答4:
As for the second question, Java is a reference language. All objects reside on the heap and are alive as long as someone holds the reference to them.
In Swing, the case is a little bit more complicated. The Swing library holds reference to all GUI elements (such as JFrame and JButton), so that's why they don't get deleted, even if you don't hold reference to them any more.
As for the first question, look at what the main thread does - it creates a new object GuiDemo1, calls the go() method which creates a JFrame, and displays that frame. There is a special Swing thread, the Event Dispatch Thread, that waits in the background for events in the GUI to happen. Displaying the frame starts the EDT which then renders the frame and sleeps. The main thread, having completed all the work in the main method then DIES.
Now you have a JFrame displayed and Event Dispatch Thread sleeping. The EDT is now waiting for user interaction (events) with the JFrame - the Swing is event-driven. Each time you interact with the frame, Swing creates an event for it and wakes the EDT to handle the event. So for example, if you click a button, the EDT wakes, animates the button "click" and calls all action listeners. Thats when your method actionPerformed gets called.
回答5:
implement ActionListener
add ActionListener to your interface
add actionPerformed method to your class
set a timer and start it in your class
that will do it
来源:https://stackoverflow.com/questions/15000143/event-handling-in-java-and-the-execution-of-actionperformed-method-in-java