每当我尝试单击JFrame上的选项按钮时,我都会收到运行时错误NullPointerException。 JFrame是CharSelection(),我想禁用它,以便我无法点击该JFrame上的任何内容,并且必须单击后退按钮才能再次启用它。这是按钮的代码。
JButton btnOptions = new JButton("Options");
btnOptions.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
CharSelection.disabled();
GameMenu.getMusicOption().setVisible(true);
}
});
btnOptions.setForeground(Color.WHITE);
btnOptions.setFocusPainted(false);
btnOptions.setBackground(Color.BLACK);
btnOptions.setBounds(10, 669, 200, 50);
contentPane.add(btnOptions);
在mouseClicked事件中,我尝试禁用然后检索其他jFrame,我知道如果我取出禁用行,我可以检索其他JFrame的事实,我用来禁用JFrame的方法是我在CharSelection类中创建的方法就是它。
public static void disabled(){
frame.setEnabled(false);
}
我将框架实例化为
private static CharSelection frame;
这可以避免任何编译错误,但每当我运行程序并单击此选项按钮时,此代码没有任何反应,我收到了NullPointerException错误。不知道如何解决这个问题,但你也应该知道我在第一个Jframe GameMenu中做了同样的事情。除了它禁用JFrame没有问题,我一直试图解决这个问题几天,每天花费大约4个小时纯粹在这个问题,并无法弄明白。
错误代码是这个
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at game.CharSelection.disabled(CharSelection.java:1925)
at game.CharSelection$34.mouseClicked(CharSelection.java:1748)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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.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)
最重要的一行是前三名。我只能推测导致这种情况的原因并且不知道原因我无法解决它。感谢帮助。
希望这会帮助您查看代码,我已经删除了所有不相关的内容。
private static CharSelection frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new CharSelection();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CharSelection() {
JButton btnOptions = new JButton("Options");
btnOptions.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
CharSelection.disabled();
GameMenu.getMusicOption().setVisible(true);
}
});
btnOptions.setForeground(Color.WHITE);
btnOptions.setFocusPainted(false);
btnOptions.setBackground(Color.BLACK);
btnOptions.setBounds(10, 669, 200, 50);
contentPane.add(btnOptions);
}
public static void disabled(){
frame.setEnabled(false);
}
public static void enabled(){
frame.setEnabled(true);
}
答案 0 :(得分:1)
您没有真正实例化框架。有一个
frame = new JFrame()
某处失踪了。
另外通过static
方法执行此操作看起来非常糟糕,但如果没有全面了解,很难提出解决方案。