Java笔记(八)
GUI:
awt和swing:
java.awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属于重量级控件。
javax.swing:在awt的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属于轻量级控件。
创建图形化界面:
(1)创建Frame窗体;
(2)对窗体进行基本设置。如:大小、位置、布局;
(3)定义组件;
(4)将组件通过窗体的add方法添加到窗体中;
(5)让窗体显示,通过setVisible(true)。
事件监听机制的特点:
(1)事件源
(2)事件
(3)监听器
(4)事件处理
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不止一个)都封装到了监听器中。
以上三者,在Java中都定义好了,直接获取其对象来用就可以。我们只要对产生的动作进行处理。
1 import java.awt.Button; 2 import java.awt.FlowLayout; 3 import java.awt.Frame; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.awt.event.WindowAdapter; 7 import java.awt.event.WindowEvent; 8 9 public class Demo{ 10 //定义该图形中的所需组件的引用 11 private Frame f; 12 private Button b; 13 Demo(){ 14 init(); 15 } 16 public void init(){ 17 f = new Frame("my frame"); 18 19 //对frame进行基本设置 20 f.setBounds(300, 200, 300, 200); 21 f.setLayout(new FlowLayout()); 22 23 b = new Button("按钮"); 24 25 //将组件添加到frame中 26 f.add(b); 27 28 //加载一下窗体上的事件 29 myEvent(); 30 31 //显示窗体 32 f.setVisible(true); 33 } 34 private void myEvent(){ 35 f.addWindowListener(new WindowAdapter(){ 36 public void windowClosing(WindowEvent e){ 37 System.exit(0); 38 } 39 }); 40 41 //让按钮具备退出程序的功能 42 //此时按钮就是事件源,想要知道哪个组件具备什么样的特有监听器,需要查看该组件对象的功能,通过查阅button的描述, 43 //发现按钮支持一个特有监听addActionListener 44 b.addActionListener(new ActionListener(){ 45 public void actionPerformed(ActionEvent e){ 46 System.exit(0); 47 } 48 }); 49 } 50 public static void main(String[] args){ 51 new Demo(); 52 } 53 }
鼠标事件:
1 import java.awt.Button; 2 import java.awt.FlowLayout; 3 import java.awt.Frame; 4 import java.awt.event.MouseAdapter; 5 import java.awt.event.MouseEvent; 6 import java.awt.event.WindowAdapter; 7 import java.awt.event.WindowEvent; 8 9 public class Demo{ 10 private Frame f; 11 private Button b; 12 Demo(){ 13 init(); 14 } 15 public void init(){ 16 f = new Frame("my frame"); 17 18 f.setBounds(300, 200, 300, 200); 19 f.setLayout(new FlowLayout()); 20 21 b = new Button("按钮"); 22 23 f.add(b); 24 25 myEvent(); 26 27 f.setVisible(true); 28 } 29 private void myEvent(){ 30 f.addWindowListener(new WindowAdapter(){ 31 public void windowClosing(WindowEvent e){ 32 System.exit(0); 33 } 34 }); 35 b.addMouseListener(new MouseAdapter(){ 36 public void mouseEntered(MouseEvent e){ 37 System.out.println("鼠标进入该组件"); 38 } 39 40 public void mouseClicked(MouseEvent e){ 41 if(e.getClickCount() == 2) 42 System.out.println("双击动作"); 43 } 44 }); 45 } 46 public static void main(String[] args){ 47 new Demo(); 48 } 49 }
键盘事件:
1 import java.awt.Button; 2 import java.awt.FlowLayout; 3 import java.awt.Frame; 4 import java.awt.TextField; 5 import java.awt.event.KeyAdapter; 6 import java.awt.event.KeyEvent; 7 import java.awt.event.WindowAdapter; 8 import java.awt.event.WindowEvent; 9 10 public class Demo{ 11 private Frame f; 12 private Button b; 13 private TextField tf; 14 Demo(){ 15 init(); 16 } 17 public void init(){ 18 f = new Frame("my frame"); 19 20 f.setBounds(300, 200, 300, 200); 21 f.setLayout(new FlowLayout()); 22 23 tf = new TextField(20); 24 25 b = new Button("按钮"); 26 27 f.add(b); 28 f.add(tf); 29 30 myEvent(); 31 32 f.setVisible(true); 33 } 34 private void myEvent(){ 35 f.addWindowListener(new WindowAdapter(){ 36 public void windowClosing(WindowEvent e){ 37 System.exit(0); 38 } 39 }); 40 41 //添加键盘监听 42 b.addKeyListener(new KeyAdapter(){ 43 // public void keyPressed(KeyEvent e){ 44 // //按“ESC”键,程序退出 45 // if(e.getKeyCode() == KeyEvent.VK_ESCAPE) 46 // System.exit(0); 47 // } 48 49 //Ctrl+回车键,使程序退出 50 public void keyPressed(KeyEvent e){ 51 if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) 52 System.exit(0); 53 } 54 }); 55 56 //设置文本框,使它只能被输入数字 57 tf.addKeyListener(new KeyAdapter(){ 58 public void keyPressed(KeyEvent e){ 59 int code = e.getKeyCode(); 60 if(!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)){ 61 System.out.println(code + "...是非法的"); 62 e.consume(); 63 } 64 } 65 }); 66 } 67 public static void main(String[] args){ 68 new Demo(); 69 } 70 }
例:列出指定目录内容:
1 import java.awt.Button; 2 import java.awt.Dialog; 3 import java.awt.FlowLayout; 4 import java.awt.Frame; 5 import java.awt.Label; 6 import java.awt.TextArea; 7 import java.awt.TextField; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.awt.event.KeyAdapter; 11 import java.awt.event.KeyEvent; 12 import java.awt.event.WindowAdapter; 13 import java.awt.event.WindowEvent; 14 import java.io.File; 15 16 public class Demo{ 17 private Frame f; 18 private Button b; 19 private TextField tf; 20 private TextArea ta; 21 private Dialog d; 22 private Label label; 23 private Button okBut; 24 Demo(){ 25 init(); 26 } 27 public void init(){ 28 f = new Frame("练习"); 29 f.setBounds(300, 100, 600, 500); 30 f.setLayout(new FlowLayout()); 31 32 b = new Button("转到"); 33 34 tf = new TextField(60); 35 ta = new TextArea(25, 70); 36 37 //参数为true,表明必须先操作Dialog 38 d = new Dialog(f, "提示信息-self", true); 39 d.setBounds(400, 200, 240, 150); 40 d.setLayout(new FlowLayout()); 41 label = new Label(); 42 okBut = new Button("确定"); 43 d.add(label); 44 d.add(okBut); 45 46 f.add(tf); 47 f.add(b); 48 f.add(ta); 49 50 myEvent(); 51 52 f.setVisible(true); 53 } 54 public void myEvent(){ 55 f.addWindowListener(new WindowAdapter(){ 56 public void windowClosing(WindowEvent e){ 57 System.exit(0); 58 } 59 }); 60 61 b.addActionListener(new ActionListener(){ 62 public void actionPerformed(ActionEvent e){ 63 showDir(); 64 } 65 }); 66 67 d.addWindowListener(new WindowAdapter(){ 68 public void windowClosing(WindowEvent e){ 69 d.setVisible(false); 70 } 71 }); 72 73 okBut.addActionListener(new ActionListener(){ 74 public void actionPerformed(ActionEvent e){ 75 d.setVisible(false); 76 } 77 }); 78 79 tf.addKeyListener(new KeyAdapter(){ 80 public void keyPressed(KeyEvent e){ 81 if(e.getKeyCode() == KeyEvent.VK_ENTER){ 82 showDir(); 83 } 84 } 85 }); 86 } 87 88 private void showDir(){ 89 String dirPath = tf.getText(); 90 File file = new File(dirPath); 91 if(file.exists() && file.isDirectory()){ 92 tf.setText(""); 93 String[] names = file.list(); 94 for(String name : names){ 95 ta.append(name + "\r\n"); 96 } 97 } 98 else{ 99 String info = "您输入的信息: " + dirPath + "是错误的。请重新输入!"; 100 label.setText(info); 101 d.setVisible(true); 102 } 103 } 104 105 public static void main(String[] args) { 106 new Demo(); 107 } 108 }
菜单:
1 import java.awt.FlowLayout; 2 import java.awt.Frame; 3 import java.awt.Menu; 4 import java.awt.MenuBar; 5 import java.awt.MenuItem; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.awt.event.WindowAdapter; 9 import java.awt.event.WindowEvent; 10 11 public class Demo{ 12 private Frame f; 13 private MenuBar mb; 14 private Menu m, subMenu; 15 private MenuItem closeItem, subItem; 16 17 Demo(){ 18 init(); 19 } 20 21 public void init(){ 22 f = new Frame("my window"); 23 f.setBounds(300, 100, 500, 600); 24 f.setLayout(new FlowLayout()); 25 26 mb = new MenuBar(); 27 28 m = new Menu("文件"); 29 subMenu = new Menu("子菜单"); 30 closeItem = new MenuItem("退出"); 31 subItem = new MenuItem("子条目"); 32 33 subMenu.add(subItem); 34 m.add(subMenu); 35 m.add(closeItem); 36 mb.add(m); 37 f.setMenuBar(mb); 38 39 myEvent(); 40 41 f.setVisible(true); 42 } 43 44 public void myEvent(){ 45 f.addWindowListener(new WindowAdapter(){ 46 public void windowClosing(WindowEvent e){ 47 System.exit(0); 48 } 49 }); 50 51 closeItem.addActionListener(new ActionListener(){ 52 public void actionPerformed(ActionEvent e){ 53 System.exit(0); 54 } 55 }); 56 57 } 58 59 public static void main(String[] args) { 60 new Demo(); 61 } 62 }
如图:
打开保存文件:
1 import java.awt.FileDialog; 2 import java.awt.Frame; 3 import java.awt.Menu; 4 import java.awt.MenuBar; 5 import java.awt.MenuItem; 6 import java.awt.TextArea; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.awt.event.WindowAdapter; 10 import java.awt.event.WindowEvent; 11 import java.io.BufferedReader; 12 import java.io.BufferedWriter; 13 import java.io.File; 14 import java.io.FileReader; 15 import java.io.FileWriter; 16 import java.io.IOException; 17 18 public class Demo{ 19 private Frame f; 20 private Menu fileMenu; 21 private MenuItem openItem, saveItem, closeItem; 22 private MenuBar bar; 23 private FileDialog openD, saveD; 24 private TextArea ta; 25 private File file; 26 Demo(){ 27 init(); 28 } 29 30 public void init(){ 31 f = new Frame("练习"); 32 f.setBounds(300, 100, 650, 600); 33 34 bar = new MenuBar(); 35 ta = new TextArea(); 36 openItem = new MenuItem("打开"); 37 saveItem = new MenuItem("保存"); 38 closeItem = new MenuItem("退出"); 39 fileMenu = new Menu("文件"); 40 41 fileMenu.add(openItem); 42 fileMenu.add(saveItem); 43 fileMenu.add(closeItem); 44 bar.add(fileMenu); 45 f.setMenuBar(bar); 46 f.add(ta); 47 48 openD = new FileDialog(f, "打开文件", FileDialog.LOAD); 49 saveD = new FileDialog(f, "保存文件", FileDialog.SAVE); 50 51 myEvent(); 52 53 f.setVisible(true); 54 } 55 56 public void myEvent(){ 57 //窗口关闭 58 f.addWindowListener(new WindowAdapter(){ 59 public void windowClosing(WindowEvent e){ 60 System.exit(0); 61 } 62 }); 63 64 //点击“退出”条目,退出程序 65 closeItem.addActionListener(new ActionListener(){ 66 public void actionPerformed(ActionEvent e){ 67 System.exit(0); 68 } 69 }); 70 71 //打开选中的文件,显示在TextArea中 72 openItem.addActionListener(new ActionListener(){ 73 public void actionPerformed(ActionEvent e){ 74 openD.setVisible(true); 75 String dirPath = openD.getDirectory(); 76 String fileName = openD.getFile(); 77 if(dirPath == null || fileName == null) 78 return; 79 ta.setText(""); 80 file = new File(dirPath, fileName); 81 try{ 82 BufferedReader bufr = new BufferedReader(new FileReader(file)); 83 String line = null; 84 while((line = bufr.readLine()) != null){ 85 ta.append(line + "\r\n"); 86 } 87 }catch(IOException ex){ 88 throw new RuntimeException("读取失败!"); 89 } 90 91 } 92 }); 93 94 //保存文件 95 saveItem.addActionListener(new ActionListener(){ 96 public void actionPerformed(ActionEvent e){ 97 if(file == null){ 98 saveD.setVisible(true); 99 String dirPath = openD.getDirectory(); 100 String fileName = openD.getFile(); 101 if(dirPath == null || fileName == null) 102 return; 103 file = new File(dirPath, fileName); 104 } 105 try{ 106 BufferedWriter bufw = new BufferedWriter(new FileWriter(file)); 107 String s = ta.getText(); 108 bufw.write(s); 109 bufw.flush(); 110 bufw.close(); 111 }catch(IOException ex){ 112 throw new RuntimeException("保存失败!"); 113 } 114 } 115 }); 116 117 } 118 119 public static void main(String[] args) { 120 new Demo(); 121 } 122 }