主要概括为一个面板,两大窗口,三大监听器,四大事件,五大布局
一个面板(Panel)
package AWT;
/*
* 一个面板
*/
import java.awt.Frame;
public class Example01 {
public static void main(String[] args) {
Frame f=new Frame("我的窗口");
f.setSize(400,300);
f.setLocation(300, 200);
f.setVisible(true);
}
}
两大窗口(Frame和Dialog)
三大监听器(继承WindowListener接口,适配器,匿名内部类)
继承WindowListener接口:
package AWT;
import java.awt.*;
import java.awt.event.*;
public class Example08 {
public static void main(String[] args) {
Frame f=new Frame();
f.setSize(400,300);
f.setLocation(300, 400);
f.setVisible(true);
MyWindowListener mw=new MyWindowListener();
f.addWindowListener(mw);
}
}
class MyWindowListener implements WindowListener{
public void windowClosing(WindowEvent e) {
Window window =e.getWindow();
window.setVisible(false);
window.dispose();
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed (WindowEvent e) {
}public void windowDeactivated(WindowEvent e) {
}public void windowDeiconified(WindowEvent e) {
}public void windowIconified(WindowEvent e) {
}public void windowOpened(WindowEvent e) {
}
}
适配器:
package AWT;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* 窗体事件
*
*/
public class Example11 {
public static void main(String[] args) {
final Frame f= new Frame("我的窗体");
f.setVisible(true);
f.setLocation(300, 300);
f.setSize(400, 300);
//使用内部类创建WindowListener实例对象,监听窗体时间
f.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体已打开");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
((Window)e.getComponent()).dispose();
System.out.println("窗体正在关闭");
// Window window =(Window)e.getComponent();
// window.dispose();
// }
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体已关闭");
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体图标化事件");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体取消图标化事件");
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体激活事件");
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体停用事件");
}
});
}
}
四大事件(窗体事件、鼠标事件、键盘事件、动作事件)
package AWT;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* 窗体事件
*
*/
public class Example11 {
public static void main(String[] args) {
final Frame f= new Frame("我的窗体");
f.setVisible(true);
f.setLocation(300, 300);
f.setSize(400, 300);
//使用内部类创建WindowListener实例对象,监听窗体时间
f.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体已打开");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
((Window)e.getComponent()).dispose();
System.out.println("窗体正在关闭");
// Window window =(Window)e.getComponent();
// window.dispose();
// }
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体已关闭");
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体图标化事件");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体取消图标化事件");
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体激活事件");
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗体停用事件");
}
});
}
}
鼠标事件:
package AWT;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author 鼠标事件
*
*/
public class Example12 {
public static void main(String[] args) {
final Frame f = new Frame("WindowEvent");
//设置窗体布局
f.setVisible(true);
f.setLocation(300, 300);
f.setSize(300, 200);
f.setLayout(new FlowLayout());
Button btn = new Button ("按钮");
f.add(btn);
f.addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent e) {
Window w = (Window)e.getComponent();
w.dispose();
}
});
//为按钮添加鼠标事件监听器
//内部类进行写
btn.addMouseListener(new MouseListener() {
@Override //放开
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseReleased - 鼠标放开事件");
}
@Override //按下
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("pressed");
}
@Override //移出
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("exited");
}
@Override //进入
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("entered");
}
@Override //单击
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton()==e.BUTTON1) {
System.out.println("鼠标左击事件");
}
if(e.getButton()==e.BUTTON3) {
System.out.println("鼠标右击事件");
}
if(e.getButton()==e.BUTTON2) {
System.out.println("鼠标单击事件");
}
// System.out.println("mouseClicked-鼠标完成单击事件");
}
});
}
}
键盘事件:
package AWT;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.Window;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 键盘事件
*/
public class Example13 {
public static void main(String[] args) {
Frame f = new Frame("键盘事件");
f.setLayout(new FlowLayout());
f.setLocation(300, 300);
f.setSize(400, 300);
TextField textField =new TextField(30); //创建一个文本框
f.add(textField);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent e) {
((Window)e.getComponent()).dispose();;
// Window window =(Window)e.getComponent();
// window.dispose();
}
});
//为文本框添加键盘事件监听器
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();//得到键值对应的整数值
String s= KeyEvent.getKeyText(KeyCode);//将整数值专化为字符串描述
char ss =e.getKeyChar();
System.out.println(s);
System.out.println(ss);
System.out.println(KeyCode);
}
});
}
}
AWT绘图
(制作验证码)
package AWT;
import java.awt.*;
import java.util.Random;
public class Example14 {
public static void main(String[] args) {
final Frame frame=new Frame("验证码");
final Panel panel =new MyPanel();
frame.add(panel);
frame.setSize(200, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class MyPanel extends Panel{
public void paint(Graphics g) {
int width=160;
int height=40;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
g.drawRect(0, 0, width, height);
Random r =new Random();
for(int i=0;i<100;i++) {
int x=r.nextInt(width-2);
int y=r.nextInt(height-2);
g.drawOval(x, y, 2, 2);
}
g.setFont(new Font("宋体",Font.BOLD,30));
g.setColor(Color.blue);
//产生随机数
char[]chars=("01234567890qwertyuiopasdfgh"+"DFGHJKL").toCharArray();
StringBuilder sb=new StringBuilder();
for(int i=0;i<4;i++) {
int pos =r.nextInt(chars.length);
char c=chars[pos];
sb.append(c+" ");
}
g.drawString(sb.toString(), 20, 30);
}
}
五大布局(FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout)
FlowLayout流式布局:
package AWT;
import java.awt.*;
public class Example02 {
public static void main(String[] args) {
final Frame f= new Frame("Flowlayout");
f.setLayout(new FlowLayout(FlowLayout.LEFT,50,50));
f.setSize(220,300);
f.setLocation(300,200);
f.add(new Button("第1"));
f.add(new Button("第2"));
f.add(new Button("第3"));
f.add(new Button("第4"));
f.add(new Button("第5"));
f.setVisible(true);
}
}
BorderLayout边界布局
package AWT;
import java.awt.*;
public class Example03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame("borderLayout");
f.setLayout(new BorderLayout());
f.setSize(300,200);
f.setLocation(300,200);
f.setVisible(true);
Button but1 =new Button("东部");
Button but2 =new Button("西部");
Button but3 =new Button("南部");
Button but4 =new Button("北部");
Button but5 =new Button("中部");
f.add(but1,BorderLayout.EAST);
f.add(but2,BorderLayout.WEST);
f.add(but3,BorderLayout.NORTH);
f.add(but4,BorderLayout.SOUTH);
f.add(but5,BorderLayout.CENTER);
}
}
GridLayout网格布局
package AWT;
import java.awt.*;
public class Example04 {
public static void main(String[] args) {
Frame f=new Frame("GridLayout");
f.setLayout(new GridLayout(3,3));
f.setSize(300,200);
f.setLocation(300,300);
for(int i=0;i<=9;i++){
Button btn=new Button("btn"+i);
f.add(btn);
}
f.setVisible(true);
}
}
GridBagLayout网格包布局
package AWT;
import java.awt.*;
public class Example05 {
class Layout extends Frame{
public Layout (String title) {
GridBagLayout layout =new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();//设置相关属性;
this.setLayout(layout);
c.fill=GridBagConstraints.BOTH;//可以拉伸
c.weightx=1;
c.weighty=1;
this.addComponent("btn1",layout,c);
this.addComponent("btn2", layout, c);
this.addComponent("btn3", layout, c);
c.gridwidth=GridBagConstraints.REMAINDER;
this.addComponent("btn4", layout, c);
c.weightx=0;
c.weighty=0;
this.addComponent("btn5", layout, c);
c.gridwidth=1;
this.addComponent("btn6", layout, c);
c.gridwidth=GridBagConstraints.REMAINDER;
this.addComponent("btn7", layout, c);
c.gridheight=2;
c.gridwidth=1;
c.weightx=2;
c.weighty=2;
this.addComponent("btn8", layout, c);
c.gridheight=GridBagConstraints.REMAINDER;
c.gridheight=1;
this.addComponent("btn9", layout, c);
this.addComponent("btn10", layout, c);
this.setTitle(title);
this.pack();
this.setVisible(true);
}
private void addComponent(String name, GridBagLayout layout, GridBagConstraints c) {
// TODO Auto-generated method stub
Button bt=new Button(name);
layout.setConstraints(bt, c);
this.add(bt);
}
}private void mian() {
// TODO Auto-generated method stub
new Layout("GridBagLayout");
}
}
CardLayout卡片布局
package AWT;
import java.awt.*;
import java.awt.event.*;
public class Example06 {
class Cardlayout extends Frame implements ActionListener{
Panel cardPanel=new Panel();//定义Pane面板放置卡片;
Panel controlpaPanel =new Panel();
Button nextbutton,preButton;
public Cardlayout() {
setSize(300,200);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClsing (WindowEvent e) {
Cardlayout.this.dispose();
}
});
cardPanel.setLayout(getLayout());
cardPanel.add(new Label("第一个界面",Label.CENTER));
cardPanel.add(new Label("第二个界面",Label.CENTER));
cardPanel.add(new Label("第三个界面",Label.CENTER));
nextbutton =new Button("下一张卡片");
preButton =new Button("上一张卡片");
nextbutton.addActionListener(this);
preButton.addActionListener(this);
controlpaPanel.add(preButton);
controlpaPanel.add(nextbutton);
this.add(cardPanel,BorderLayout.CENTER);
this.add(controlpaPanel,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==nextbutton) {
// cardLayout.next(cardPanel);
}if(e.getSource()==preButton) {
// cardLayout.previous(cardPanel);
}
}
}
public static void main(String[] args) {
// Cardlayout cardlayout=new Cardlayout();
}
}
附加自定义布局
package AWT;
import java.awt.*;
public class Example07 {
public static void main(String[] args) {
Frame f =new Frame("hello");
f.setLayout(null);
f.setSize(300,150);
Button btn1=new Button("press");
Button btn2=new Button("press");
btn1.setBounds(40, 60, 100, 30);
btn2.setBounds(140, 90, 100, 30);
f.add(btn1);
f.add(btn2);
f.setVisible(true);
}
}