GUI编程之AWT

一、AWT介绍

1.包含了很多接口与和类
2.元素:窗口、按钮、文本框

二、组件和容器

注意:
1. Frame是一个顶级窗口
2. Panel 无法单独显示,必须添加到某个容器中。

1.Frame

public class Test {
    public static void main(String[] args) {
        Frame frame = new Frame("我的图形界面窗口");
        frame.setVisible(true);
        frame.setBackground(Color.BLACK);
       // frame.setResizable(false);//重新定义大小
        frame.setLocation(200,200);
        frame.setSize(100,200);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

2.画板Panel

public class PanelDemo {
    public static void main(String[] args) {
//设置绝对布局,面板--布局和面板边界坐标--背景颜色--可见--监听窗体关闭事监听
        Frame frame = new Frame("窗口");
        Panel panel = new Panel();

        frame.setLayout(null);
        frame.setBounds(100,200,200,300);
        panel.setBounds(50,50,50,50);
        frame.setBackground(Color.black);
        panel.setBackground(Color.white);
        frame.setVisible(true);
        panel.setVisible(true);
        frame.add(panel);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

3.布局管理器

流式布局

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //组件-按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //设置为流式布局
        //frame.setLayout(new FlowLayout());
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setSize(200,200);

        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);


    }
}

东西南北中布局

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");


        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(200,200);
        frame.setVisible(true);

    }
}

表格布局

public class GridLayoutDemo {
    public static void main(String[] args) {
        Frame frame = new Frame("表格布局");
        frame.setVisible(true);
        Panel panel = new Panel(new GridLayout(2, 1));
        frame.setBackground(Color.black);
        panel.setBackground(Color.red);
        String s = new Label("你好").getText();
        Button b = new Button(s);


        panel.add(b);
        Button b1 = new Button("ha");
        panel.add(b1);
        frame.setVisible(true);
        frame.setBounds(100,200,300,400);
        panel.setBounds(20,30,40,50);
        frame.add(panel);


    }
}

组合使用

public class Layout {
    public static void main(String[] args) {
        //画窗口
        Frame frame = new Frame("布局结合使用");
        frame.setBounds(100,100,200,200);
        frame.setVisible(true);
        frame.setResizable(false);
        //四个画布
        Panel panel1 = new Panel();
        Panel panel2 = new Panel();
        Panel panel3 = new Panel();
        Panel panel4 = new Panel();
        frame.setLayout(new GridLayout(2,1));
        frame.add(panel1);
        frame.add(panel3);

        //东西南北中布局
        panel1.setLayout(new BorderLayout());
        //表格布局
        panel2.setLayout(new GridLayout(2,1));
        panel3.setLayout(new BorderLayout());
        panel4.setLayout(new GridLayout(2,2));

        //添加按钮
        panel1.add(new Button("west1"),BorderLayout.WEST);
        panel1.add(new Button("east1"),BorderLayout.EAST);
        panel2.add(new Button("shang1"));
        panel2.add(new Button("shang2"));
        panel3.add(new Button("west2"),BorderLayout.WEST);
        panel3.add(new Button("east2"),BorderLayout.EAST);
        panel4.add(new Button("xia1"));
        panel4.add(new Button("xia2"));
        panel4.add(new Button("xia3"));
        panel4.add(new Button("xia4"));
        //画板嵌套
        panel1.add(panel2);
        panel3.add(panel4);
        //关闭
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


    }
}

4.事件监听

事件监听:当某个事情发生的时候,应该做什么

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button();
        //因为,addActionListener()需要一个 ActionListener,所以我们需要构造一个 ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();

        windowClose(frame); //关闭窗口
        frame.setVisible(true);


    }

    //关闭窗体的事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

//事件监听
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }

}

多个按钮触发事件

//事件监听
public class TestAction {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setBounds(100, 200, 300, 400);
        frame.setVisible(true);
        frame.setLayout(new FlowLayout());
        Button start = new Button("start");
        Button end = new Button("end");
        end.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               if(e.getActionCommand().equals("start")){
                   System.out.println("游戏开始了");
                   frame.setBackground(Color.red);
               }
            }
        });
        frame.add(start,BorderLayout.EAST);
        frame.add(end,BorderLayout.WEST);

    }
}

5.输入框TextField

public class TestText01 {
    public static void main(String[] args) {
        //启动!
        new MyFrame();
    }
}

class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);

        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter 就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);

        //设置替换编码
        textField.setEchoChar('*');

        setVisible(true);
        pack();

    }
}

class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();     //获得一些资源,返回的一个对象
        System.out.println(field.getText()); //获得输入框的文本
        field.setText(""); //null  ""
    }
}

6.应用:简易计算器,组合+内部类

public class ZdCalculator extends Frame implements ActionListener {
// 设置三个文本框,一个按钮,一个标签
    private TextField textField_num1;
    private TextField textField_num2;
    private TextField textField_num3;
    private Button button =null;
    private Label label =null;

    public void init(){
        textField_num1 = new TextField();
        textField_num2 = new TextField();
        textField_num3 = new TextField();

        button = new Button("=");
        button.addActionListener(this);
        label = new Label("+");
        this.setVisible(true);
        this.setLayout(new FlowLayout());
        this.add(textField_num1);
        this.add(label);
        this.add(textField_num2);
        this.add(button);
        this.add(textField_num3);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String num1 = textField_num1.getText();
        String num2 = textField_num2.getText();
        int n1 = Integer.parseInt(num1);
        int n2=Integer.parseInt(num2);
        textField_num3.setText(""+(n1+n2));
        textField_num1.setText("");
        textField_num2.setText("");
    }
}

7.画笔

public class ZdPaint extends Frame {
    //要在构造函数里面设置好窗口大小、颜色等
    public ZdPaint() {
        this.setBackground(Color.red);
        this.setBounds(200, 300, 400, 500);
        this.setVisible(true);
        this.setResizable(false);
    }
//画笔画矩形
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.fillRect(100, 100, 100, 200);
        g.setColor(Color.white);

    }
}

8.鼠标监听

public class PaintMouse {
    public static void main(String[] args) {
        new MyFrame();
    }
}

//窗体
class MyFrame extends Frame implements ActionListener,MouseListener, MouseMotionListener{
    ArrayList pointList;
    boolean dragging = false;
    Timer timer;

    public MyFrame() {
        super("画点");
        pointList = new ArrayList();
        timer = new Timer(1000,this);
        timer.start();
        this.setBounds(100, 100, 400, 400);
        this.setVisible(true);
        this.setResizable(false);
        this.setBackground(Color.white);
        // pointList = new ArrayList();
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }
    //画画
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Iterator iterator = pointList.iterator();
        while (iterator.hasNext()) {
            Point point = (Point) iterator.next();
            g.drawOval(point.x, point.y, 10, 10);
            g.fillOval(point.x, point.y, 10, 10);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       // System.out.println("Timer");
        if (dragging) {
            repaint();
        }
       // timer.start();
    }

    @Override
    public void mouseClicked(MouseEvent e){

    }
    @Override
    public void mouseEntered(MouseEvent e){

    }
    @Override
    public void mouseExited(MouseEvent e){

    }
    @Override
    public void mousePressed(MouseEvent e){
        Point point = e.getPoint();
        System.out.println("mousePressed at " + point);
        dragging = true;
    }
    @Override
    public void mouseReleased(MouseEvent e){
        Point point = e.getPoint();
        System.out.println("mouseReleased at " + point);
        dragging = false;
    }
    @Override
    public void mouseDragged(MouseEvent e){
        Point p = e.getPoint();
        System.out.println("mouseReleased at " + p);
        pointList.add(p);
//        if (dragging) {
//            repaint();
//        }
    }
    @Override
    public void mouseMoved(MouseEvent e){

    }


}

//鼠标监听
//class MyMouseListener extends MouseAdapter {
//    @Override
//    public void mousePressed(MouseEvent e) {
//       MyFrame frame= (MyFrame) e.getSource();
//        Point point = e.getPoint();
//        System.out.println(point.x+point.y);
//        frame.pointList.add(point);
//        //frame.repaint();
//
//    }
//}

9.键盘监听

public class TestKey {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame  {


    public KeyFrame(){

        this.setBounds(10,10,200,200);
        this.setLayout(new FlowLayout());
        this.setResizable(false);
        this.setVisible(true);

        this.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                int KeyCode=e.getKeyCode();
                if(KeyCode==KeyEvent.VK_RIGHT){
                    System.out.println("哥哥");
                }
            }
        });
    }

}

发布了46 篇原创文章 · 获赞 1 · 访问量 1007

猜你喜欢

转载自blog.csdn.net/qq_42022411/article/details/103357531