这个JAVA程序怎么显示的不对?

来源:百度知道 编辑:UC知道 时间:2024/06/04 14:54:43
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class WindowTextArea extends Frame implements TextListener,ActionListener
{ TextArea text1,text2;
Button buttonClear;
WindowTextArea()
{ setLayout(new FlowLayout());
text1=new TextArea(6,15);
text2=new TextArea(6,15);
buttonClear=new Button("清空");
add(text1);
add(text2);
add(buttonClear);
text2.setEditable(false);
text1.addTextListener(this);
buttonClear.addActionListener(this);
setBounds(100,100,350,160);
setVisible(true);
validate();
}
public void textValueChanged(TextEvent e)
{ String s=text1.getText();
StringTokenizer fenxi=new StringTokenizer(s," ,'\n'");
int n=fenxi.countTokens();
String a[]=new String[n];
for(int i=0;i&

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
加不加与程序能不能正确运行没有关系。它只是java Swing里用来关闭窗口的方法。在awt里可以用WindowsListner或windowsAdapter来实现。
真正的错误是
for (int i=0;i<n-1;i++) {
String temp=fenxi.nextToken();
a[i]=temp;
}
应为i<=n-1或i<n 原循环致使程序抛出异常,故得不到想要的结果。

首先要用Collections.sort(List, comp1);进行排序
其次text2.append(a[i]+"\n");意思是 追加“字符串+回车”
再次 是让字符串在text中显示出来

首先更正一下楼主和xhp861031的错误,text2.append(a[i]+"\n");仅仅是给text2这个对象赋值,也就是a[i]+"\n";因为text2是textfield类型的,所以用它的成员函数append追加就可以。并没有往屏幕上显示,要是想显示的话应该用text2.setText(“XX”);

再有看看程序的流程,主函数创建一个WindowTextArea 对象,然后掉构造函数,然后对text1和buttonClear进行监听,可监听的函数里呢?什么都没有啊?
至少应该是
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == text1){

}else if(e.getSource() == buttonClear){

}
}
然后在里边加入你想做的操作啊

还有就是textValueChanged这个函数,楼主写了半天都没调用,能有什么变