package techer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestCard
implements ActionListener{
CardLayout c = new CardLayout();
JPanel jp1 = new JPanel();
Timer t = new Timer(300,this);
public void actionPerformed(ActionEvent ae){
String comm = ae.getActionCommand();
if("first".equals(comm)){
c.first(jp1);
}else if("next".equals(comm)){
c.next(jp1);
}else if("last".equals(comm)){
c.last(jp1);
}else if("previous".equals(comm)){
c.previous(jp1);
}else if("start".equals(comm)){
t.start();
//}else if("stop".equals(comm)){//1
}else if(comm.equals("stop")){//2
t.stop();
}else{
c.next(jp1);
}
}
public TestCard(){
JFrame jf = new JFrame("Card");
String[] str = {"first","last",
"next","previous","start","stop"};
jp1.setLayout(c);
JPanel jp2 = new JPanel();
JLabel[] jl = new JLabel[6];
String[] s = {"1.jpg","2.jpg",
"3.jpg","4.jpg","5.jpg","6.jpg"};
for(int i=0;i<jl.length;i++){
ImageIcon ii = new
ImageIcon("d:\\hao\\pic\\"+s[i]);
jl[i] = new JLabel(ii);
jp1.add(jl[i],i+"");
}
JButton[] jb = new JButton[str.length];
for(int i=0;i<jb.length;i++){
jb[i] = new JButton(str[i]);
jp2.add(jb[i]);
jb[i].addActionListener(this);
}
jf.add(jp1);
jf.add(jp2,BorderLayout.SOUTH);
jf.setLocation(100,30);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestCard();
}
}点start按钮抛NullPointerException异常,其他按钮运行和功能都正常,但是如果放开注释1把2注释掉,如下
}else if("stop".equals(comm)){//1
//}else if(comm.equals("stop")){//2
点start按钮就一切正常,啥原因啊,郁闷!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestCard
implements ActionListener{
CardLayout c = new CardLayout();
JPanel jp1 = new JPanel();
Timer t = new Timer(300,this);
public void actionPerformed(ActionEvent ae){
String comm = ae.getActionCommand();
if("first".equals(comm)){
c.first(jp1);
}else if("next".equals(comm)){
c.next(jp1);
}else if("last".equals(comm)){
c.last(jp1);
}else if("previous".equals(comm)){
c.previous(jp1);
}else if("start".equals(comm)){
t.start();
//}else if("stop".equals(comm)){//1
}else if(comm.equals("stop")){//2
t.stop();
}else{
c.next(jp1);
}
}
public TestCard(){
JFrame jf = new JFrame("Card");
String[] str = {"first","last",
"next","previous","start","stop"};
jp1.setLayout(c);
JPanel jp2 = new JPanel();
JLabel[] jl = new JLabel[6];
String[] s = {"1.jpg","2.jpg",
"3.jpg","4.jpg","5.jpg","6.jpg"};
for(int i=0;i<jl.length;i++){
ImageIcon ii = new
ImageIcon("d:\\hao\\pic\\"+s[i]);
jl[i] = new JLabel(ii);
jp1.add(jl[i],i+"");
}
JButton[] jb = new JButton[str.length];
for(int i=0;i<jb.length;i++){
jb[i] = new JButton(str[i]);
jp2.add(jb[i]);
jb[i].addActionListener(this);
}
jf.add(jp1);
jf.add(jp2,BorderLayout.SOUTH);
jf.setLocation(100,30);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestCard();
}
}点start按钮抛NullPointerException异常,其他按钮运行和功能都正常,但是如果放开注释1把2注释掉,如下
}else if("stop".equals(comm)){//1
//}else if(comm.equals("stop")){//2
点start按钮就一切正常,啥原因啊,郁闷!
相当于
comm!=null && comm.equals("abc")如果用到变量,首先要想到,变量可能为null,变量为null则调用任何方法都失败(NPE)。
public void actionPerformed(ActionEvent ae) {
String comm = ae.getActionCommand();
System.out.println(ae.getSource().getClass());
System.out.println(ae.getActionCommand());
if ("first".equals(comm)) {
c.first(jp1);
} else if ("next".equals(comm)) {
c.next(jp1);
} else if ("last".equals(comm)) {
c.last(jp1);
} else if ("previous".equals(comm)) {
c.previous(jp1);
} else if ("start".equals(comm)) {
t.start();
// }else if("stop".equals(comm)){//1
} else if (comm.equals("stop")) {// 2
t.stop();
} else {
c.next(jp1);
}
}你可以 把你的监听里面 加上两句输出语句 你看下 你的监听执行了两遍
有一个是timer执行的 它的getActionCommand 是null 所以你的后面就会出现java.lang.NullPointerException这个错误
你可以试下 把t.start();这个注释掉 就不会有错误了
我也试了,让他打印comm到控制台,结果第一个是start,第二个null
super();
this.delay = delay;
this.initialDelay = delay; doPostEvent = new DoPostEvent(); if (listener != null) {
addActionListener(listener);
}
}它会addActionListener(listener);
listener就是你的this 它给自己加了listener 然后执行的时候 就会调用了~
明白了!
是不是这样,
当我点击start的时候,Timer t执行监听,而当执行到comm.equals("stop")时,comm因为是null,所以抛异常
而"stop".equals(comm) 先判断了comm是否为null,所以不会执行能正常通过感谢楼上各位尤其jinxfei和horizonlyhw的指点!