一个猜数字小游戏,大二实训自做
前言
刚做出来,没什么好说的,本来也就不是啥大项目
初代版本,没改字体颜色大小啥的,可以自己改
不过自己能做出来,很高兴
废话不多说,直接放源码
源码
一个GuessNumFrame类
同时还用了一个封装好的本地工具类:LocationUtil
注意代码中图片地址非确定,改为自己的
GuessNumFrame类:
package top.handsomelv.study.test;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import top.handsomelv.study.util.LocationUtil;
/**
*
* <p>项目名称: GuessNumGame </p>
* <p>文件名称: GuessNumFrame </p>
* <p>创建时间: 2021/7/15/12:56 </p>
* @author <a href="mail to: handsomelvv@qq.com">HANDSOME_LV</a>
* @version v1.0
*/
public class GuessNumFrame extends JFrame implements ActionListener {
//生成随机数
Random rand = new Random();//用Random产生随机数
int randNum = rand.nextInt(100);//接收随机数randNum在[0,100)内
//添加按钮
private JButton close;
private JButton go;
private JButton again;
private JButton tel;
//添加文本输入输出框
private JTextArea getnum;
private JTextArea outnum;
//重写构造器
public GuessNumFrame() {
System.out.println("构造器");
JLabel mainJLabel = new JLabel(new ImageIcon("image/case02/background.jpg"));
this.setIconImage(new ImageIcon("image/case02/icon.jpg").getImage());
this.setUndecorated(true);
this.add(mainJLabel);
//设置界面可以跟随鼠标移动
LocationUtil util = new LocationUtil(this);
this.setBounds(200,50,400,300);
close = new JButton(new ImageIcon("image/case02/close.png"));
close.setContentAreaFilled(false);
close.setBorderPainted(false);
close.setBounds(365,5,24,23);
mainJLabel.add(close);
go = new JButton(new ImageIcon("image/case02/go.png"));
go.setContentAreaFilled(false);
go.setBorderPainted(false);
go.setBounds(295,123,20,20);
mainJLabel.add(go);
again = new JButton(new ImageIcon("image/case02/again_press.png"));
again.setContentAreaFilled(false);
again.setBorderPainted(false);
again.setBounds(233,164,80,26);
mainJLabel.add(again);
tel = new JButton(new ImageIcon("image/case02/tel_press.png"));
tel.setContentAreaFilled(false);
tel.setBorderPainted(false);
tel.setBounds(88,164,80,26);
mainJLabel.add(tel);
getnum = new JTextArea();
getnum.setBackground(new Color(240,173,43));
getnum.setBounds(206,126, 39, 17);
mainJLabel.add(getnum);
outnum = new JTextArea();
outnum.setBackground(new Color(255,191,31));
outnum.setBounds(86,218, 230, 60);
mainJLabel.add(outnum);
//添加事件
close.addActionListener(this);
go.addActionListener(this);
again.addActionListener(this);
tel.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==close) {
System.out.println("关闭游戏");
System.exit(0);
}
if (e.getSource()==go) {
System.out.println("确定答案");
if (Integer.parseInt(getnum.getText())==randNum) {
JOptionPane.showMessageDialog(
GuessNumFrame.this,
"恭喜你答对了,数字是"+randNum);
}else if (Integer.parseInt(getnum.getText())>randNum) {
System.out.println("猜的有点大");
outnum.setText("猜的有点大");
}else if (Integer.parseInt(getnum.getText())<randNum) {
System.out.println("猜的有点小");
outnum.setText("猜的有点小");
}
}
if (e.getSource()==again) {
System.out.println("再来一次");
getnum.setText(null);
outnum.setText(null);
randNum = rand.nextInt(100);
}
if (e.getSource()==tel) {
System.out.println("告诉我吧");
System.out.println("数字是"+randNum);
outnum.setText("数字是"+randNum);
}
}
public static void main(String[] args) {
GuessNumFrame frame = new GuessNumFrame();
}
}
LocationUtil 类:
package top.handsomelv.study.util;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
/*
* 封装一个工具类
* 设计模式的一种:高内聚,低耦合
* 1)点击窗口的时候,输出点击的位置x,y
* 2)鼠标操作,窗体跟随鼠标移动
*
*/
public class LocationUtil {
JFrame fram;
private boolean isDraging = false;
private int xx,yy;
//重写构造器,添加形参
public LocationUtil(JFrame frame) {
this.fram =frame;
//监听事件
fram.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
isDraging = true;
xx = e.getX();
yy = e.getY();
System.out.println("=="+xx+":"+yy);
}
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
isDraging = false;
}
});
fram.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
if (isDraging) {
int left = fram.getLocation().x;
int top = fram.getLocation().y;
fram.setLocation(left+e.getX(),top+e.getY());
}
}
});
}
}
可以留下你的信息哦(去Github_issues)😀😀😀
GitHub Issues