java怎样在ActionListener里面使用类里面的变量

时间: 2023-07-15 22:12:18 浏览: 144
在ActionListener中可以通过以下方式访问类中的变量: 1. 将变量声明为类的实例变量,而不是方法内的局部变量。这样,变量就可以在整个类中使用。 2. 在ActionListener中创建类的对象,并使用该对象访问变量。例如: ```java public class MyClass { private int myVariable; public void myMethod() { JButton myButton = new JButton("Click Me"); myButton.addActionListener(new ActionListener() { MyClass obj = new MyClass(); // 创建类的对象 public void actionPerformed(ActionEvent e) { obj.myVariable = 10; // 通过对象访问变量 // 执行其他操作... } }); } } ``` 在上面的示例中,创建了MyClass类的对象obj,并在ActionListener中使用该对象访问myVariable变量。 注意,如果变量是静态变量,则可以直接在ActionListener中访问,不需要创建类的对象。例如: ```java public class MyClass { private static int myVariable; public void myMethod() { JButton myButton = new JButton("Click Me"); myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MyClass.myVariable = 10; // 直接访问静态变量 // 执行其他操作... } }); } } ```
阅读全文

相关推荐

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (EntertainmentMode) void is an invalid type for the variable actionPerformed Syntax error on token "(", ; expected Syntax error on token ")", ; expected cy cannot be resolved cy cannot be resolved panel cannot be resolved to a variable at idiom_game.EntertainmentMode.<init>(GameMenu.java:208) at idiom_game.GameMenu.actionPerformed(GameMenu.java:137) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389) at java.desktop/java.awt.Component.processEvent(Component.java:6391) at java.desktop/java.awt.Container.processEvent(Container.java:2266) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)