Java实验-Swing 色子游戏

一、实验要求:、

  1. 筛子游戏是一个具有趣味性的小游戏,它的实现原理是绘制图形,加载图片,通过鼠标事件点击按钮,晃动色子采用的是定时器,每隔200毫秒切换一次色子的图片实现:

请用Java图形界面的知识,编写以上软件。

代码:

MyFrame主界面类和MyTask定时器类

package com.junlin.exer_8;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyFrame extends JFrame {
    public int score = 100; //记录得分
    public int cnt = 1;   //记录当前色子的点数
    public JLabel imageLabel;
    public String filepath = "D:/onedrive/桌面/Java学习/Java作业/实验/实验8色子游戏/色子素材/a";
    //由于我没有新开一个点击处理类,就直接将整个窗口放到全局了
    public MyFrame window = this;
    public Timer timer; //定时器
    public JLabel titleLabel;  //文字
    public boolean isStop = true;  //判断当前摇色子是否处于停止状态

    public MyFrame() {
        super("xxx");
        JPanel root = new JPanel();
        root.setLayout(null);

        titleLabel = new JLabel("当前分数为:" + score);
        titleLabel.setBounds(150, 20, 100, 20);
        root.add(titleLabel);

        imageLabel = new JLabel();
        imageLabel.setIcon(new ImageIcon(filepath + cnt + ".jpg"));
        imageLabel.setBounds(150, 80, 120, 120);
        root.add(imageLabel);

        JButton playBtn = new JButton("摇色子");
        playBtn.addActionListener(new ActionListener() {
            //摇色子按键处理
            @Override
            public void actionPerformed(ActionEvent e) {
                //如果上一次没有停止就不继续执行,因为这样会让定时加快
                if(isStop == false) return ;
                isStop = false;
                timer = new Timer();
                timer.schedule(new MyTask(window), 0, 200);
            }
        });
        playBtn.setBounds(150, 200, 120, 50);
        root.add(playBtn);

        JButton bigBtn = new JButton("买大");
        bigBtn.addActionListener(new ActionListener() {
            //买大按键处理
            @Override
            public void actionPerformed(ActionEvent e) {
                //如果摇色子停止了就不能再判断分数了
                if(isStop == true) return ;
                timer.cancel();
                isStop = true;
                if(cnt >= 4) {
                    score += 10;
                }else {
                    score -= 10;
                }
                titleLabel.setText("当前分数 为" + score);
                titleLabel.paintImmediately(titleLabel.getBounds());
            }
        });
        bigBtn.setBounds(100, 300, 80, 50);
        root.add(bigBtn);

        JButton smallyBtn = new JButton("买小");
        smallyBtn.addActionListener(new ActionListener() {
            //买小按键处理
            @Override
            public void actionPerformed(ActionEvent e) {
                //如果摇色子停止了就不能再判断分数了
                if(isStop == true) return ;
                timer.cancel();
                isStop = true;
                if(cnt < 4) {
                    score += 10;
                }else {
                    score -= 10;
                }
                titleLabel.setText("当前分数 为:" + score);
            }
        });
        smallyBtn.setBounds(220, 300, 80, 50);
        root.add(smallyBtn);

        this.add(root);
        this.setSize(new Dimension(400, 600));
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
//定时器线程用来进行摇色子
class MyTask extends TimerTask{
    private MyFrame frame;
    public MyTask(MyFrame frame) {
        this.frame = frame;
    }
    @Override
    public void run() {
        //生成一个随机数用来选择目录下的一张色子图片
        int n = 1 + (int)(Math.random() * (6-1+1));
        this.frame.cnt = n;
        this.frame.imageLabel.setIcon(new ImageIcon(this.frame.filepath + this.frame.cnt + ".jpg"));
        //frame.imageLabel.paintImmediately(frame.imageLabel.getBounds());
    }
}

JavaMain类:

package com.junlin.exer_8;

public class JavaMain {

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

posted @ 2023-03-02 13:20  junlin623  阅读(117)  评论(0编辑  收藏  举报