Java学习-动作监听器-数字炸弹-按钮监听

动作监听器:ActionListener

按钮类的作者设计一个调用逻辑,点击按钮就调用一个固定名字的空方法,我们需要给这个方法中添加代码,使得按钮被点击时调用执行我们添加的代码。

  • 动作设定:某个动作发生时,就做某事
  • 接口(interface):也是一份与class同级的代码
  • 类实现接口(implements):

       public class 类名 implements 接口名{}                                                                                               需要将接口中的抽象方法(空方法)重新在类中写一遍,加上大括号

  1. 自己创建一个新的类BtnAction实现ActionListener接口                                                                  pubic class BtnAction implements ActionListener{}
  2. 在BtnAction中重写ActionListener中的actionPerformed方法                                                        pubic void actionPerformed(ActionEvent e){}
  3. 在actionPerformed方法中添加一句输出语句进行测试                                                             System.out.println("按钮被点击了”);
  4. 在showUI方法的可视化之后,创建一个BtnAction对象                                                             BtnAction ba=new BtnAction()
  5. 使用按钮对象 调用addActionListener方法添加这个对象                               btn.addActionListener(ba);

数字监听器

package tky1020;

import javax.swing.*;
import java.awt.*;

public class BomUI {
    public void showUI(){
        JFrame jf=new JFrame();
        jf.setTitle("数字炸弹");
        jf.setSize(300,200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flow=new FlowLayout();
        jf.setLayout(flow);
        jf.setLocationRelativeTo(null);
        JLabel jla1=new JLabel("数字炸弹");
        Font font=new Font("黑体",Font.BOLD,48);
        jla1.setFont(font);

        JLabel jla2=new JLabel("请输入猜的数字:");
        JTextField jtf1=new JTextField(25);
        JButton btn=new JButton("确定");

        jf.add(jla1);
        jf.add(jla2);
        jf.add(jtf1);
        jf.add(btn);

        jf.setVisible(true);

        BtnAction ba=new BtnAction();
        btn.addActionListener(ba);
        ba.jtf2=jtf1;
    }
public static void main(String[]args){
        BomUI bomUI=new BomUI();
        bomUI.showUI();
}
}
package tky1020;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class BtnAction implements ActionListener {
    JTextField jtf2;

    int bomNum = -1;
    int min = -1;
    int max = 100;

    Random random = new Random();

    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了");

        if (bomNum == -1) {
            bomNum = random.nextInt(100);
            System.out.println(bomNum);
        }

            String numStr = jtf2.getText();
            System.out.println(numStr);
            int num = Integer.parseInt(numStr);

            if (num <= min || num >= max) {
                System.out.println("输入错误,输入的数字不在(" + min + "-" + max + ")范围内");
            } else {
                if (num == bomNum) {
                    System.out.println("猜中了,炸弹是" + num);
                    JOptionPane.
                            showMessageDialog(null, "猜中了,炸弹是" + num);
                    bomNum = -1;
                    min = -1;
                    max = 100;
                    jtf2.setText("");
                    JOptionPane.
                            showMessageDialog(null, "炸弹已经重置,请开始下一局游戏。");

                } else if (num < bomNum) {
                    JOptionPane.
                            showMessageDialog(null, "猜小了");
                    System.out.println("猜小了");
                    min = num;
                } else {
                    JOptionPane.
                            showMessageDialog(null, "猜大了");
                    System.out.println("猜大了");
                    max = num;
                }
            }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值