图形用户界面GUI编程:AWT事件监听、输入框事件监听、鼠标监听、窗口监听、键盘监听

GUI编程学习笔记-2,根据目录食用~

1、事件监听

1.1 单个按钮触发事件

先监听事件:

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

再创建按钮触发监听事件:

 MyActionListener myActionListener = new MyActionListener();
 button.addActionListener(myActionListener);

完整:

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");
    }
}

1.2 多个按钮共享同一个触发事件

public class TestActionTwo {
    public static void main(String[] args) {
        // 两个按钮,实现同一个监听
        Frame frame = new Frame("开始-停止");
        Button bt1 = new Button("start");
        Button bt2 = new Button("stop");

        // 可以显示的定义触发会返回的命令,如果不显示定义,则走默认的值
        // !意思是:可以多个按钮只写一个监听类,方便查看
        bt2.setActionCommand("bt2-stop");

        MyMonitor myMonitor = new MyMonitor();
        bt1.addActionListener(myMonitor);
        bt2.addActionListener(myMonitor);

        frame.add(bt1,BorderLayout.NORTH);
        frame.add(bt2,BorderLayout.SOUTH);

        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 MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        // e.getActionCommand()获得按钮的信息
        System.out.println("按钮被点击:msg=>"+e.getActionCommand());
    }
}

2、输入框事件监听

输入框输入显示**,保密: textField.setEchoChar('*');

输入一次按回车后显示空: field.setText("");

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

// oop原则:组合 > 继承
// 继承
class MyFrame extends Frame{
	// 组合 
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);

        // 监听文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        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("");
    }
}

3、练习----简易计算器

// 简易计算器
public class TestCalc {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        windowClose(calculator);
    }

    // 关闭窗体
    private static void windowClose(Calculator calculator) {
        calculator.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}


// 计算器类
class Calculator extends Frame {
    public Calculator() {
        // 3个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);

        // 1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1, num2, num3));

        // 1个标签
        Label label = new Label("+");

        // 布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

}

// 监听器类
class MyCalculatorListener implements ActionListener {
    // 获取三个变量
    private TextField num1, num2, num3;  // 参数私有化保证安全性

    public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 1.获得加数与被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        // 2.加法算数后放入第三个框
        num3.setText("" + (n1 + n2));

        // 3.清楚前两个框
        num1.setText("");
        num2.setText("");
    }
}

完全改造为面向对象写法,改进后(减少硬编码,提高代码复用性):

// 简易计算器
public class TestCalc {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.loadFrame();
        windowClose(calculator);
    }

    // 关闭窗体
    private static void windowClose(Calculator calculator) {
        calculator.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}


// 计算器类
class Calculator extends Frame {
    // 属性
    TextField num1, num2, num3;

    // 方法
    public void loadFrame() {
        // 3个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);

        // 1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(this));

        // 1个标签
        Label label = new Label("+");

        // 布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }


}

// 监听器类
class MyCalculatorListener implements ActionListener {
    // 获取计算器这个对象,在一个类中组合另外一个类
    Calculator calculator = null;

    public MyCalculatorListener(Calculator calculator) {
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 1.获得加数与被加数
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        // 2.加法算数后放入第三个框
        calculator.num3.setText(""+(n1+n2));
        // 3.清楚前两个框
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

4、画笔 paint

基础操作:

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame {
    public void loadFrame(){
        setBounds(200, 200,600,500);
        setVisible(true);
    }

    // 画笔
    @Override
    public void paint(Graphics g) {
        // 颜色
        g.setColor(Color.red);
        // 画各种
        g.drawOval(100,100,100,100);
        g.fillOval(200,100,100,100);
    }
}

5、鼠标监听

public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("鼠标监听");
    }
}

class MyFrame extends Frame{
    ArrayList points;
    public MyFrame(String title){
        super(title);
        setBounds(200,200,400,300);
        // 存鼠标点击的点
        points = new ArrayList<>();

        setVisible(true);

        // 鼠标监听器,正对这个窗口
        this.addMouseListener(new MyHouseListener());
    }

    @Override
    public void paint(Graphics g) {
        // 画画,监听鼠标事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x, point.y,10,10);
        }
    }
    // 添加一个点到界面上
    public void addPaint(Point point){
        points.add(point);
    }
    // 适配器模式
    private class MyHouseListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();
            frame.addPaint(new Point(e.getPoint()));
            // 每次点击鼠标都需要重画
            frame.repaint();

        }
    }
}

6、窗口监听

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.blue);
        setBounds(100, 100, 200, 200);
        setVisible(true);
//        addWindowListener(new MyWindowListener());
        this.addWindowListener(
            new WindowAdapter() {
                // 关闭窗口
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("windowClosing");
                    System.exit(0);
                }
                // 激活窗口
                @Override
                public void windowActivated(WindowEvent e) {
                    WindowFrame source = (WindowFrame) e.getSource();
                    source.setTitle("被激活了");
                    System.out.println("windowActivated");
                }
            }
        );
    }
}

7、键盘监听

public class TestKeyboardListener {
    public static void main(String[] args) {
        new KeyFrame();

    }
}

class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(1,2,300,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                if(keyCode == KeyEvent.VK_UP){
                    System.out.println("您按了上建");
                }
                if(keyCode == KeyEvent.VK_DOWN){
                    System.out.println("您按了下键");
                }
            }
        });
    }
}

联想——>贪吃蛇游戏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值