【问题描述】
用 JAVA 语言设计如下界面的石头剪刀布游戏。游戏的一方点击界面上石头、剪刀、布按钮,游戏的 另一方计算机随机选择,然后给出游戏结果。
【题解】
package two; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; class TestOne extends JFrame implements ActionListener { // 定义石头剪刀布的 JButton 对象 JButton jiandao, shitou, bu, clear; JPanel panel2 = new JPanel(), panel1 = new JPanel(); // 组件 JLabel label = new JLabel("选择剪刀、石头、布开始游戏"), scoreLabel, userLabel = new JLabel("用户"), comLabel = new JLabel("电脑"), user = new JLabel(), com = new JLabel(); // 菜单 JMenuItem item1 = new JMenuItem("文本显示"), item2 = new JMenuItem("图形显示"), item3 = new JMenuItem("结束游戏"); // 图片路径 private static final String BASE_PATH = "D:\\图片\\"; String[] transOne = {"剪刀", "石头", "布"}; String[] transTwo = {"失败", "平手", "胜利"}; // 默认游戏主题 int theme = 0; // 用户的选择 int userOpt = -1; // 初始化菜单 public void initMenu() { // 创建菜单栏 JMenuBar jMenuBar = new JMenuBar(); // 创建菜单 JMenu jMenu1 = new JMenu("显示方式"); JMenu jMenu2 = new JMenu("关于我们"); // 增加菜单选项 jMenuBar.add(jMenu1); jMenuBar.add(jMenu2); // 增加菜单项 jMenu1.add(item1); jMenu1.add(item2); jMenu1.add(item3); // 给选项添加点击事件 item1.addActionListener(this); item2.addActionListener(this); item3.addActionListener(this); // 给窗体对象设置菜单. setJMenuBar(jMenuBar); } // 主函数 public static void main(String[] args) { TestOne frame = new TestOne(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭按钮 frame.setVisible(true); // 使窗体可见 } // 设置窗体 public TestOne() { setTitle("石头剪刀布游戏"); // 石头剪刀布游戏 setLocation(250, 250); // 设置窗体坐上顶点坐标 setSize(350, 350); // 设置窗体大小。窗体大小 : 350X350 // 建立对象 panel1.setBackground(Color.yellow); shitou = new JButton("石头"); jiandao = new JButton("剪刀"); bu = new JButton("布"); clear = new JButton("清空"); label = new JLabel("选择剪刀, 石头, 布开始游戏"); // 给所有 JBUtton 对象添加事件约束 // 给事件添加监听器 shitou.addActionListener(this); jiandao.addActionListener(this); bu.addActionListener(this); clear.addActionListener(this); // 把所有的新添加的对象都放进去窗体里面,先放进组件类,再放进窗体类 panel1.add(shitou); panel1.add(jiandao); panel1.add(bu); panel1.add(clear); add(panel1, BorderLayout.NORTH); // 初始化菜单 initMenu(); paintViewBot(); } // 绘制游戏界面中间 public void paintViewBot(){ // 取消默认布局 panel2.setLayout(null); // 设置面板2背景 panel2.setBackground(Color.green); // 放置组件 panel2.add(user); user.setBounds(10,50,100,100); panel2.add(com); com.setBounds(230,50,100,100); panel2.add(label); label.setBounds(120,50,100,100); panel2.add(userLabel); userLabel.setBounds(10, 20, 100, 20); panel2.add(comLabel); comLabel.setBounds(230, 20, 100, 20); add(panel2,BorderLayout.CENTER); add(panel2, BorderLayout.CENTER); } // 结果判定 public void judgeGame(int user) { // 防止同一个编译器产生相同的数字 Random num = new Random(); // 随机生成 4,7,10 中的一个数字 int i = 4 + (3 * ((int) (num.nextInt() * 10)) % 3); int res = i + user; if (res == 6 || res == 7 || res == 11) { showView(theme, 2, user, (user + 2) % 3); } else if (res == 5 || res == 9 || res == 10) { showView(theme, 0, user, (user + 1) % 3); } else if (res == 4 || res == 8 || res == 12) { showView(theme, 1, user, user); } } public void showView(int theme, int res, int i, int j) { if (theme == 0) { // 清除图片 clearIcon(); user.setText("用户: " + transOne[i]); com.setText("电脑:" + transOne[j]); label.setText("本局结果: " + transTwo[res] + "!"); } else if (theme == 1) { // 清除文本 clearText(); user.setIcon(new ImageIcon(BASE_PATH + theme + i + ".png")); com.setIcon(new ImageIcon(BASE_PATH + theme + j + ".png")); label.setIcon(new ImageIcon(BASE_PATH + theme + "_" + res + ".png")); } } // 清除图片 public void clearIcon() { user.setIcon(null); com.setIcon(null); label.setIcon(null); } // 清除文本 public void clearText() { user.setText(null); com.setText(null); } // 清除界面 public void clearAll() { clearText(); clearIcon(); label.setText("选择剪刀、石头、布开始游戏"); } // 事件响应器 @Override public void actionPerformed(ActionEvent e) { // 监听按下的键,执行对应的操作 if (e.getSource() == jiandao) { userOpt = 0; } else if (e.getSource() == shitou) { userOpt = 1; } else if (e.getSource() == bu) { userOpt = 2; } else if (e.getSource() == clear) { // 清除界面 clearAll(); // 重置用户操作 userOpt = -1; } // 切换显示方式 if (e.getSource() == item1) { theme = 0; } else if (e.getSource() == item2) { theme = 1; } else if (e.getSource() == item3) { theme = 2; // 退出窗口,清除内存 dispose(); } // 用户已进行操作 if (userOpt != -1) { // 开始绘制界面 judgeGame(userOpt); } } }