1ButtonListener button_listener =newButtonListener();2button.addActionListener(button_listener); 接下来如果你又想移除该对象了,就直接remove掉就行了 1button.removeActionListener(button_listener); 代码演示: 1packageExample;2importjava.awt.*;3importjava.awt.event.*;4importjavax.swing.*;56publicclasse...
Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... } In general, to detect when the user clicks an onscreen button (or does the keyboard equivalent), a program must have an objec...
/** * Simple1.java - 处理事件的第一种方法 * 在这个例子中,利用一个ActionListener来监听事件源产生的事件 * 用一些if语句来决定是哪个事件源 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Simple1 { private static JFrame frame; // 定义为静态变量以便main...
AnActioncan be used to separate functionality and state from a component. For example, if you have two or more components that perform the same function, consider using an Action object to implement the function. An Action object is an action listener that provides not only action-event handlin...
The code that defines the ActionListener interface, looks something like this: 1 package java.awt.event; 2 import java.util.EventListener; 3 4 public interface ActionListener extends EventListener { 5 6 public void actionPerformed(ActionEvent e); 7 8 } The ActionListener example is an interfac...
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class GuiApp1 { //Note: Typically the main method will be in a //separate class. As this is a simple one class //example it's all in the one class. ...
The java.awt.event.ActionListener interface is responsible for processing events java.awt.event.ActionEvent action generated by the drive buttons (Button), selection items (MenuItem) in suspended (drop-down) or independent menus (popup) at Pressing the "
ActionListener.actionPerformed(ActionEvent e) has the following syntax. voidactionPerformed(ActionEvent e) Example In the following code shows how to use ActionListener.actionPerformed(ActionEvent e) method. //fromwww.java2s.comimportjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;import...
glassButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { glass.setVisible(false); p1.repaint(); } }); } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
importjavax.swing.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;publicclassToggleButtonExample{publicstaticvoidmain(String[]args){JFrameframe=newJFrame("Toggle Button Example");JToggleButtontoggleButton=newJToggleButton("Toggle");toggleButton.addActionListener(newActionListener(){@Overr...