黑马程序员——Java—GUI编程

                                          黑马程序员——GUI编程-------------------------------------------------
 ---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! -----
package com.itheima.gui;


import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;


public class FrameDemo {


/**
* @param args
         * @hutian
*/
public static void main(String[] args) {

Frame f=new Frame("Myfram");//创建一个窗口

f.setSize(500,400);
f.setLocation(400, 200);

f.setBounds(400,200,500,400);
f.setLayout(new FlowLayout());//流的布局

Button bt=new Button("创建一个按钮");
f.add(bt);

f.addWindowFocusListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){

System.exit(0);//退出
}
});


//按钮创建监听事件
bt.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {
System.out.print("button ....run");
System.exit(0);//退出

}
});
f.setVisible(true);
System.out.println("over");
}


}
练习:
事件监听机制及案例:
package com.itheima.gui;


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;
import java.io.IOException;




public class MouseKeyDemo {


/**
* @param args
*/

private Frame f;
private TextField tf;
    private Button bt;
    
public static void main(String[] args) {


       new MouseKeyDemo();
}
 
    public MouseKeyDemo(){
    init();
    }
private void init(){
f=new Frame("演示鼠标和键盘监听");
f.setBounds(400,200,500,400);
f.setLayout(new FlowLayout());

tf=new TextField(40);
bt=new Button("一个按钮");

f.add(bt);
f.add(tf);

MyEvent();
f.setVisible(true);
}
   private void MyEvent(){
  //给文本添加键盘监听
  
  tf.addKeyListener(new KeyAdapter() {
  public void KeyPressed(KeyEvent e){
  
  System.out.print("Key..Run"+KeyEvent.getKeyText(e.getKeyCode())+"::::::"+e.getKeyCode());
  
  int code=e.getKeyCode();
  if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){
  System.out.print("必须是数字");
  e.consume();
  }
  if(e.isControlDown()&& e.getKeyCode()==KeyEvent.VK_ENTER){
  System.out.print("enter.....Run");
  }
  
  }
  
}); 
  f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){

     System.exit(0);//退出
}
});
//按钮创建监听事件
bt.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.print("action ....run");
}
});
  //创建鼠标监听事件
bt.addMouseListener(new MouseAdapter() {
private int count=1;

public void mouseEntered(MouseEvent e){
System.out.print("Mouse enter..."+count++);
tf.setText("Mouse enter..."+count++);

}
            public void mouseClicked(MouseEvent e){
            if(e.getClickCount()==2)
           
    tf.setText("Mouse click..."+count++);
             System.out.print("Mouse click..."+count++);
            System.out.print(e);
           
            }
});

   }
}

总结:用户图形界面的编程,各种元素有Java.Awt,使用的是两个核心包:Component 和Containter

 ---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! -----


你可能感兴趣的:(技术学习)