练习1:
View Code
1 1 import javax.swing.*; 2 2 import java.awt.*; 3 3 import java.awt.event.*; 4 4 public class MyWindowEvent 5 5 { 6 6 private Frame f; 7 7 private Button bt; 8 8 private TextField tf; 9 9 private TextArea ta; 10 10 public MyWindowEvent() 11 11 { 12 12 init(); 13 13 } 14 14 private void init() 15 15 { 16 16 f = new Frame("My window"); 17 17 bt = new Button("转到"); 18 18 tf = new TextField(40); 19 19 ta = new TextArea(10,50); 20 20 21 21 f.setLayout(new FlowLayout(FlowLayout.LEFT)); 22 22 f.setBounds(300,200,400,300); 23 23 24 24 f.add(tf); 25 25 f.add(bt); 26 26 f.add(ta); 27 27 28 28 myEvent(); 29 29 30 30 f.setVisible(true); 31 31 } 32 32 public void myEvent() 33 33 { 34 34 f.addWindowListener(new WindowAdapter() 35 35 { 36 36 public void windowClosing(WindowEvent e) 37 37 { 38 38 System.exit(0); 39 39 } 40 40 }); 41 41 } 42 42 43 43 public static void main(String[] agrs) 44 44 { 45 45 new MyWindowEvent(); 46 46 } 47 47 }
练习2:
View Code
1 1 import javax.swing.*; 2 2 import java.awt.*; 3 3 import java.awt.event.*; 4 4 public class MyWindowEvent2 5 5 { 6 6 private Frame f; 7 7 private Button bt; 8 8 private TextField tf; 9 9 private TextArea ta; 10 10 public MyWindowEvent2() 11 11 { 12 12 init(); 13 13 } 14 14 private void init() 15 15 { 16 16 f = new Frame("My window"); 17 17 bt = new Button("转到"); 18 18 tf = new TextField(40); 19 19 ta = new TextArea(10,50); 20 20 21 21 f.setLayout(new FlowLayout(FlowLayout.LEFT)); 22 22 f.setBounds(300,200,400,300); 23 23 24 24 f.add(tf); 25 25 f.add(bt); 26 26 f.add(ta); 27 27 28 28 myEvent(); 29 29 30 30 f.setVisible(true); 31 31 } 32 32 public void myEvent() 33 33 { 34 34 f.addWindowListener(new WindowAdapter() 35 35 { 36 36 public void windowClosing(WindowEvent e) 37 37 { 38 38 System.exit(0); 39 39 } 40 40 }); 41 41 bt.addActionListener(new ActionListener() 42 42 { 43 43 public void actionPerformed(ActionEvent a) 44 44 { 45 45 String text = tf.getText();//返回此文本组件表示的文本。 46 46 ta.setText(text);// 将此文本组件显示的文本设置为指定文本。 47 47 48 48 tf.setText("");//清空输入中的内容 49 49 } 50 50 }); 51 51 } 52 52 53 53 public static void main(String[] agrs) 54 54 { 55 55 new MyWindowEvent2(); 56 56 } 57 57 }
练习3:获取文件目录
View Code
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.io.*; 5 public class MyWindowEvent3 6 { 7 private Frame f; 8 private Button bt; 9 private TextField tf; 10 private TextArea ta; 11 public MyWindowEvent3() 12 { 13 init(); 14 } 15 private void init() 16 { 17 f = new Frame("My window"); 18 bt = new Button("转到"); 19 tf = new TextField(40); 20 ta = new TextArea(10,50); 21 22 f.setLayout(new FlowLayout(FlowLayout.LEFT)); 23 f.setBounds(300,200,400,300); 24 25 f.add(tf); 26 f.add(bt); 27 f.add(ta); 28 29 myEvent(); 30 31 f.setVisible(true); 32 } 33 public void myEvent() 34 { 35 f.addWindowListener(new WindowAdapter() 36 { 37 public void windowClosing(WindowEvent e) 38 { 39 System.exit(0); 40 } 41 }); 42 bt.addActionListener(new ActionListener() 43 { 44 public void actionPerformed(ActionEvent a) 45 { 46 String dirPath = tf.getText(); 47 File dir = new File(dirPath); 48 if(dir.exists() && dir.isDirectory() )//判断dir是否存在并且是个目录 49 { 50 ta.setText(""); 51 String[] names = dir.list(); 52 for(String name : names)//foreach循环 53 { 54 ta.append(name+"\n"); 55 } 56 } 57 else 58 { 59 ta.setText(""); 60 ta.setText("不存在!"); 61 } 62 } 63 }); 64 } 65 66 public static void main(String[] agrs) 67 { 68 new MyWindowEvent3(); 69 } 70 }