import java.awt.*;
import java.awt.event.*;public class ChatClint {
public static void main(String[] args) {
new ChatClintFrame();
}
}class ChatClintFrame extends Frame{
TextField sendText = new TextField(300);
TextArea showText = new TextArea("欢迎您!",40,1);
public ChatClintFrame() {
setBounds(400,160,380,500);
setTitle("ChatClint");
setLayout(new BorderLayout());
add(sendText,BorderLayout.SOUTH);
add(showText,BorderLayout.NORTH);
addWindowListener(new ChatClintClose()) ;
sendText.addActionListener(new SendMessage());
setVisible(true);
}
/*运行Launch文件
public void launchChatClintFrame() {
new ChatClintFrame();
}
*/
}
class ChatClintClose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}}class SendMessage implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField sendText = (TextField)e.getSource();
String s = sendText.getText();
sendText.setText("");
TextArea showText;
showText.setText(s); //这里注掉以后可以运行不报错,但是没达到我的要求,为什么呢?
}
}最后一行 注掉以后可以运行不报错,但是没达到我的要求,为什么呢?
解决方案 »
- 没用数组之类的,运行时弹出【空指针问题】,求解。。。。。
- 大家有人见过这个错误提示吗?5555555555555希望大家说说啊。或者指引一条处理这个问题的方法啊55谢谢
- 图形界面工具问题
- 关于泛型 和 传类问题啊
- 在html页面中,如何用程序判断本机是否连接到因特网?
- struts 问题,高手帮忙解决,在线等,急~~
- 请问throw 跳转和if else跳转的区别..在实际应用中?哪个更实际?
- ****急急急****java中可不可以传任意多个参数阿,比如说LogOut(String strFmt, ...)这种形式的阿---谢了先
- java的危机!!!
- hashmap数组里存放的key都是同一个对象
- 用FileInputStream写入文件的问题
- JFrame汉字标题 出现乱码 ?这怎么解决 有时程序中有汉字就不能通过编译
public void SendMessage(TextField sendText,TextArea showText){
this.sendText = sendText;
this.showText = showText;
}
public void actionPerformed(ActionEvent e){
TextField sendText = (TextField)e.getSource();
String s = sendText.getText();
sendText.setText("");
TextArea showText;
showText.setText(s); //这里注掉以后可以运行不报错,但是没达到我的要求,为什么呢?
}
}但是会报错,这个怎么改呢?
import java.awt.event.*;public class ChatClint {
public static void main(String[] args) {
new ChatClintFrame();
}
}class ChatClintFrame extends Frame{
TextField sendText = new TextField(300);
static TextArea showText = new TextArea("欢迎您!",40,1);
public ChatClintFrame() {
setBounds(400,160,380,500);
setTitle("ChatClint");
setLayout(new BorderLayout());
add(sendText,BorderLayout.SOUTH);
add(showText,BorderLayout.NORTH);
addWindowListener(new ChatClintClose()) ;
sendText.addActionListener(new SendMessage());
setVisible(true);
}
/*运行Launch文件
public void launchChatClintFrame() {
new ChatClintFrame();
}
*/
}
class ChatClintClose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}}class SendMessage implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField sendText = (TextField)e.getSource();
String s = sendText.getText();
sendText.setText("");
ChatClintFrame.showText.setText(s); //这是改了好之后正常运行。 }
}我在eclipse下和doc下都运行正常
import java.awt.event.*;public class ChatClint {
public static void main(String[] args) {
new ChatClintFrame();
}
}class ChatClintFrame extends Frame{
TextField sendText = new TextField(300);
TextArea showText = new TextArea("欢迎您!",40,1);
public ChatClintFrame() {
setBounds(400,160,380,500);
setTitle("ChatClint");
setLayout(new BorderLayout());
add(sendText,BorderLayout.SOUTH);
add(showText,BorderLayout.NORTH);
addWindowListener(new ChatClintClose()) ;
sendText.addActionListener(new SendMessage(showText));
setVisible(true);
}
/*运行Launch文件
public void launchChatClintFrame() {
new ChatClintFrame();
}
*/
}
class ChatClintClose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}}class SendMessage implements ActionListener{
TextArea showText;
public SendMessage(TextArea showText){
this.showText = showText;
}
public void actionPerformed(ActionEvent e){
TextField sendText = (TextField)e.getSource();
String s = sendText.getText();
sendText.setText("");
showText.setText(s); //这里注掉以后可以运行不报错,但是没达到我的要求,为什么呢?
}
}
lz你试试这个吧。
import java.awt.event.*;public class ChatClient {
public static void main(String[] args) {
ChatClintFrame f= new ChatClintFrame();
}
}
class ChatClintFrame extends Frame{
TextField sendText = new TextField(300);
TextArea showText = new TextArea("欢迎您!",40,1);
public ChatClintFrame() {
setBounds(400,160,380,500);
setTitle("ChatClint");
setLayout(new BorderLayout());
add(sendText,BorderLayout.SOUTH);
add(showText,BorderLayout.NORTH);
addWindowListener(new ChatClintClose()) ;
sendText.addActionListener(new SendMessage());
setVisible(true);
}
class SendMessage implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField sendText = (TextField)e.getSource();
String s = sendText.getText();
System.out.println(s);
sendText.setText("");
showText.setText(s); //这里注掉以后可以运行不报错,但是没达到我的要求,为什么呢?
}
}
}
class ChatClintClose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}}
把SendMessage当内部类