嗨,我正在尝试添加an监听器,它将从用户那里获取输入并改变背景的颜色,但是我得到了这个错误.你能不能具体点给初学者一个答案..。谢谢enter code here
package Quizzes_practice;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TextField1 extends JFrame {
private JPanel mainpanel;
private JLabel text;
private JTextField mytext;
private JTextField bg;
public TextField1(){
setLayout(new GridLayout(2,0));
JPanel mainpanel = new JPanel();
JLabel text = new JLabel("Enter value: ");
JTextField mytext = new JTextField(10);
JTextField bg = new JTextField(40);
add(mainpanel);
mainpanel.setLayout(new FlowLayout());
mainpanel.add(text);
mainpanel.add(mytext);
mainpanel.add(bg);
myhand handler = new myhand();
mytext.addActionListener(handler);
bg.addActionListener(handler);
}
private class myhand implements ActionListener{
public void actionPerformed (ActionEvent event){
int a2 = Integer.parseInt(event.getActionCommand());
Color mycol= new Color(a2);
mainpanel.setBackground(mycol);
}
}
}我所犯的错误是
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Quizzes_practice.TextField1$myhand.actionPerformed(TextField1.java:47)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.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)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)发布于 2014-06-13 08:56:48
在构造函数中,您做到了
JPanel mainpanel = new JPanel();它创建一个本地mainPanel,并且该作用域在构造函数中。您需要初始化实例成员。到目前为止,他们是null。因此出现了例外。
那应该是
mainpanel = new JPanel();剩下的成员text,mytext,bg也是如此。
https://stackoverflow.com/questions/24201382
复制相似问题