2013-03-31 46 views
1

嗨,我是新手Java学习者。我正在努力实现一个简单的GUI程序,通过点击按钮来改变面板的颜色。Java:NullPointerException一个简单的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[] 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) 

请指点。

亲切的问候。

+0

不应该将该类命名为Button_label吗? – stevenelberger

回答

2

你永远不会初始化this.frame,所以它是null

go(),您创建和初始化不同的变量,名为frame

JFrame frame = new JFrame(); 

你可能想要删除的第一JFrame

frame = new JFrame(); 
+1

你是这些例外的冠军,你不是NPE吗? ;) – karmanaut

+0

@karmanaut:我的面包和黄油:) – NPE

+0

既然没有PM在这里,你在编程领域有多久了?你的个人资料说2年,但在这个领域没有多久。 – karmanaut

0

您已经声明2个变量称为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

0

帧为空。只需使用一个setter的框架。例如:

void setFrame(JFrame theFrame) { 
    this.frame = theFrame; 
} 

此外,您已声明帧两次。从go()中移除它;

0

改变这一行:

JFrame frame = new JFrame(); 

通过这样的:

frame = new JFrame(); 
0
public class Button_lable implements ActionListener { 
public JFrame frame; // This is a class/global variable 
//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(); // This is local variable. 
    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); 


} 

您已定义的类变量作为帧。 您正在实例化局部变量帧。所以不分配它是非常明显的,它会抛出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); 


} 
0

让我来解释Exception的原因。

您在第一个括号下声明的第一个public JFrame frame;具有全局范围。你初始化的第二个JFrame frame = new JFrame();创建它的一个新实例。您在go()方法中声明的框架具有局部范围,并且它初始化它不是全局范围。这就是为什么global scope instance of frame is uninitialized这是造成NullPointerException