java GUI 图形界面编程

转:http://takeme.iteye.com/blog/1876850

GUI(图形用户界面) 

Java代码   收藏代码
  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. }  


键盘和鼠标事件  
Java代码   收藏代码
  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(300200600500);  
  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. }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值