1.AWT介绍
–元素:窗口,按钮,文本框
–awt包下: java.awt
组件和容器
1.Frame
创建一个窗口
import java.awt.*;
public class TestFrame {
//窗口默认可缩小,但无法关闭
public static void main(String[] args) {
Frame frame=new Frame("第一个GUI");
//1.使窗口可见
frame.setVisible(true);
//2.设置窗口大小
frame.setSize(800,200);
//3.设置窗口初始位置
frame.setLocation(200,500);
//上述两种方法等同于
// frame.setBounds(200,500,800,200);
//4.设置背景颜色 三原色 red green blue
//以及自带静态 颜色 magenta粉色 等
frame.setBackground(Color.MAGENTA);
// frame.setBackground(new Color(135, 227, 119));
//5.固定窗口大小 使其不可变
frame.setResizable(false);
}
}
创建多个窗口
import java.awt.*;
public class TestFrameMul {
public static void main(String[] args) {
//ctrl + d 复制本行到下一行
new MyFrame(100,100,500,500,Color.BLUE);
new MyFrame(100,300,500,500,Color.BLUE);
new MyFrame(300,100,500,500,Color.BLUE);
new MyFrame(300,300,500,500,Color.BLUE);
}
}
class MyFrame extends Frame{
//静态id反应共多少个弹窗
static int id=0;
public MyFrame (int x,int y,int w,int h,Color color) {
super("多个弹窗" + (++id));
this.setVisible(true);
this.setBounds(x,y,w,h);
this.setBackground(color);
}
}
2.panel(面板)
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
//Panel 无法单独存在,可看最是一个空间
public static void main(String[] args) {
Frame frame=new Frame("第一个Panel");
//FlowLayout 流布局
Panel panel=new Panel();
//设置可见
frame.setVisible(true);
//将panel 添加到frame上
frame.add(panel);
//设置布局
frame.setLayout(null);//NULL报错
//大小位置
frame.setBounds(200,600,500,500);
panel.setBounds(100,300,250,250);
//背景
frame.setBackground(new Color(161, 146, 245));
panel.setBackground(Color.WHITE);
//awt实现关闭功能
//监听 监听关闭事件
//给frame添加windows监听事件
frame.addWindowListener(new WindowAdapter() {
//参数应为Windos适配器 否则直接为WindowsListener 需重载太多方法
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3.布局管理器
·(1)流式(FlowLayout)布局 从左到右
import java.awt.*;
public class TestFlowLayOut {
//alt+enter 默认给new出来的对象提供左边
public static void main(String[] args) {
Frame frame =new Frame();
Button bt1=new Button("First");
Button bt2=new Button("Second");
Button bt3=new Button("Third");
//设置流式布局
// frame.setLayout(new FlowLayout());//默认居中
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//设置大小及可见性
frame.setVisible(true);
frame.setBounds(500,500,400,400);
//添加按钮
frame.add(bt1);
frame.add(bt2);
frame.add(bt3);
}
}
·(2)东南西北中(BorderLayout) 上下式
import java.awt.*;
public class TestBorderLayout {
//border 边界
public static void main(String[] args) {
Frame frame=new Frame("空");
frame.setVisible(true);
frame.setBounds(500,500,300,300);
frame.setLayout(new BorderLayout());
Button bt1=new Button("first");
Button bt2=new Button("second");
Button bt3=new Button("third");
Button bt4=new Button("four");
Button bt5=new Button("five");
frame.add(bt1,BorderLayout.NORTH);
frame.add(bt2,BorderLayout.SOUTH);
frame.add(bt3,BorderLayout.WEST);
frame.add(bt4,BorderLayout.EAST);
frame.add(bt5,BorderLayout.CENTER);
// frame.pack();
}
}
·(3)表格式(GridLayout) 几行几列
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame=new Frame("GridLayout");
frame.setVisible(true);
frame.pack();
frame.setSize(200,200);
frame.setLocation(600,600);
Button bt1=new Button("bt1");
Button bt2=new Button("bt2");
Button bt3=new Button("bt3");
Button bt4=new Button("bt4");
Button bt5=new Button("bt5");
Button bt6=new Button("bt6");
frame.setLayout(new GridLayout(2,3,1,1));
frame.add(bt1);
frame.add(bt2);
frame.add(bt3);
frame.add(bt4);
frame.add(bt5);
frame.add(bt6);
}
}
(4).综合练习
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestAll {
public static void main(String[] args) {
Frame frame=new Frame("Demo");
frame.setVisible(true);
frame.setBounds(500,500,600,600);
frame.setBackground(Color.MAGENTA);
//分为上下两个面板
frame.setLayout(new GridLayout(2,1));
Panel p1=new Panel(new BorderLayout());
Panel p2=new Panel(new BorderLayout());
Panel p3=new Panel(new GridLayout(2,1));
Panel p4=new Panel(new GridLayout(2,2));
//上面的面板分为左,中,右 中部再分为上下
p1.add(new Button("1-west"),BorderLayout.WEST);
p1.add(new Button("1-east"), BorderLayout.EAST);
p1.add(p3,BorderLayout.CENTER);
p3.add(new Button("1-center-up"));
p3.add(new Button("1-center-down"));
//下面
p2.add(new Button("2-west"),BorderLayout.WEST);
p2.add(new Button("2-east"),BorderLayout.EAST);
p2.add(p4,BorderLayout.CENTER);
p4.add(new Button("2-center-up-1"));
p4.add(new Button("2-center-up-2"));
p4.add(new Button("2-center-down-1"));
p4.add(new Button("2-center-down-2"));
//面板需先做好,然后再添加
frame.add(p1);
frame.add(p2);
frame.pack();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中。
3.布局管理器
1.流式 FlowLayout
2.东西南北中 BorderLayout
3.表格 GridLayout
4.大小 ,定位,背景颜色,可见性!
4.事件监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionListener {
public static void main(String[] args) {
Frame frame=new Frame();
Button bt1=new Button("start");
Button bt2=new Button("end");
bt1.setActionCommand("start");
//addActionListener需要一个ActionListener参数,ActionListener/是一个接口
MyActionListener actionListener=new MyActionListener();
bt1.addActionListener(actionListener);
bt2.addActionListener(actionListener);
frame.add(bt1,BorderLayout.NORTH);
frame.add(bt2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("e.getActionCommand()=="+e.getActionCommand());
}
}
5.输入框事件监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTextField {
public static void main(String[] args) {
//启动
new MyFrame1();
}
}
class MyFrame1 extends Frame {
public MyFrame1(){
TextField textField = new TextField();
MyActionListener1 myActionListener=new MyActionListener1();
//按enter键触发事件
textField.addActionListener(myActionListener);
//隐藏编码
textField.setEchoChar('*');
add(textField);
pack();
setVisible(true);
}
}
class MyActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getSource();//返回一个object对象
TextField field=(TextField)e.getSource();
System.out.println( field.getText() );
field.setText("");
}
}
6.简易计算器
oop原则 组合大于继承
使用了内部类
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calc().loadFrame();
}
}
class Calc extends Frame {
public TextField textField1,textField2,textField3;
public void loadFrame() {
//三个文本框,一个按钮,一个标签
textField1=new TextField();
textField2=new TextField();
textField3=new TextField();
Button button = new Button("=");
Label label=new Label("+");
//文本框长度 按钮监听事件
textField1.setColumns(10);
textField2.setColumns(10);
textField3.setColumns(20);
button.addActionListener(new MyCalcActionListener());
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
private class MyCalcActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int n1=Integer.parseInt(textField1.getText());
int n2=Integer.parseInt(textField2.getText());
textField3.setText(""+(n1+n2));
textField2.setText("");
textField1.setText("");
}
}
}
7.画笔
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(600,600,400,300);
setVisible(true);
}
@Override
public void paint(Graphics g) {
//super.paint(g);
//draw 和 fill Oval是椭圆形
// g.setColor(Color.MAGENTA);
g.drawOval(100,100,100,100);//x,y为相对位置
// g.fillOval(200,200,100,100);
g.fillRect(100,200,200,300);
//画笔用完,将其还原为最初颜色
}
}
8.鼠标监听
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
class MyFrame extends Frame{
ArrayList<Point> points;
MyFrame(String title)
{
super(title);
setBounds(200,200,400,300);
points=new ArrayList<>();
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
Iterator iterator=points.iterator();
while(iterator.hasNext()){
Point point=(Point)iterator.next();
g.setColor(Color.MAGENTA);
g.fillOval(point.x,point.y,10,10);
}
}
private class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame=(MyFrame)e.getSource();
//这个点就是鼠标的点
points.add(new Point(e.getX(),e.getY()));
myFrame.repaint();
}
}
}
9.窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.MAGENTA);
setBounds(100,100,200,200);
setVisible(true);
// addWindowListener(new MyWindowListener());
addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
Frame source = (Frame)e.getSource();
source.setTitle("窗口被激活了");
System.out.println("windowActivated");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
});
}
// private class MyWindowListener extends WindowAdapter{
//
// @Override
// public void windowClosing(WindowEvent e) {
// setVisible(false);//隐藏窗口
System.exit(0);//正常退出
// }
// }
}
10.键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setTitle("KeyFrame");
setBounds(200,200,200,200);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按下了up键");
}
}
});
setVisible(true);
}
}