1、GUI
1.1、
java.awt: Abstract Window ToolKit(抽象窗口工具包)需要调用本地系统方法实现功能。跨平台性稍不好,属重量级控件
javax.Swing: 在AWT基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由java实现,增强了移植性,属轻量级控件
1 package learn; 2 3 import java.awt.Button; 4 import java.awt.FlowLayout; 5 import java.awt.Frame; 6 import java.awt.event.WindowAdapter; 7 import java.awt.event.WindowEvent; 8 import java.awt.event.WindowListener; 9 /* 10 * 创建图形化界面 11 * 1、创建frame窗体 12 * 2、对窗体进行基本设置 13 * 比如大小,位置,布局, 14 * 3、定义组件 15 * 4、将组件通过窗体的add方法添加到窗体中 16 * 5、让窗体显示,通过setVisible(true) 17 * 18 * 事件监听机制特点 19 * 1、事件源 20 * 2、事件 21 * 3、监听器 22 * 4、事件处理 23 * 24 * 事件源:就是AWT包或者swing包中的那些图形界面组件 25 * 事件:每一个事件源都有特有的对应事件和共性事件 26 * 27 * 监听器:将可以触发一个事件的动作(不只一个动作)都已经封装到了监听器中 28 * 以上三者,在java中都已经定义好了 29 * 直接获取其对象来用就可以了。 30 * 我们要做的事情是,就是对产生的动作进行处理 31 * 32 * */ 33 public class AwtDemo { 34 public static void main(String[] args) { 35 Frame f = new Frame("my awt"); 36 f.setSize(500,400); 37 f.setLocation(300,200); 38 f.setLayout(new FlowLayout()); 39 Button b = new Button("我是一个按钮"); 40 f.add(b); 41 // f.addWindowListener(new Mywin()); 42 //内部类 43 f.addWindowListener(new WindowAdapter() 44 { 45 public void windowClosing(WindowEvent e) 46 { 47 System.out.println("我关"); 48 System.exit(0); 49 } 50 } 51 ); 52 //设可见边框 53 f.setVisible(true); 54 // System.out.println("helloworld"); 55 } 56 } 57 //class MyWin implements WindowListener 58 //{ 59 // //覆盖7个方法,可只需要关闭 60 // //其他没用到的也得覆写 61 //} 62 //因为WindowListener的子类WindowAdapter已经实现了WindowListener接口 63 //并覆盖了其中的所有方法,那么我只要继承WindowAdapter覆盖我需要的方法既可 64 class Mywin extends WindowAdapter 65 { 66 public void windowClosing(WindowEvent e) 67 { 68 System.out.println("WindowClosing"); 69 System.exit(0); 70 } 71 }
1.2、按钮的监听
1 package learn; 2 3 import java.awt.Button; 4 import java.awt.FlowLayout; 5 import java.awt.Frame; 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 FrameDemo { 12 //定义图形中所需的组件的引用 13 private Frame f; 14 private Button but; 15 FrameDemo() 16 { 17 init(); 18 } 19 public void init() 20 { 21 f =new Frame("myframe"); 22 //对frame进行基本设置 23 f.setBounds(300,100,600,500); 24 f.setLayout(new FlowLayout()); 25 26 but = new Button("my button"); 27 //将组件添加到frame中 28 f.add(but); 29 //加载一下窗体上时间 30 myEvent(); 31 //显示窗体 32 f.setVisible(true); 33 } 34 private void myEvent() 35 { 36 f.addWindowListener(new WindowAdapter() 37 {public void windowClosing(WindowEvent e) 38 { 39 System.exit(0); 40 } 41 }); 42 //让按钮具备退出程序功能 43 //按钮就是事件源 44 //通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器 45 //需要查看该组件对象的功能 46 //通过查阅Buffon的描述,发现按钮支持一个特有监听addActionListener 47 but.addActionListener(new ActionListener() 48 { 49 public void actionPerformed(ActionEvent e) 50 { 51 System.out.println("退出,按钮干的"); 52 System.exit(0); 53 } 54 }); 55 } 56 57 public static void main(String[] args) { 58 59 new FrameDemo(); 60 } 61 }
1.3、鼠标键盘监听
if(e.getClickCount()==2)//监听双击
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//组合键
e.consume();//屏蔽进
1 package learn; 2 3 import java.awt.Button; 4 import java.awt.FlowLayout; 5 import java.awt.Frame; 6 import java.awt.TextField; 7 import java.awt.event.KeyAdapter; 8 import java.awt.event.KeyEvent; 9 import java.awt.event.MouseAdapter; 10 import java.awt.event.MouseEvent; 11 import java.awt.event.WindowAdapter; 12 import java.awt.event.WindowEvent; 13 14 public class MouseAndKey { 15 16 //定义图形中所需的组件的引用 17 private Frame f; 18 private Button but; 19 private TextField tf; 20 MouseAndKey() 21 { 22 init(); 23 } 24 public void init() 25 { 26 f =new Frame("myframe"); 27 28 f.setBounds(300,100,600,500); 29 f.setLayout(new FlowLayout()); 30 tf = new TextField(20); 31 but = new Button("my button"); 32 33 f.add(tf); 34 f.add(but); 35 36 myEvent(); 37 38 f.setVisible(true); 39 } 40 private void myEvent() 41 { 42 f.addWindowListener(new WindowAdapter() 43 {public void windowClosing(WindowEvent e) 44 { 45 System.exit(0); 46 } 47 }); 48 tf.addKeyListener(new KeyAdapter() 49 { 50 public void keyPressed(KeyEvent e) 51 { 52 int code = e.getKeyCode(); 53 if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)) 54 { 55 System.out.println("code是非法"); 56 //屏蔽进 57 e.consume(); 58 } 59 60 } 61 }); 62 //键盘监听 63 but.addKeyListener(new KeyAdapter() 64 { 65 public void keyPressed(KeyEvent e) 66 { 67 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER) 68 System.out.println("ctrl+enter"); 69 //esc 70 // if(e.getKeyCode()==KeyEvent.VK_ESCAPE) 71 // System.exit(0); 72 // System.out.println(KeyEvent.getKeyText(e.getKeyChar())+"......."+e.getKeyCode()); 73 } 74 }); 75 // but.addActionListener(new ActionListener() 76 // { 77 // public void actionPerformed(ActionEvent e) 78 // { 79 // System.out.println("action ok"); 80 // } 81 // } 82 // ); 83 but.addMouseListener(new MouseAdapter() 84 { 85 private int count = 1; 86 private int clickCount=1; 87 public void mouseEntered(MouseEvent e) 88 { 89 System.out.println("鼠标进入到该组件"+count++); 90 } 91 public void mouseClicked(MouseEvent e) 92 { 93 if(e.getClickCount()==2) 94 System.out.println("双击动作"+clickCount++); 95 } 96 }); 97 } 98 public static void main(String[] args) { 99 new MouseAndKey(); 100 } 101 }
1.4、列出制定目录内容&弹窗
1 package learn; 2 3 import java.awt.Button; 4 import java.awt.Dialog; 5 import java.awt.FlowLayout; 6 import java.awt.Frame; 7 import java.awt.Label; 8 import java.awt.TextArea; 9 import java.awt.TextField; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 import java.awt.event.KeyAdapter; 13 import java.awt.event.KeyEvent; 14 import java.awt.event.WindowAdapter; 15 import java.awt.event.WindowEvent; 16 import java.io.File; 17 18 public class MyWindowDemo { 19 private Frame f; 20 private TextField tf; 21 private Button but; 22 private TextArea ta; 23 private Dialog d; 24 private Label lab; 25 private Button okBut; 26 MyWindowDemo() 27 { 28 init(); 29 } 30 public void init() 31 { 32 f = new Frame("my window"); 33 f.setBounds(300,100,600,500); 34 //流布局,默认布局是边界布局,且居中 35 f.setLayout(new FlowLayout()); 36 tf = new TextField(30); 37 but = new Button("转到"); 38 ta = new TextArea(25,70); 39 40 d = new Dialog(f,"提示信息-self",true); 41 d.setBounds(400, 200, 240, 150); 42 d.setLayout(new FlowLayout()); 43 lab = new Label(); 44 okBut = new Button("确定"); 45 46 d.add(lab); 47 d.add(okBut); 48 49 50 f.add(tf); 51 f.add(but); 52 f.add(ta); 53 myEvent(); 54 f.setVisible(true); 55 56 57 } 58 private void myEvent() 59 { 60 okBut.addActionListener(new ActionListener() 61 { 62 public void actionPerformed(ActionEvent e) 63 { 64 d.setVisible(false); 65 } 66 }); 67 f.addWindowListener(new WindowAdapter() 68 { 69 public void windowClosing(WindowEvent e) 70 { 71 d.setVisible(false); 72 } 73 }); 74 75 tf.addKeyListener(new KeyAdapter() 76 { 77 public void keyPressed(KeyEvent e) 78 { 79 if(e.getKeyCode()==KeyEvent.VK_ENTER) 80 showDir(); 81 } 82 }); 83 but.addActionListener(new ActionListener() 84 { 85 public void actionPerformed(ActionEvent e) 86 { 87 showDir(); 88 } 89 }); 90 f.addWindowListener(new WindowAdapter() 91 { 92 public void windowClosing(WindowEvent e) 93 { 94 System.exit(0); 95 } 96 }); 97 } 98 private void showDir() 99 { 100 String dirPath =tf.getText(); 101 File dir = new File(dirPath); 102 if(dir.exists()&&dir.isDirectory()) 103 { 104 ta.setText(""); 105 String[] names = dir.list(); 106 for(String name:names) 107 { 108 ta.append(name+" "); 109 } 110 } 111 else 112 { 113 String info = "您输入的信息:"+dirPath+"是错误的,请重输"; 114 lab.setText(info); 115 d.setVisible(true); 116 } 117 } 118 public static void main(String[] args) { 119 new MyWindowDemo(); 120 } 121 122 }
1.5、菜单
打开,保存。
1 package learn; 2 3 import java.awt.FileDialog; 4 import java.awt.Frame; 5 import java.awt.Menu; 6 import java.awt.MenuBar; 7 import java.awt.MenuItem; 8 import java.awt.TextArea; 9 import java.awt.event.ActionEvent; 10 import java.awt.event.ActionListener; 11 import java.awt.event.WindowAdapter; 12 import java.awt.event.WindowEvent; 13 import java.io.BufferedReader; 14 import java.io.BufferedWriter; 15 import java.io.File; 16 import java.io.FileReader; 17 import java.io.FileWriter; 18 import java.io.IOException; 19 20 public class MyMenuDemo { 21 private Frame f; 22 private MenuBar bar; 23 private TextArea ta; 24 private Menu fileMenu; 25 private MenuItem openItem,saveItem,closeItem; 26 private FileDialog openDia,saveDia; 27 private File file; 28 29 30 MyMenuDemo() 31 { 32 init(); 33 } 34 public void init() 35 { 36 f = new Frame("my window"); 37 f.setBounds(300,100,650,600); 38 39 40 bar = new MenuBar(); 41 42 ta = new TextArea(); 43 44 fileMenu = new Menu("文件"); 45 46 47 openItem = new MenuItem("打开"); 48 saveItem = new MenuItem("保存"); 49 closeItem = new MenuItem("退出"); 50 51 52 fileMenu.add(openItem); 53 fileMenu.add(saveItem); 54 fileMenu.add(closeItem); 55 bar.add(fileMenu); 56 57 f.setMenuBar(bar); 58 openDia = new FileDialog(f,"我要打开",FileDialog.LOAD); 59 saveDia = new FileDialog(f,"我要打开",FileDialog.SAVE); 60 61 f.add(ta); 62 myEvent(); 63 f.setVisible(true); 64 65 } 66 private void myEvent() 67 { 68 saveItem.addActionListener(new ActionListener() 69 { 70 public void actionPerformed(ActionEvent e) 71 { 72 //文件不存在才建文件 73 if(file == null) 74 { 75 saveDia.setVisible(true); 76 String dirPath = saveDia.getDirectory(); 77 String fileName = saveDia.getFile(); 78 if(dirPath==null||fileName==null) 79 return ; 80 81 file = new File(dirPath,fileName); 82 } 83 try 84 { 85 BufferedWriter bufw = new BufferedWriter(new FileWriter(file)); 86 String text =ta.getText(); 87 bufw.write(text); 88 //bufw.flush(); 89 bufw.close(); 90 } 91 catch(IOException e3) 92 { 93 throw new RuntimeException(); 94 } 95 } 96 }); 97 openItem.addActionListener(new ActionListener() 98 { 99 public void actionPerformed(ActionEvent e) 100 { 101 openDia.setVisible(true); 102 String dirPath = openDia.getDirectory(); 103 String fileName = openDia.getFile(); 104 System.out.println(dirPath+"::"+fileName); 105 if(dirPath==null||fileName==null) 106 return ; 107 ta.setText(""); 108 File file = new File(dirPath,fileName); 109 try 110 { 111 BufferedReader bufr = new BufferedReader(new FileReader(file)); 112 String line = null; 113 while((line=bufr.readLine())!=null) 114 { 115 ta.append(line+" "); 116 } 117 bufr.close(); 118 } 119 catch(IOException e1) 120 { 121 throw new RuntimeException("读取失败"); 122 } 123 } 124 }); 125 closeItem.addActionListener(new ActionListener() 126 { 127 public void actionPerformed(ActionEvent e) 128 { 129 System.exit(0); 130 } 131 }); 132 f.addWindowListener(new WindowAdapter() 133 { 134 public void windowClosing(WindowEvent e) 135 { 136 System.exit(0); 137 } 138 }); 139 } 140 141 public static void main(String[] args) { 142 new MyMenuDemo(); 143 } 144 }