存个代码
问题:
![](https://i-blog.csdnimg.cn/blog_migrate/d3d8c31f296a376c9a84876b3467898b.png)
样式图:
![](https://i-blog.csdnimg.cn/blog_migrate/6b59c40b3bc4eb9f5bffebc990c37474.png)
代码:
package Main;
import java.awt.*; //导入抽象窗口工具类包;
import java.awt.event.*; //导入事件处理工具类包
import javax.swing.*; //导入GUI工具包
import java.util.*;
public class PlayGame {
public static void main(String agrs[]) { //添加一个主函数,对窗体显示情况进行测试;
work11 frame =new work11(); //初始化窗体对象;
frame.setResizable(false); //设定大小不可改变
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗体关闭按钮;
frame.setVisible(true); //使窗体可见;
int[] a = new int[4];
Arrays.stream(a).sorted();
}
}
class work11 extends JFrame implements ActionListener {
//导入图片
ImageIcon None = new ImageIcon("");
ImageIcon W_ST = new ImageIcon("./src/IMG/W_Shitou.jpg");
ImageIcon P_ST = new ImageIcon("./src/IMG/P_Shitou.jpg");
ImageIcon L_ST = new ImageIcon("./src/IMG/L_Shitou.jpg");
ImageIcon W_JD = new ImageIcon("./src/IMG/W_Jiandao.jpg");
ImageIcon P_JD = new ImageIcon("./src/IMG/P_Jiandao.jpg");
ImageIcon L_JD = new ImageIcon("./src/IMG/L_Jiandao.jpg");
ImageIcon W_BU = new ImageIcon("./src/IMG/W_Bu.jpg");
ImageIcon P_BU = new ImageIcon("./src/IMG/P_Bu.jpg");
ImageIcon L_BU = new ImageIcon("./src/IMG/L_Bu.jpg");
//定义菜单相关的面板和按钮
JPanel panel3;
JButton About, TextDisplay, GrapDisplay, OverGame, Display;
//字体样式
Font font_res = new Font("隶书",Font.BOLD,19); //结果显示样式
Font font_op = new Font("华文行楷",Font.BOLD,19); //操控按钮样式
boolean IsText = true; //结果显示方式是否为文本显示的标记
//原组件定义
JButton jiandao, shitou, bu, clear; //把JButton组件定义放到类里面,方便其他函数访问;
JPanel pane12, panel1; //把JPanel画板组件定义放到类里面,方便其他函数访问;
JLabel label; //把Jlabel标签组件定义放到类里面,方便其他函数访问;
//游戏界面设定
public work11() {
setTitle("石头剪刀布游戏"); //设置窗体标题,修改标题为“石头剪刀布游戏”;
setLocation(250, 250); //设置窗体显示位置
setSize(360, 320); //设置窗体大小
panel1 = new JPanel(); //新建操控区窗体对象
panel1.setBackground(Color.yellow); //设定窗体背景颜色
shitou = new JButton("石头"); //新建按钮对象-石头
jiandao = new JButton("剪刀"); //新建按钮对象-剪刀
bu = new JButton("布"); //新建按钮对象-布
clear = new JButton("清空"); //新建按钮对象-清空
pane12 = new JPanel(); //新建游戏区窗体对象
pane12.setBackground(Color.green); //设定游戏区窗体背景颜色
label = new JLabel();//新建标签对象
label.setText("选择剪刀、石头、布开始游戏"); //游戏提示
shitou.addActionListener(this); //给石头添加事件监听器
bu.addActionListener(this); //给布添加事件监听器
jiandao.addActionListener(this); //给剪刀添加事件监听器
clear.addActionListener(this); //给清空添加事件监听器
panel1.add(shitou); //把shitou放进panel1中;
panel1.add(jiandao); //把jiandao放进panel1中;
panel1.add(bu); //把bu放进panel1中;
panel1.add(clear); //把clear放进panel1中;
pane12.add(label); //把label标签放进panel1中;
add(panel1, BorderLayout.NORTH); //添加游戏操控区面板,设定边框
add(pane12, BorderLayout.CENTER); //添加游戏显示区面板,设定边框
label.setFont(font_res); //设置标签字体
//建立最下方的菜单面板
panel3 = new JPanel();
panel3.setBackground(Color.YELLOW);
//按钮实例化
About = new JButton("关于游戏");
Display = new JButton("结果显示设置");
panel3.add(Display); //将结果显示方式控制按钮加入面板
panel3.add(About); //将关于游戏按钮加入面板
add(panel3, BorderLayout.SOUTH); //添加菜单控制区面板,设定边框
//设置按钮样式
About.setFont(font_op);
About.setBackground(Color.WHITE);
Display.setFont(font_op);
Display.setBackground(Color.WHITE);
jiandao.setFont(font_op);
jiandao.setBackground(Color.WHITE);
shitou.setFont(font_op);
shitou.setBackground(Color.WHITE);
bu.setFont(font_op);
bu.setBackground(Color.WHITE);
clear.setFont(font_op);
clear.setBackground(Color.WHITE);
//添加按钮点击事件
About.addActionListener(this);
//添加结果显示方式按钮点击事件
Display.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Menu();
}
});
}
//菜单界面设定
public void Menu(){
JFrame menu= new JFrame("菜单"); //建立菜单窗体
menu.setLocation(280,280); //设置窗体显示位置
menu.setSize(150,200); //设置窗体大小
JPanel jpanel = new JPanel(); //建立菜单面板
jpanel.setVisible(true); //子面板可见
menu.setLocationRelativeTo(null); //设置面板中央打开
jpanel.setSize(150, 250); //设置面板大小
jpanel.setBackground(Color.YELLOW); //设置面板背景颜色
//菜单按钮实例化
TextDisplay = new JButton("文本显示");
GrapDisplay = new JButton("图片显示");
OverGame = new JButton("结束游戏");
//设置按钮样式
TextDisplay.setFont(font_op);
TextDisplay.setBackground(Color.WHITE);
GrapDisplay.setFont(font_op);
GrapDisplay.setBackground(Color.WHITE);
OverGame.setFont(font_op);
OverGame.setBackground(Color.WHITE);
//添加按钮点击事件
TextDisplay.addActionListener(this);
GrapDisplay.addActionListener(this);
OverGame.addActionListener(this);
//将按钮加入面板
jpanel.add(TextDisplay);
jpanel.add(GrapDisplay);
jpanel.add(OverGame);
menu.getContentPane().add(jpanel); //将面板放入窗体
menu.setVisible(true); //窗体可见
}
//实现ActionListener接口方法,当按下按钮就会调用里面的方法,可以称为事件响应器;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == shitou) { //如果你按下的是shitou按键时,就会执行以下操作;
int i = ((int) (Math.random() * 10)) % 3; //定义一个int型变量i,利用Math.random()函数获取随机数,因为*10所以
//随机数区间为[0,10),最后强制转换为int型,再除3取余,最后的赋值给i;
switch (i) { //i为0时就执行case 0操作,以此类推;
case 0:
//显示设置(之后设定方式与此次相同)
if(IsText) { //是文本显示
label.setIcon(None); //去掉图片
label.setText("你出石头,电脑出石头,平局!"); //添加文字
}
else{ //图片显示
label.setText(""); //去掉文字
label.setIcon(P_ST); //添加如片
}
break; //当i=0时,执行后续操作,
//到break结束;(必须要添加break,不然会一直执行下去);
case 1:
if(IsText) {
label.setIcon(None);
label.setText("你出石头,电脑出剪子,恭喜你赢了!");
}
else{
label.setText("");
label.setIcon(W_ST);
}
break; //当i=1时,与上同理;
case 2:
if(IsText) {
label.setIcon(None);
label.setText("你出石头,电脑出布,很遗憾你输了!");
}
else{
label.setText("");
label.setIcon(L_ST);
}
break; //当i=2时,与上同理;
}
} else if (e.getSource() == bu) { //如果你按下的是bu按键时,与上同理;
int i = ((int) (Math.random() * 10)) % 3;
switch (i) {
case 0:
if(IsText) {
label.setIcon(None);
label.setText("你出布,电脑出布,平局!");
}
else{
label.setText("");
label.setIcon(P_BU);
}
break;
case 1:
if(IsText) {
label.setIcon(None);
label.setText("你出布,电脑出石头,恭喜你赢了!");
}
else{
label.setText("");
label.setIcon(W_BU);
}
break;
case 2:
if(IsText) {
label.setIcon(None);
label.setText("你出布,电脑出剪刀,很遗憾你输了!");
}
else{
label.setText("");
label.setIcon(L_BU);
}
break;
}
} else if (e.getSource() == jiandao) { //当你按下jiandao按键时,与上同理;
int i = ((int) (Math.random() * 10)) % 3;
switch (i) {
case 0:
if(IsText) {
label.setIcon(None);
label.setText("你出剪刀,电脑出剪刀,平局!");
}
else{
label.setText("");
label.setIcon(P_JD);
}
break;
case 1:
if(IsText) {
label.setIcon(None);
label.setText("你出剪刀,电脑出布,恭喜你赢了!");
}
else{
label.setText("");
label.setIcon(W_JD);
}
break;
case 2:
if(IsText) {
label.setIcon(None);
label.setText("你出剪刀,电脑出石头,很遗憾你输了!");
}
else{
label.setText("");
label.setIcon(L_JD);
}
break;
}
} else if (e.getSource() == clear) { //当你按下clear按键时,直接输出
label.setIcon(None);
label.setText("选择剪刀、石头、布开始游戏");
}
else if (e.getSource() == About) { //当你按下About按键时,显示关于游戏
label.setIcon(None); //删除图片
label.setText("这是我的课设小游戏"); //添加文字
}
else if (e.getSource() == TextDisplay) { //当你按下TextDisplay按键时,结果显示方式变为文本
IsText = true;
} else if (e.getSource() == GrapDisplay) { //当你按下GrapDisplay按键时,结果显示方式变为图片
IsText = false;
} else if (e.getSource() == OverGame) { //当你按下结束游戏按键时,程序结束
System.exit(0);
}
}
}