项目起因:
初学java Swing,想做一个小游戏 。

前言:
1. 刚学java Swing,做的一个剪刀石头布的小游戏,还有很多不会的,忘大佬指点。
2. 游戏规则:点击中间三个按钮,按钮代表了你的出拳方式,然后电脑随机出拳,赛出结果。

目录

  • (一)图形界面演示
  • (二)项目功能介绍
  • (三)项目组成结构
  • (四)项目代码
  • (五)总结

正文:

(一)图形界面演示

1.主界面

2.点击中间三个按钮,这里以第一个按钮“我出剪刀”为示例



3.点击菜单,出现三个菜单项,并依次点击三个菜单项:




(二)项目功能介绍

实现了如下功能:1.电脑随机出拳功能。2.设置了背景图片。3.菜单下拉功能。4.点击按钮或者菜单选项出现第二窗口(dialog窗口)功能。5.继续游戏、退出游戏、和返回主界面功能。

(三)项目组成结构

项目总体结构为三个类:
(一)设计图形界面和实现具体实例的类FingerGameFinal2。
(二)封装数据和方法的类ComputerOperation。
(三)自定义异常的类ExceptionTest。

(一)类FingerGameFinal2的结构:

 1.1 构造函数: public FingerGameFinal2(){ this.addAndSetComponent(); this.addListener();} 1.2 其中 两个函数的功能分别为:设置组件的函数 addAndSetComponent()和为按钮和菜单项添加监听器的函数 addListener() 。

(二)类ComputerOperation的结构:

 2.1 封装一些要返回的数据。2.2 方法有ComputerOutFinger()用来返回电脑随机出的手势。

(三)自定义异常的类ExceptionTest的结构及作用:

 3.1 结构:重写父类的无参和有参构造函数3.2 作用:如果类ComputerOperation中的方法ComputerOutFinger()中的

r.nextInt(n)中的参数大于3,抛出异常信息“超出范围”1

(四)项目代码

(一)设计图形界面和实现具体实例的类FingerGameFinal2

package game;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class FingerGameFinal2 extends JFrame{private JButton btn1,btn2,btn3,btn4,btn5,btn6;private JMenuBar menuBar;private JMenu menu;private JMenuItem menuItem1,menuItem2,menuItem3;private JDialog dialog_MenuItem1,dialog_MenuItem2,dialog_MenuItem3,dialog_btn1,dialog_btn2,dialog_btn3;private JLabel label_GameName,label_btn1,label_btn2,label_btn3,label_MenuItem1_1,label_MenuItem2_2,label_MenuItem2_1;private ComputerOperation cptOperation;ImageIcon backgroundImage;// 主函数创建类的实例对象public static void main(String[] args){new FingerGameFinal2();}// 构造函数public FingerGameFinal2(){this.addAndSetComponent(); // 添加组件和设置属性this.addListener(); // 添加监听器}// 添加组件和设置属性private void addAndSetComponent(){cptOperation = new ComputerOperation();btn1 = new JButton(cptOperation.getYourScissor());btn1.setForeground(new Color(0, 0, 128));btn1.setBounds(4, 102, 93, 27);btn1.setBackground(Color.ORANGE);btn2 = new JButton(cptOperation.getYourStone());btn2.setForeground(new Color(0, 0, 128));btn2.setBounds(103, 102, 93, 27);btn2.setVerticalAlignment(SwingConstants.TOP);btn2.setBackground(Color.ORANGE);btn3 = new JButton(cptOperation.getYourCloth());btn3.setForeground(new Color(0, 0, 128));btn3.setBounds(202, 102, 77, 27);btn3.setBackground(Color.ORANGE);// 按钮btn4和btn5作用于菜单栏中的退出游戏dialog窗口中btn4 = new JButton("确定");btn4.setBackground(new Color(255,105,180));btn5 = new JButton("返回");btn5.setBackground(new Color(0,250,154));// 窗口名this.setTitle("fingergame");   // 窗体背景色,如果没有图片,将显示此背景色this.getContentPane().setBackground(new Color(0, 153, 102));// 对话框设置this.setSize(300,250);//窗体大小固定this.setResizable(false);this.setLocation(700, 300);this.getContentPane().setLayout(null);this.getContentPane().add(btn1);this.getContentPane().add(btn2);this.getContentPane().add(btn3);// 添加 游戏名 的标签label_GameName = new JLabel("剪刀石头布游戏");label_GameName.setForeground(Color.WHITE);label_GameName.setFont(new Font("宋体", Font.BOLD, 26));label_GameName.setBounds(45, 30, 200, 47);this.getContentPane().add(label_GameName);// 创建 菜单栏menuBarmenuBar = new JMenuBar();menuBar.setBounds(0, 0, 39, 26);this.getContentPane().add(menuBar);menuBar.setBackground(new Color(238, 238, 0));// 把菜单menu 添加 到菜单栏menuBar中menu = new JMenu("菜单");menuBar.add(menu);// 创建 三个菜单项menuItem1 = new JMenuItem("游戏规则");menuItem1.setBackground(UIManager.getColor("MenuItem.background"));menu.add(menuItem1);menu.addSeparator();menuItem2 = new JMenuItem("声明");menuItem2.setBackground(UIManager.getColor("MenuItem.background"));menu.add(menuItem2);menu.addSeparator();menuItem3 = new JMenuItem("退出游戏");menuItem3.setBackground(UIManager.getColor("MenuItem.background"));menu.add(menuItem3);}// 添加监听器private void addListener(){dialog1Menu.setResizable(false);menuItem1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_MenuItem1 = new JDialog();dialog_MenuItem1.getContentPane().setLayout(null);label_MenuItem1_1 = new JLabel();label_MenuItem1_1.setText("点击三个按钮对应按钮上的操作,随后电脑随机出拳,判断胜负");label_MenuItem1_1.setBounds(10,0,500,65);dialog_MenuItem1.getContentPane().add(label_MenuItem1_1);dialog_MenuItem1.setTitle("游戏规则");dialog_MenuItem1.setSize(400, 100);dialog_MenuItem1.setLocation(650,360);dialog_MenuItem1.setVisible(true);}});menuItem2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_MenuItem2 = new JDialog();dialog_MenuItem2.getContentPane().setLayout(null);label_MenuItem2_1 = new JLabel();label_MenuItem2_2 = new JLabel();label_MenuItem2_2.setText(cptOperation.getStatementName());label_MenuItem2_1.setText(cptOperation.getStatement());label_MenuItem2_2.setForeground(new Color(255, 0, 0));label_MenuItem2_1.setBounds(40, 0, 170, 65);label_MenuItem2_2.setBounds(160,0,80,65);dialog_MenuItem2.getContentPane().add(label_MenuItem2_1);dialog_MenuItem2.getContentPane().add(label_MenuItem2_2);dialog_MenuItem2.setTitle("声明");dialog_MenuItem2.setSize(300, 100);dialog_MenuItem2.setLocation(700,350);dialog_MenuItem2.setVisible(true);}});menuItem3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_MenuItem3 = new JDialog();dialog_MenuItem3.setResizable(false);JLabel label_MenuItem3 = new JLabel("确定退出fingergame吗?");label_MenuItem3.setBounds(70, 0, 200,50);dialog_MenuItem3.getContentPane().setLayout(null);dialog_MenuItem3.setTitle("退出游戏");dialog_MenuItem3.getContentPane().add(label_MenuItem3);btn4.setBounds(50, 50, 80, 35);btn5.setBounds(150, 50, 80, 35);dialog_MenuItem3.getContentPane().add(btn4);dialog_MenuItem3.getContentPane().add(btn5);dialog_MenuItem3.setSize(290, 130);dialog_MenuItem3.setLocation(705,370);dialog_MenuItem3.setVisible(true);btn4.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根System.exit(0);}});btn5.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_MenuItem3.dispose();}});   }});// 设置单击关闭的窗口this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口显示this.setVisible(true);//创建JDialog对话框,作用是点击三个按钮出现第二窗口btn6 = new JButton("继续游戏");// 背景图片backgroundImage = new ImageIcon("res/image/image1.jpg");JLabel label_BackgroundImage = new JLabel(backgroundImage);label_BackgroundImage.setBounds(0, 0, backgroundImage.getIconWidth(), backgroundImage.getIconHeight());JPanel j=(JPanel)this.getContentPane();j.setOpaque(false);// ((JComponent) this.getContentPane()).setOpaque(false);getContentPane().add(label_BackgroundImage);btn1.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_btn1 = new JDialog();dialog_btn1.setResizable(false);label_btn1 = new JLabel();label_btn1.setFont(new Font("黑体",Font.BOLD,18));dialog_btn1.getContentPane().setLayout(null);dialog_btn1.setLocation(750, 350);dialog_btn1.setSize(220, 150);btn6.setBounds(48, 50, 100, 35);btn6.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_btn1.dispose();}});label_btn1.setBounds(26, 0, 200,50);dialog_btn1.getContentPane().add(btn6);dialog_btn1.getContentPane().add(label_btn1);dialog_btn1.setModal(true);try {if (cptOperation.judgeWithScissor()){label_btn1.setForeground(new Color(0, 199, 140));label_btn1.setText(cptOperation.getComputerScissor()+" 平局");dialog_btn1.getContentPane().add(label_btn1);}else if (cptOperation.judgeWithStone()){label_btn1.setForeground(new Color(255, 0, 0));label_btn1.setText(cptOperation.getComputerStone()+" 你输了");dialog_btn1.getContentPane().add(label_btn1);}else{label_btn1.setForeground(new Color(0, 0, 0));label_btn1.setText(cptOperation.getComputerCloth()+" 你赢了");dialog_btn1.getContentPane().add(label_btn1);}} catch (ExceptionTest e1) {// TODO 自动生成的 catch 块e1.printStackTrace();}dialog_btn1.setVisible(true);}});btn2.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_btn2 = new JDialog();dialog_btn2.setResizable(false);label_btn2 = new JLabel();label_btn2.setFont(new Font("黑体",Font.BOLD,18));dialog_btn2.getContentPane().setLayout(null);dialog_btn2.setLocation(750, 350);dialog_btn2.setSize(220, 150);btn6.setBounds(48, 50, 100, 35);btn6.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_btn2.dispose();}});label_btn2.setBounds(26, 0, 200,50);dialog_btn2.getContentPane().add(btn6);dialog_btn2.getContentPane().add(label_btn2);dialog_btn2.setModal(true);try {if (cptOperation.judgeWithScissor()){label_btn2.setForeground(new Color(0, 0, 0));label_btn2.setText(cptOperation.getComputerScissor()+" 你赢了");dialog_btn2.getContentPane().add(label_btn2);}else if (cptOperation.judgeWithStone()){label_btn2.setForeground(new Color(0, 199, 140));label_btn2.setText(cptOperation.getComputerStone()+" 平局");dialog_btn2.getContentPane().add(label_btn2);}else{label_btn2.setForeground(new Color(255, 0, 0));label_btn2.setText(cptOperation.getComputerCloth()+" 你输了");dialog_btn2.getContentPane().add(label_btn2);}} catch (ExceptionTest e1) {// TODO 自动生成的 catch 块e1.printStackTrace();}dialog_btn2.setVisible(true);}});btn3.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_btn3 = new JDialog();dialog_btn3.setResizable(false);label_btn3 = new JLabel();label_btn3.setFont(new Font("黑体",Font.BOLD,18));dialog_btn3.getContentPane().setLayout(null);dialog_btn3.setLocation(750, 350);dialog_btn3.setSize(220, 150);btn6.setBounds(48, 50, 100, 35);btn6.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO 自动生成的方法存根dialog_btn3.dispose();}});label_btn3.setBounds(26, 0, 200,50);dialog_btn3.getContentPane().add(btn6);dialog_btn3.getContentPane().add(label_btn3);try {if (cptOperation.judgeWithScissor()){label_btn3.setForeground(new Color(255, 0, 0));label_btn3.setText(cptOperation.getComputerScissor()+" 你输了");dialog_btn3.getContentPane().add(label_btn3);}else if (cptOperation.judgeWithStone()){label_btn3.setForeground(new Color(0, 0, 0));label_btn3.setText(cptOperation.getComputerStone()+" 你赢了");dialog_btn3.getContentPane().add(label_btn3);}else{label_btn3.setForeground(new Color(0, 199, 140));label_btn3.setText(cptOperation.getComputerCloth()+" 平局");dialog_btn3.getContentPane().add(label_btn3);}} catch (ExceptionTest e1) {// TODO 自动生成的 catch 块e1.printStackTrace();}dialog_btn3.setVisible(true);}});}
}
(二) 封装数据和方法的类ComputerOperation
package game;
import java.util.Random;
import book_knowledge.ExceptionTest;
public class ComputerOperation {// 创建三个按钮,即"我出剪刀"、"我出石头"、"我出布"private String statement = "本游戏解释权为";private String statementName = "@xjy";private String yourScissor = "我出剪刀";private String yourStone = "我出石头";private String yourCloth = "我出布";// computerScissor、computerStone 、computerCloth是电脑随机出的手型private String computerScissor = "电脑出剪刀";private String computerStone = "电脑出石头";private String computerCloth = "电脑出布";// 返回声明人姓名和声明public String getStatementName() {return statementName;}public String getStatement(){return statement;}// 返回computerScissor、computerCloth、computerStone的值public String getComputerScissor() {return computerScissor;}public String getComputerStone() {return computerStone;}public String getComputerCloth() {return computerCloth;}// 返回yourScissor、yourStone、yourClothpublic String getYourScissor() {return yourScissor;}public String getYourStone() {return yourStone;}public String getYourCloth() {return yourCloth;}// 判断是电脑出随机出的手型否是和“电脑出剪刀”相同;public Boolean judgeWithScissor() throws ExceptionTest{if ( ComputerOutFinger().equals(getComputerScissor()) )return true;elsereturn false;}// 判断是电脑出随机出的手型否是和“电脑出石头”相同;public Boolean judgeWithStone() throws ExceptionTest{if ( ComputerOutFinger().equals(getComputerStone()) )return true;elsereturn false;}// 电脑随机生成0、1、2,如果是1电脑就出剪刀,2电脑出石头,3电脑出布Random r = new Random();public String ComputerOutFinger() throws ExceptionTest{switch (r.nextInt(3)) {case 0:return (computerScissor);case 1:return (computerStone);case 2:return (computerCloth);        default:throw new ExceptionTest("超出范围");}}
}
(三)自定义异常的类ExceptionTest(如果类ComputerOperation中的方法r.nextInt(n)中的参数大于3,出现异常)
package book_knowledge;
import game.ComputerOperation;
@SuppressWarnings("serial")
public class ExceptionTest extends Exception{public ExceptionTest(){super();}public ExceptionTest(String message){super(message);}
}

(五)总结

项目的逻辑很简单,即你出拳,然后电脑出拳,最后比较得出你的输、赢或平局,
主要在于设计图形界面方面,就是感觉代码过于冗长,很多地方能变短的,
但是不知道怎么办,还是要继续学习呀。

  1. 注脚的解释

      ! 注意:这里参数如果小于0的话,就会出  现 n must be positive的异常信息,所以这里用词为“超出范围”,而不是“不在范围”。
    

    ↩︎

java Swing 做剪刀石头布游戏相关推荐

  1. Java Swing 经典小游戏《飞机大战》———— (四)碰撞检测 游戏状态与得分 玩家升级

    前期回顾 Java Swing 经典小游戏<飞机大战>---- (一)获取素材,创建窗口,添加滚动背景,双缓冲 Java Swing 经典小游戏<飞机大战>---- (二)玩家 ...

  2. Java Swing打猎射击游戏编程代码下载

    代码下载地址:http://www.zuidaima.com/share/1858069987494912.htm 原文:Java Swing打猎射击游戏编程代码下载 这是一款java swing编写 ...

  3. 基于Java+Swing实现俄罗斯方块游戏

    基于Java+Swing实现俄罗斯方块游戏 一.系统介绍 二.功能展示 三.其他系统 一.系统介绍 俄罗斯方块项目,基本功能包括:游戏主界面显示模块.方块及数据显示模块.方块移动控制模块.游戏界面颜色 ...

  4. java Swing 做一个简单的输入文本框

    java Swing做一个简单的文本输入框, 新建一个SwingDemo类: // //java swing做一个简单的文本框 //Created by lee_1310 on 2019.03.29 ...

  5. Java Swing实现小游戏集合源码

    ​小编也不给大家废话, 直接给大家分享源码 Java 植物大战僵尸 Java Swing实现小游戏扫雷 Java Swing实现的拼图小游戏 Java swing实现的小游戏魔方 Java Swing ...

  6. 基于Java+Swing实现愤怒的小鸟游戏

    基于Java+Swing实现愤怒的小鸟游戏 一.系统介绍 二.功能展示 三.其他系统 四.获取源码 一.系统介绍 基于Java的愤怒的小鸟游戏的设计与实现,基本功能包括:新游戏.载入游戏.控制帮助.退 ...

  7. Eclipse+Java+Swing实现斗地主游戏

    Java+Swing实现斗地主游戏 一.系统介绍 二.系统展示 1.扑克分发 2.抢地主 3.出牌 4.游戏胜利 三.系统实现 Card.java CardType.java Common.java ...

  8. Java+Swing实现五子棋游戏

    目录 一.系统介绍 1.开发环境 2.技术选型 3.系统功能 二.系统展示 1.首页 2.黑棋走 3.白棋走 三.部分代码 AI.java Chess.java Gobang.java GobangL ...

  9. 基于Java Swing 飞机大战游戏

    一.项目概要: 基于Java Swing飞机大战游戏,打死飞机,可以随机出现加血包.子弹升级包.并支持根据游戏的分数,调整游戏难度. 二.界面展示: 三.主要代码: package com.maoxu ...

最新文章

  1. 阿里大神分享API网关在微服务架构中的应用!
  2. 使用AspNetpagerGridView添加连续的序号
  3. 揭开webRTC媒体服务器的神秘面纱——WebRTC媒体服务器开源项目介绍
  4. hamcrest_重新设计Hamcrest
  5. 8.4. Socket 方式
  6. 学习HashMap的笔记
  7. java 获取jboss路径_java中获取文件路径的几种方式
  8. 快速排序(C#)实现
  9. 树莓派PICO:DS1302时钟芯片(MicroPython)
  10. Hessian 矩阵(黑塞矩阵)以及hessian矩阵奇异的用法
  11. mitmproxy+python
  12. QT Libvlc音视频环境配置及编译错误解决
  13. 美国薪资最高的技术技能:Golang、Kafka、DynamoDB、Redshift、Cassandra
  14. fcitx 添加输入法但并没有输入候选项
  15. 上海数据交易中心交易系统开放
  16. 种草软文怎么写?分享一些超实用的种草软文写作技巧。
  17. 对计算机系相关人物进行访谈,瑶湖计算机系副主任姚华访谈录
  18. 何伊凡:猪、土豪与屌丝
  19. 2012年苏州大学872真题整理
  20. 常见六大Web安全攻防解析

热门文章

  1. java计算机毕业设计高校社区生鲜配送系统源码+mysql数据库+系统+lw文档+部署
  2. 页面中到达顶部和底部的按钮
  3. 多啦咪图虫_浏览器插件图片批量下载工具扩展插件图片下载
  4. 组织机构同步和人员同步
  5. 【Linux】CentOS 7安装 MySQL
  6. 掌握这些Vue原理,就能月薪30K?
  7. 从头开始进行CUDA编程:Numba并行编程的基本概念
  8. 明明觉醒为啥服务器还在维护,文明觉醒无法连接服务器是什么原因
  9. 花朵数c语言算法,画几朵花
  10. 一个到东京收买烟火 水浒传