簡體   English   中英

Java:簡單GUI程序的NullPointerException

[英]Java: NullPointerException of a simple GUI program

嗨,我是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[] 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)

請指教。

親切的問候。

您永遠不會初始化this.frame ,因此它為null

go() ,創建並初始化一個不同的變量,稱為frame

JFrame frame = new JFrame();

您可能要刪除第一個JFrame

frame = new JFrame();

您已經聲明了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

框架為空。 只需使用二傳手為框架。 就像是 :

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

另外,您已兩次聲明幀。 從go()中刪除它;

更改此行:

JFrame frame = new JFrame();

這樣:

frame = new JFrame();
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);


}

讓我解釋一下異常的原因。

第一個public JFrame frame; 您聲明第一個花括號具有全局范圍。 第二個JFrame frame = new JFrame(); 您初始化會為其創建一個新實例。 您在go()方法中聲明的框架具有局部范圍,並且不會將其初始化為全局范圍。 這就是為什么global scope instance of frame is uninitialized 它導致NullPointerException

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM