1ButtonListener button_listener =newButtonListener();2button.addActionListener(button_listener); 接下来如果你又想移除该对象了,就直接remove掉就行了 1button.removeActionListener(button_listener); 代码演示: 1packageExample;2importjava.awt.*;3importjava.awt.event.*;4importjavax.swing.*;56publicclasse...
/** * Simple1.java - 处理事件的第一种方法 * 在这个例子中,利用一个ActionListener来监听事件源产生的事件 * 用一些if语句来决定是哪个事件源 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Simple1 { private static JFrame frame; // 定义为静态变量以便main...
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...
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...
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. ...
创建ActionEvent对象啊,这个对象可以获取到事件源,事件源是一个控件,比如按钮,这只是一个事件对象
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.
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...
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...