AWT(Abstract Window Toolkit)包括了很多类的接口,用于Java Application的GUI(Graphics User Interface图形用户界面)编程。
两种常用的Container:
Frame是Window的子类,由Frame或其子类创建的对象为一个窗体。
Frame的常用构造方法:
Frame()
Frame(String s) //创建标题栏为字符串s的窗口
import java.awt.*;
/**
* GUI(图形用户界面)
* Graphical User Interface(图形用户接口)
* 用图形的方式,来显示计算机操作的界面,这样更方便更直观.
*
* CLI
* Command Line User Interface(命令行用户接口)
* 就是常用的Dos命令行操作.
* 需要记忆一些常用的命令.操作更直观.
*
* 举例:
* 比如:创建文件夹,或者删除文件夹等
* md haha del haha
*
*
* Java的GUI提供的对象都存在 java.Awt 和 javax.Swing 两个包中.
*
* java.Awt:Abstract Window ToolKit(抽象 窗口工具包)
* 需要调用本地系统方法实现功能.属重量级控件 (跨平台不够强)
*
* java.Swing:在AWT的基础上,建立的一套图形界面系统,器重提供了更多的组件,
* 而且完全由java实现,增强了移植性,属于轻量级控件.(跨平台很好)
*
* java.swt: IBM 公司开发 Eclipse 用的组件工具 可以Eclipse网站下载后就可以使用了.
*
*
* 布局管理器
* 1)容器中的组件的排放方式,就是布局.
* 2)常见的布局管理器
* FlowLayout(流式布局管理器)
* 从左到右的顺序排列
* Panel默认的布局管理器
* BorderLayout(辩解布局管理器)
* 东 南 西 北 中
* Frame 默认的布局管理器
* 不指定布局方式,默认 满屏覆盖,在添加一个 也是 满屏覆盖
* GridLayout (网格布局管理器)
* 规则的矩阵
* CardLayout (卡片布局管理器)
* 选项卡
* GridBagLayout(网格包布局管理器)
* 非规则的矩阵
*
* 事件监听机制组成
* 事件源:
* 事件:Event
* 监听器:Listener
* 时间处理:(引发事件后处理方式)
*
* 事件源:就是awt包或者swing包中的那些图像界面组件.
* 事件:每个事件源都有自己特定的对应时间和共性时间.
* 监听器:可以出发某一个事件的动作都已经封装到监听器中.
*/
public class test{
public static void main( String args[]) {
Frame f = new Frame("My First Test");
f.setLocation(300, 300);
f.setSize( 170,100);
f.setBackground( Color.blue);
f.setResizable(false);
f.setVisible( true);
}
}
例如2:
import java.awt.*;
public class test {
public static void main(String args[]) {
MyFrame f1 = new MyFrame(100,100,200,200,Color.BLUE);
MyFrame f2 = new MyFrame(300,100,200,200,Color.YELLOW);
MyFrame f3 = new MyFrame(100,300,200,200,Color.GREEN);
MyFrame f4 = new MyFrame(300,300,200,200,Color.MAGENTA);
}
}
class MyFrame extends Frame{
static int id = 0;
MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame " + (++id));
setBackground(color);
setLayout(null);
setBounds(x,y,w,h);
setVisible(true);
}
}
import java.awt.*;
public class TestPanel {
public static void main(String args[]) {
Frame f = new Frame("Java Frame with Panel");
Panel p = new Panel(null);
f.setLayout(null);
f.setBounds(300,300,500,500);
f.setBackground(new Color(0,0,102));
p.setBounds(50,50,400,400);
p.setBackground(new Color(204,204,255));
f.add(p);
f.setVisible(true);
}
}
例如2:
import java.awt.*;
public class test{
public static void main( String args[]) {
new MyFrame2("MyFrameWithPanel",300,300,400,300);
}
}
class MyFrame2 extends Frame{
private Panel p1,p2,p3,p4;
MyFrame2(String s,int x,int y,int w,int h){
super(s);
setLayout(null);
p1 = new Panel(null); p2 = new Panel(null);
p3 = new Panel(null); p4 = new Panel(null);
p1.setBounds(0,0,w/2,h/2);
p2.setBounds(0,h/2,w/2,h/2);
p3.setBounds(w/2,0,w/2,h/2);
p4.setBounds(w/2,h/2,w/2,h/2);
p1.setBackground(Color.BLUE);
p2.setBackground(Color.GREEN);
p3.setBackground(Color.YELLOW);
p4.setBackground(Color.MAGENTA);
add(p1);add(p2);add(p3);add(p4);
setBounds(x,y,w,h);
setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class test {
public static void main(String args[]) {
Frame f = new Frame("Test");
Button b = new Button("Press Me!");
Monitor bh = new Monitor();
b.addActionListener(bh);
f.add(b,BorderLayout.CENTER);
f.pack(); //调整此窗口大小,以适应其组件的首先大小和布局
f.setVisible(true);
}
}
class Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("a button has been pressed");
}
}
java.awt.TextFieldl类用来创建文本框对象。
TextField有如下常用方法:
TextField事件监听
import java.awt.*;
import java.awt.event.*;
public class TFPassword {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TFFrame2();
}
}
class TFFrame2 extends Frame
{
TFFrame2()
{
TextField tf = new TextField();
add(tf);
tf.addActionListener(new TFActionListener2());
tf.setEchoChar('*');
pack();
setVisible(true);
}
}
class TFActionListener2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
TextField tf = (TextField)e.getSource();
System.out.println(tf.getText());
tf.setText("");
}
}
好处:
何时使用:
Graphics类 Paint方法
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new PaintFrame().launchFrame();
}
}
class PaintFrame extends Frame {
public void launchFrame() {
setBounds(200,200,640,480);
setVisible(true);
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(50, 50, 30, 30);
g.setColor(Color.green);
g.fillRect(80,80,40,40);
g.setColor(c);
}
}
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();
}
}