当前位置: 代码迷 >> Java相关 >> 为什么关闭按钮不起作用?
  详细解决方案

为什么关闭按钮不起作用?

热度:247   发布时间:2007-11-18 15:13:47.0
为什么关闭按钮不起作用?

package applet;
import java.awt.*;
import java.awt.event.*;
//我添加了事件处理程序,但为什么关闭按钮还是不起作用?谢谢
public class MyTextArea extends Frame implements TextListener {
static Frame fr=new Frame("My new window!Superb!");
TextArea ta1,ta2;
public MyTextArea(){
setBounds(0,0,200,160);
String str1="Come on,boy!";
String str2="What a sunny day today!";
ta1=new TextArea(str1,10,6,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta2=new TextArea(str2,10,6,TextArea.SCROLLBARS_HORIZONTAL_ONLY);
setLayout(new FlowLayout(FlowLayout.LEFT));
ta1.addTextListener(this);
ta2.setEditable(false);
add(ta1);
add(ta2);
setVisible(true);
}

public void textValueChanged(TextEvent e){
ta2.setText(ta1.getText());
}
public static void main(String args[]){
new MyTextArea();

fr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);

}

}

搜索更多相关的解决方案: ONLY  按钮  Frame  awt  

----------------解决方案--------------------------------------------------------
以下是引用zzxwill在2007-11-18 15:13:47的发言:

package applet;
import java.awt.*;
import java.awt.event.*;
//我添加了事件处理程序,但为什么关闭按钮还是不起作用?谢谢
public class MyTextArea extends Frame implements TextListener {
//static Frame fr=new Frame("My new window!Superb!");
TextArea ta1,ta2;
public MyTextArea(){
setBounds(0,0,200,160);
String str1="Come on,boy!";
String str2="What a sunny day today!";
ta1=new TextArea(str1,10,6,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta2=new TextArea(str2,10,6,TextArea.SCROLLBARS_HORIZONTAL_ONLY);
setLayout(new FlowLayout(FlowLayout.LEFT));
ta1.addTextListener(this);
ta2.setEditable(false);
add(ta1);
add(ta2);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);

}
public void textValueChanged(TextEvent e){
ta2.setText(ta1.getText());
}
public static void main(String args[]){
new MyTextArea();

}

}


----------------解决方案--------------------------------------------------------

LS的意思是不能在构造器外面加监听么?我觉得应该可以吧~你看看这样改行不行,我在网吧没法测试
package applet;
import java.awt.*;
import java.awt.event.*;
//我添加了事件处理程序,但为什么关闭按钮还是不起作用?谢谢
public class MyTextArea extends Frame implements TextListener {
//static Frame fr=new Frame("My new window!Superb!");你这里创建的是一个普通的Frame而不是你的MyTextArea
TextArea ta1,ta2;
public MyTextArea(){
setBounds(0,0,200,160);
String str1="Come on,boy!";
String str2="What a sunny day today!";
ta1=new TextArea(str1,10,6,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta2=new TextArea(str2,10,6,TextArea.SCROLLBARS_HORIZONTAL_ONLY);
setLayout(new FlowLayout(FlowLayout.LEFT));
ta1.addTextListener(this);
ta2.setEditable(false);
add(ta1);
add(ta2);
setVisible(true);
}

public void textValueChanged(TextEvent e){
ta2.setText(ta1.getText());
}
public static void main(String args[]){
new MyTextArea().addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);

;

}


----------------解决方案--------------------------------------------------------
"LS的意思是不能在构造器外面加监听么?"
我没说不能在外面添加;反正有添加就行,在哪里无所谓;
----------------解决方案--------------------------------------------------------
还有一点,不要再用AWT了,尽量用SWING里面的组件吧
----------------解决方案--------------------------------------------------------
  相关解决方案