最后一个方法public void windowClosing(WindowEvent e){}起什么作用?
import java.awt.*;import java.awt.event.*;
import java.io.*;
public class WinAdapter extends WindowAdapter implements ActionListener{
Frame fr;
Panel p1,p2,p3;
Label lb1,lb2,lsp1,lsp2;
TextField tf1,tf2;
Button bClear,bCopy,bClose;
public void go(){
fr=new Frame("MyFrame");
fr.setSize(350, 250);
fr.setLayout(new GridLayout(6,1,0,8));
p1=new Panel();
p2=new Panel();
p3=new Panel();
lb1=new Label("Source");
lb2=new Label("Target");
lsp1=new Label();
lsp2=new Label();
tf1=new TextField(15);
tf2=new TextField(15);
bClear=new Button("Clear");
bCopy=new Button("Copy");
bClose=new Button("Close");
bClear.addActionListener(this);
bCopy.addActionListener(this);
bClose.addActionListener(this);
p1.add(lb1);
p1.add(tf1);
p2.add(lb2);
p2.add(tf2);
p3.setLayout(new FlowLayout(FlowLayout.CENTER,40,0));
p3.add(bClear);
p3.add(bCopy);
p3.add(bClose);
fr.add(lsp1);
fr.add(p1);
fr.add(p2);
fr.add(lsp2);
fr.add(p3);
fr.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bClear){
tf1.setText("");
tf2.setText("");
}
if(e.getSource()==bCopy){
tf2.setText(tf1.getText());
}
if(e.getSource()==bClose){
System.exit(0);
}
}
public static void main(String[] args){
WinAdapter wa=new WinAdapter();
wa.go();
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Press enter key to exit");
try{
bf.readLine();
}catch(IOException e){
System.out.print("IOException");
}finally{
System.exit(0);
}
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
----------------解决方案--------------------------------------------------------
没看整个程序 最后一个方法 应该是个关闭窗口的
----------------解决方案--------------------------------------------------------
它是从WindowAdapter父类继承的方法,当一个窗口正被关闭时执行的方法。
API解释:Invoked when a window is in the process of being closed.
----------------解决方案--------------------------------------------------------