回 帖 发 新 帖 刷新版面

主题:Done为什么关闭按钮不起作用?

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


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);
   }
  }
  );
  
}



}

回复列表 (共4个回复)

沙发

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();
  
  /*fr.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  }
  );*/
  
}



}

板凳

因为弹出的那个窗口不是fr,而是一个MyTextArea的实例,而且你没有对那个实例设置关闭操作

3 楼

忘了,把main函数写成这样就行啦
public static void main(String args[]) {
        MyTextArea aa = new MyTextArea();
        aa.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

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

    }


其实fr那一部分都可以去掉

4 楼

将窗口事件加到构造方法下面是个良好的习惯~~

我来回复

您尚未登录,请登录后再回复。点此登录或注册