Java:NullPointerException是一个简单的GUI程序
大家好,我是Java初学者。我试图实现一个简单的GUI程序,通过点击按钮来改变面板的颜色Java:NullPointerException是一个简单的GUI程序,java,Java,大家好,我是Java初学者。我试图实现一个简单的GUI程序,通过点击按钮来改变面板的颜色 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Button_lable implements ActionListener { public JFrame frame; //JPanel panel; //JLabel label; public static void main(String
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Button_lable implements ActionListener {
public JFrame frame;
//JPanel panel;
//JLabel label;
public static void main(String[] args) {
Button_lable gui = new Button_lable();
gui.go();
}//end of main
public void go(){
//System.out.println("Entered Go()");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b_frame = new JButton("Click to change the color");
b_frame.addActionListener(this);
MyDrawpanel d_panel = new MyDrawpanel();
frame.getContentPane().add(BorderLayout.SOUTH,b_frame);
frame.getContentPane().add(BorderLayout.CENTER ,d_panel);
frame.setSize(300, 300);
frame.setVisible(true);
}//end of go
public void actionPerformed(ActionEvent e) {
frame.repaint();
}
}//end of Button_lable
class MyDrawpanel extends JPanel {
public void paintComponent(Graphics g){
Graphics2D grph = (Graphics2D) g;
int red = (int)(Math.random()* 255);
int green = (int)(Math.random()* 255);
int blue = (int)(Math.random()* 255);
Color strt_clr = new Color(red,green,blue);
red = (int)(Math.random()* 255);
green = (int)(Math.random()* 255);
blue = (int)(Math.random()* 255);
Color end_clr = new Color(red,green,blue);
GradientPaint gradient = new GradientPaint(70,70,strt_clr,150,150,end_clr);
grph.setPaint(gradient);
grph.fillOval(50,25, 150, 150);
}
}
我得到输出窗口。但是当我点击按钮时,我得到了以下异常
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Button_lable.actionPerformed(Button_lable.java:34)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(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.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)
请给我一些建议
亲切问候。您从未初始化此.frame,因此它是
null
在go()
中,创建并初始化另一个名为frame
的变量:
JFrame frame = new JFrame();
frame = new JFrame();
frame = new JFrame();
您可能需要删除第一个JFrame
:
JFrame frame = new JFrame();
frame = new JFrame();
frame = new JFrame();
您已经声明了两个名为
frame
的变量-一个类变量和一个内部变量go
。
删除go
中的声明&只需初始化go
public void go(){
//System.out.println("Entered Go()");
JFrame frame = new JFrame();
将上述内容更改为
public void go(){
//System.out.println("Entered Go()");
frame = new JFrame();
由于您正在
go
中创建一个名为frame
的新变量,因此类变量frame
永远不会初始化。它保持为null
,因此NullPointerException
帧为null。只需使用一个框架设置器。比如:
void setFrame(JFrame theFrame) {
this.frame = theFrame;
}
您还声明了两次框架。将其从go()中移除 更改此行:
JFrame frame = new JFrame();
据此:
您已经将类变量定义为frame。
您正在实例化局部变量frame。因此,如果不指定,很明显它将抛出NPE。因为局部变量的作用域只存在于特定的方法中
你只需要做以下事情
public class Button_lable implements ActionListener {
public JFrame frame;
//JPanel panel;
//JLabel label;
public static void main(String[] args) {
Button_lable gui = new Button_lable();
gui.go();
}//end of main
public void go(){
//System.out.println("Entered Go()");
frame = new JFrame(); // here, the class variable is instantiated. So it wont give NPE.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b_frame = new JButton("Click to change the color");
b_frame.addActionListener(this);
MyDrawpanel d_panel = new MyDrawpanel();
frame.getContentPane().add(BorderLayout.SOUTH,b_frame);
frame.getContentPane().add(BorderLayout.CENTER ,d_panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
让我解释一下例外的原因。
第一个
公共JFrame在第一个大括号下声明的代码>具有全局范围。第二个JFrame=newjframe()代码>初始化将创建它的新实例。在go()方法中声明的框架具有局部作用域,它初始化它而不是全局作用域。这就是为什么框架的全局作用域实例未初始化的原因它导致了NullPointerException
该类不应该被命名为Button\u label吗?你是这些异常的拥护者,不是NPE吗?;)因为这里没有PM,你在编程领域有多久了?你的个人资料上写的是两年,但不是多久。