标签:
图形用户界面
1 Frame
package GuiDemo;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
public class FrameDemo {
public static void main(String[] args) {
Frame f=new Frame("my Frame");
f.setSize(500, 300);
f.setLocation(300, 400);
//设置布局
f.setLayout(new FlowLayout()); //流式
//f.setBounds(x, y, width, height);
Button b=new Button("一个按钮");
f.add(b);
f.setVisible(true);
}
}
2 事件监听机制
组成:事件源 事件 监听器 处理方式
package GuiDemo;
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;
public class FrameDemo {
public static void main(String[] args) {
Frame f=new Frame("my Frame");
f.setSize(500, 300);
f.setLocation(300, 400);
//设置布局
f.setLayout(new FlowLayout()); //流式
//f.setBounds(x, y, width, height);
Button b=new Button("一个按钮");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
System.out.println("close......");
System.exit(0);
}
});
f.add(b);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("closing...");
System.exit(0);
}
});
f.setVisible(true);
}
}
3 鼠标事件
package GuiDemo;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseAndKeyBorad {
private Frame f;
private TextField tf;
private Button b;
public MouseAndKeyBorad()
{
init();
}
private void init()
{
f=new Frame("演示键盘和鼠标监听");
f.setBounds(400, 200, 500, 400);
f.setLayout(new FlowLayout());
tf=new TextField(15);
b=new Button("一个按钮");
f.add(b);
f.add(tf);
myEvent();
f.setVisible(true);
}
private void myEvent() {
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//在按钮上添加一个鼠标监听器
b.addMouseListener(new MouseAdapter() {
private int count=0;
public void mouseEntered(MouseEvent e) {
//System.out.println("mouse enter..."+count++);
tf.setText("mouse enter..."+count++);
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2) //连续双击
{
tf.setText("double click");
}
}
});
}
public static void main(String[] args) {
new MouseAndKeyBorad();
}
}
4 键盘事件
package GuiDemo;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseAndKeyBorad {
private Frame f;
private TextField tf;
private Button b;
public MouseAndKeyBorad()
{
init();
}
private void init()
{
f=new Frame("演示键盘和鼠标监听");
f.setBounds(400, 200, 500, 400);
f.setLayout(new FlowLayout());
tf=new TextField(15);
b=new Button("一个按钮");
f.add(b);
f.add(tf);
myEvent();
f.setVisible(true);
}
private void myEvent() {
//给文本框添加键盘监听
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
//System.out.println("key run::::"+e.getKeyChar()+":::"+KeyEvent.getKeyText(e.getKeyCode()));
int code=e.getKeyCode();
/*if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9))
{
System.out.println("必须是数字");
e.consume();//阻止不满足if语句的事件产生
}
*/
/* if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
System.out.println("enter run");
}
*/
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
{
System.out.println("enter run");
}
}});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//在按钮上添加一个鼠标监听器
b.addMouseListener(new MouseAdapter() {
private int count=0;
public void mouseEntered(MouseEvent e) {
//System.out.println("mouse enter..."+count++);
tf.setText("mouse enter..."+count++);
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2) //连续双击
{
tf.setText("double click");
}
}
});
}
public static void main(String[] args) {
new MouseAndKeyBorad();
}
}
5 SWING演示装插件
标签:
原文地址:http://www.cnblogs.com/HJL085/p/5793992.html