转自:http://takeme.iteye.com/blog/1876850
GUI(图形用户界面)
1 import java.awt.Button; 2 import java.awt.FlowLayout; 3 import java.awt.Frame; 4 import java.awt.event.WindowAdapter; 5 import java.awt.event.WindowEvent; 6 7 /** 8 * GUI(图形用户界面) 9 * Graphical User Interface(图形用户接口) 10 * 用图形的方式,来显示计算机操作的界面,这样更方便更直观. 11 * 12 * CLI 13 * Command Line User Interface(命令行用户接口) 14 * 就是常用的Dos命令行操作. 15 * 需要记忆一些常用的命令.操作更直观. 16 * 17 * 举例: 18 * 比如:创建文件夹,或者删除文件夹等 19 * md haha del haha 20 * 21 * 22 * Java的GUI提供的对象都存在 java.Awt 和 javax.Swing 两个包中. 23 * 24 * java.Awt:Abstract Window ToolKit(抽象 窗口工具包) 25 * 需要调用本地系统方法实现功能.属重量级控件 (跨平台不够强) 26 * 27 * java.Swing:在AWT的基础上,建立的一套图形界面系统,器重提供了更多的组件, 28 * 而且完全由java实现,增强了移植性,属于轻量级控件.(跨平台很好) 29 * 30 * java.swt: IBM 公司开发 Eclipse 用的组件工具 可以Eclipse网站下载后就可以使用了. 31 * 32 * 33 * 布局管理器 34 * 1)容器中的组件的排放方式,就是布局. 35 * 2)常见的布局管理器 36 * FlowLayout(流式布局管理器) 37 * 从左到右的顺序排列 38 * Panel默认的布局管理器 39 * BorderLayout(辩解布局管理器) 40 * 东 南 西 北 中 41 * Frame 默认的布局管理器 42 * 不指定布局方式,默认 满屏覆盖,在添加一个 也是 满屏覆盖 43 * GridLayout (网格布局管理器) 44 * 规则的矩阵 45 * CardLayout (卡片布局管理器) 46 * 选项卡 47 * GridBagLayout(网格包布局管理器) 48 * 非规则的矩阵 49 * 50 * 事件监听机制组成 51 * 事件源: 52 * 事件:Event 53 * 监听器:Listener 54 * 时间处理:(引发事件后处理方式) 55 * 56 * 事件源:就是awt包或者swing包中的那些图像界面组件. 57 * 事件:每个事件源都有自己特定的对应时间和共性时间. 58 * 监听器:可以出发某一个事件的动作都已经封装到监听器中. 59 */ 60 61 62 public class GuiDemo { 63 public static void main(String[] args) { 64 Frame f=new Frame("my awt"); 65 f.setSize(500,400); 66 f.setLocation(300,200); 67 f.setLayout(new FlowLayout()); 68 69 Button b=new Button("我是一个按钮"); 70 71 f.add(b); 72 73 f.addWindowListener(new MyWin()); 74 75 f.setVisible(true); 76 System.out.println("Hello world!"); 77 } 78 79 } 80 81 //因为接口WindowLinstener中的所有方法都被子类 WindowAdapter实现了,. 82 //并且覆盖了其中的所有方法,那么我们只能继承 WindowAdapter 覆盖我们的方法即可 83 class MyWin extends WindowAdapter{ 84 85 @Override 86 public void windowClosing(WindowEvent e) { 87 // TODO Auto-generated method stub 88 //System.out.println("Window closing"+e.toString()); 89 System.out.println("我关了"); 90 System.exit(0); 91 } 92 @Override 93 public void windowActivated(WindowEvent e) { 94 //每次获得焦点 就会触发 95 System.out.println("我活了"); 96 //super.windowActivated(e); 97 } 98 @Override 99 public void windowOpened(WindowEvent e) { 100 // TODO Auto-generated method stub 101 System.out.println("我开了"); 102 //super.windowOpened(e); 103 } 104 105 }
简单Frame
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 FrameDemo { 10 11 //定义该图像中所需要的组件的引用 12 private Frame f; 13 private Button but; 14 15 FrameDemo(){ 16 init(); 17 18 } 19 20 public void init(){ 21 22 f=new Frame("my freame"); 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 //加载一下窗体上的事件. 31 myEvent(); 32 //显示窗体 33 f.setVisible(true); 34 } 35 private void myEvent(){ 36 f.addWindowListener(new WindowAdapter(){ 37 @Override 38 public void windowClosing(WindowEvent e) { 39 // TODO Auto-generated method stub 40 //super.windowClosing(e); 41 System.exit(0); 42 } 43 }); 44 45 //让按钮具备退出程序的功能 46 /* 47 * 按钮就是事件源 48 * 那么选择那个监听器呢? 49 * 通过关闭窗体实例了解到,下个要知道那个组件具备什么样的特有监听器, 50 * 需要查看该组件对象的功能. 51 */ 52 //添加一个活动监听 53 but.addActionListener(new ActionListener() { 54 55 @Override 56 public void actionPerformed(ActionEvent e) { 57 // TODO Auto-generated method stub 58 System.out.println("退出, 按钮干的"); 59 System.exit(0); 60 } 61 }); 62 63 } 64 65 public static void main(String[] args) { 66 FrameDemo f=new FrameDemo(); 67 } 68 69 }
键盘和鼠标事件
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.ActionEvent; 6 import java.awt.event.ActionListener; 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 MouseAndKeyEvent { 15 16 private Frame f; 17 private Button but; 18 private TextField tf; 19 20 public MouseAndKeyEvent() { 21 init(); 22 } 23 24 private void init(){ 25 f=new Frame("me frame"); 26 f.setBounds(300, 200, 600, 500); 27 f.setLayout(new FlowLayout()); 28 29 tf=new TextField(20); 30 but=new Button("my Botton"); 31 32 f.add(tf); 33 f.add(but); 34 35 event(); 36 37 f.setVisible(true); 38 } 39 40 private void event(){ 41 f.addWindowListener(new WindowAdapter() { 42 @Override 43 public void windowClosing(WindowEvent e) { 44 // TODO Auto-generated method stub 45 System.exit(0); 46 } 47 48 }); 49 tf.addKeyListener(new KeyAdapter() { 50 public void keyPressed(KeyEvent e){ 51 int code=e.getKeyCode(); 52 if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){ 53 System.out.println(code+"....非法的输入"); 54 e.consume(); //不执行加入文本框. 55 } 56 57 } 58 59 }); 60 61 62 63 but.addActionListener(new ActionListener() { 64 65 @Override 66 public void actionPerformed(ActionEvent e) { 67 // TODO Auto-generated method stub 68 System.out.println("actionPerformed 活动一次"); 69 } 70 }); 71 72 but.addMouseListener(new MouseAdapter() { 73 private int count=0; 74 private int clickCount=1; 75 public void mouseEntered(MouseEvent e){ 76 System.out.println("鼠标进入到改组件"+count++); 77 } 78 79 public void mouseClicked(MouseEvent e){ 80 if(e.getClickCount()==2){ 81 System.out.println("双击动作"); 82 }else 83 System.out.println("点击动作"+clickCount++); 84 85 } 86 87 }); 88 //添加 键盘事件 89 but.addKeyListener(new KeyAdapter() { 90 91 public void keyPressed(KeyEvent e){ 92 System.out.println(e.getKeyChar()+"..."+e.getKeyCode()); 93 System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode()); 94 //enter 就退出 95 /*if(e.getKeyCode()==KeyEvent.VK_ENTER) 96 System. 97 exit(0);*/ 98 99 //ctrl + Enter 发送消息 100 if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){ 101 System.out.println("我要发送消息!"); 102 } 103 104 } 105 106 }); 107 } 108 109 public static void main(String[] args) { 110 // TODO Auto-generated method stub 111 new MouseAndKeyEvent(); 112 } 113 114 }
- import java.awt.Button;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class MouseAndKeyEvent {
- private Frame f;
- private Button but;
- private TextField tf;
- public MouseAndKeyEvent() {
- init();
- }
- private void init(){
- f=new Frame("me frame");
- f.setBounds(300, 200, 600, 500);
- f.setLayout(new FlowLayout());
- tf=new TextField(20);
- but=new Button("my Botton");
- f.add(tf);
- f.add(but);
- event();
- f.setVisible(true);
- }
- private void event(){
- f.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- // TODO Auto-generated method stub
- System.exit(0);
- }
- });
- tf.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e){
- int code=e.getKeyCode();
- if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){
- System.out.println(code+"....非法的输入");
- e.consume(); //不执行加入文本框.
- }
- }
- });
- but.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- System.out.println("actionPerformed 活动一次");
- }
- });
- but.addMouseListener(new MouseAdapter() {
- private int count=0;
- private int clickCount=1;
- public void mouseEntered(MouseEvent e){
- System.out.println("鼠标进入到改组件"+count++);
- }
- public void mouseClicked(MouseEvent e){
- if(e.getClickCount()==2){
- System.out.println("双击动作");
- }else
- System.out.println("点击动作"+clickCount++);
- }
- });
- //添加 键盘事件
- but.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e){
- System.out.println(e.getKeyChar()+"..."+e.getKeyCode());
- System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());
- //enter 就退出
- /*if(e.getKeyCode()==KeyEvent.VK_ENTER)
- System.
- exit(0);*/
- //ctrl + Enter 发送消息
- if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){
- System.out.println("我要发送消息!");
- }
- }
- });
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new MouseAndKeyEvent();
- }
- }