急!!!!!java TextField()问题
源程序:importjava.awt.*;importjava.awt.event.*;publicclassmessageextendsFrameimplementsA...
源程序:
import java.awt.*;
import java.awt.event.*;
public class message extends Frame implements ActionListener{
private TextArea content;
private TextField input_content ;
private Label title;
private Label hi;
private Label say;
private Button clear;
private Button to_top;
private Button to_bottom;
private Button submit;
private Choice face;
private String face_to[] = {"Smail","Angry","3","4","5","6"};
public message(){
setLayout(new BorderLayout());
//title
title = new Label("Message");
add("North", title);
//Text Input Area
content = new TextArea(200,180);
content.setText("留言内容:");
content.setEditable(false);
add("Center", content);
//Function bottom
Panel fun_area = new Panel();
fun_area.setLayout(new GridLayout(3,1,10,20));
clear = new Button("Clear");
fun_area.add(clear);
to_top = new Button("Top");
fun_area.add(to_top);
to_bottom = new Button("Bottom");
fun_area.add(to_bottom);
add("East", fun_area);
//Bottom
Panel bottom_area = new Panel();
bottom_area.setLayout(new FlowLayout(FlowLayout.RIGHT,5,0));
Label hi = new Label("You");
bottom_area.add(hi);
Choice face = new Choice();
for(int i =0; i < face_to.length; i++ ){
face.addItem(face_to[i]);
}
bottom_area.add(face);
Label say = new Label("Say");
bottom_area.add(say);
TextField input_content = new TextField(15);
input_content.setText("123");
bottom_area.add(input_content);
Button submit = new Button("Submit");
bottom_area.add(submit);
add("South", bottom_area);
submit.addActionListener(this);
submit.setActionCommand("submit");
to_top.addActionListener(this);
to_top.setActionCommand("to_top");
to_bottom.addActionListener(this);
to_bottom.setActionCommand("to_bottom");
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("submit")){
if(content.getText() != null){
content.append("\n"+"你"+"地说:"+input_content.getText()); //如果这去掉input_content.getText()程序运行没问题,加上就会提示空指针,怎么回事?是不是哪没初始化还是怎么的
}
}
else if(cmd.equals("to_top")){
}
else if(cmd.equals("to_bottom")){
}
}
public static void main(String args[]){
message m=new message();
m.setSize(400,300);
m.setVisible(true);
}
}
问题解决了,2楼比较详细, 难道把类型去掉了就是初始化了? TextField input_content = new TextField(15);
这样不是初始化? 展开
import java.awt.*;
import java.awt.event.*;
public class message extends Frame implements ActionListener{
private TextArea content;
private TextField input_content ;
private Label title;
private Label hi;
private Label say;
private Button clear;
private Button to_top;
private Button to_bottom;
private Button submit;
private Choice face;
private String face_to[] = {"Smail","Angry","3","4","5","6"};
public message(){
setLayout(new BorderLayout());
//title
title = new Label("Message");
add("North", title);
//Text Input Area
content = new TextArea(200,180);
content.setText("留言内容:");
content.setEditable(false);
add("Center", content);
//Function bottom
Panel fun_area = new Panel();
fun_area.setLayout(new GridLayout(3,1,10,20));
clear = new Button("Clear");
fun_area.add(clear);
to_top = new Button("Top");
fun_area.add(to_top);
to_bottom = new Button("Bottom");
fun_area.add(to_bottom);
add("East", fun_area);
//Bottom
Panel bottom_area = new Panel();
bottom_area.setLayout(new FlowLayout(FlowLayout.RIGHT,5,0));
Label hi = new Label("You");
bottom_area.add(hi);
Choice face = new Choice();
for(int i =0; i < face_to.length; i++ ){
face.addItem(face_to[i]);
}
bottom_area.add(face);
Label say = new Label("Say");
bottom_area.add(say);
TextField input_content = new TextField(15);
input_content.setText("123");
bottom_area.add(input_content);
Button submit = new Button("Submit");
bottom_area.add(submit);
add("South", bottom_area);
submit.addActionListener(this);
submit.setActionCommand("submit");
to_top.addActionListener(this);
to_top.setActionCommand("to_top");
to_bottom.addActionListener(this);
to_bottom.setActionCommand("to_bottom");
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("submit")){
if(content.getText() != null){
content.append("\n"+"你"+"地说:"+input_content.getText()); //如果这去掉input_content.getText()程序运行没问题,加上就会提示空指针,怎么回事?是不是哪没初始化还是怎么的
}
}
else if(cmd.equals("to_top")){
}
else if(cmd.equals("to_bottom")){
}
}
public static void main(String args[]){
message m=new message();
m.setSize(400,300);
m.setVisible(true);
}
}
问题解决了,2楼比较详细, 难道把类型去掉了就是初始化了? TextField input_content = new TextField(15);
这样不是初始化? 展开
展开全部
input_content这个没有初始化,初始化一下就好了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
content.append("\n"+"你"+"地说:"+input_content.getText()); //如果这去掉input_content.getText()程序运行没问题,加上就会提示空指针,怎么回事?是不是哪没初始化还是怎么的
改为:
content.append("\n"+"你"+"地说:"+String(input_content.getText()));
改为:
content.append("\n"+"你"+"地说:"+String(input_content.getText()));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询