简体   繁体   English

无法在JButton中编写ActionListener

[英]Can not write ActionListener in JButton

Please help me solve my problem. 请帮助我解决我的问题。 My problems are with this code in java . 我的问题与java这段代码有关。

button.addActionListener(new ActionListener() {     
   public void actionPerformed(ActionEvent arg0) {               
        text.append("hello\n");
   }
}); 

and error is : 错误是:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swing.Mainframe.<init>(Mainframe.java:23)
at swing.Swing$1.run(Swing.java:11)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     

Write this before you code: 在编写代码之前先编写以下代码:

button = new JButton();

It seems like your variable isn't been initialized yet. 看来您的变量尚未初始化。

Or either the text variable 或者是text变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM