import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class Dark implements ActionListener {
JPanel p;
Timer timer;
int ptr = 0;
Graphics2D g2;
int mode = 0; public Dark() {
p = new JPanel();
p.setSize(800, 600);
p.setVisible(true);
timer = new Timer(250, this);
} public void st(int i) { timer.stop();
if (i == 1) {
ptr = 0;
} else {
ptr = 60;
} mode = i;
timer.restart();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == timer) {
Graphics g = p.getGraphics();
Color c = new Color(255, 255, 255, ptr); //RGB固定,alpha值由1遞增到255
g.setColor(c);
g.fillRect(0, 0, 800, 600); System.out.println(ptr);
if (mode == 1) {
if (ptr < 55) {
ptr++; } else {
timer.stop();
}
} else {
if (ptr > 0) {
ptr--;
} else {
timer.stop();
}
}
}
}}
别的类用它的时候
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Dark.actionPerformed(Dark.java:43)
为甚啊??
import javax.swing.*;
import java.awt.event.*;public class Dark implements ActionListener {
JPanel p;
Timer timer;
int ptr = 0;
Graphics2D g2;
int mode = 0; public Dark() {
p = new JPanel();
p.setSize(800, 600);
p.setVisible(true);
timer = new Timer(250, this);
} public void st(int i) { timer.stop();
if (i == 1) {
ptr = 0;
} else {
ptr = 60;
} mode = i;
timer.restart();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == timer) {
Graphics g = p.getGraphics();
Color c = new Color(255, 255, 255, ptr); //RGB固定,alpha值由1遞增到255
g.setColor(c);
g.fillRect(0, 0, 800, 600); System.out.println(ptr);
if (mode == 1) {
if (ptr < 55) {
ptr++; } else {
timer.stop();
}
} else {
if (ptr > 0) {
ptr--;
} else {
timer.stop();
}
}
}
}}
别的类用它的时候
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Dark.actionPerformed(Dark.java:43)
为甚啊??
at Dark.actionPerformed(Dark.java:43)字面意思是Dark.java这个文件的第43行有一个复合类型的变量是null,但是却使用了它。lz在自己的ide中确定一下出错位置。