当前位置: 技术问答>java相关
大家请看这个问题
来源: 互联网 发布时间:2015-04-18
本文导语: import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class choices extends Applet implements ItemListener{ Choice choice1; TextField text1; public void init(){ text1=new TextField(20); add(text1); choice1.add("Item1"); choice1.add("Item2"); c...
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class choices extends Applet implements ItemListener{
Choice choice1;
TextField text1;
public void init(){
text1=new TextField(20);
add(text1);
choice1.add("Item1");
choice1.add("Item2");
choice1.add("Item3");
choice1.add("Item4");
add(choice1);
choice1.addItemListener(this);
}
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==choice1){
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item1"){
text1.setText("1 be selected");
}
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item2"){
text1.setText("2 be selected");
}
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item3"){
text1.setText("3 be selected");
}
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item4"){
text1.setText("4 be selected");
}
}
}
}
编译没问题!可是用appletviewer choices.htm运行时:
java.lang.NullPointerException
at choices.init(choices.java:14)
at sun.applet.AppletPanel.run(AppletPanel.java:341)
at java.lang.Thread.run(Thread.java:536)
初始化不了小程序
import java.awt.event.*;
import java.awt.*;
public class choices extends Applet implements ItemListener{
Choice choice1;
TextField text1;
public void init(){
text1=new TextField(20);
add(text1);
choice1.add("Item1");
choice1.add("Item2");
choice1.add("Item3");
choice1.add("Item4");
add(choice1);
choice1.addItemListener(this);
}
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==choice1){
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item1"){
text1.setText("1 be selected");
}
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item2"){
text1.setText("2 be selected");
}
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item3"){
text1.setText("3 be selected");
}
if(((Choice)e.getItemSelectable()).getSelectedItem()=="Item4"){
text1.setText("4 be selected");
}
}
}
}
编译没问题!可是用appletviewer choices.htm运行时:
java.lang.NullPointerException
at choices.init(choices.java:14)
at sun.applet.AppletPanel.run(AppletPanel.java:341)
at java.lang.Thread.run(Thread.java:536)
初始化不了小程序
|
choice1没有初始化啊