1.项目简介
设计并实现一个和计算机交互的剪刀、石头、布游戏。人和计算机各选择一项,然后决出胜负。程序必须随机选取一项但不表示出来,然后提示用户选择。当用户选择后,程序同时显示出计算机和用户的选择并且输出游戏结果。游戏一直进行直到用户退出为止,并且输出双方输赢次数和总局数。
2.功能描述
根据题目要求建立一个Panel类Main1用于存储图片和组件。在类中申请3个panel,第一个用于存放图片,第二个用于存放按键,最后将这两个放进panel3中。在panel1中的图片需要根据panel2中按键触发的事件进行相应的改变。电脑和用户都有石头、剪刀和布三张图片进行变化。每次在用户点击按键石头或剪刀或布后电脑会根据随机数产生器发出的随机数进行相应的“出拳”,然后和用户选择的“出拳”进行比较输赢。当用户选择后,两者的出拳图会显示在panel1中,并出现信息说明当前游戏总次数和用户和电脑各自的胜出次数。用户可以再一次游戏介绍后继续点击按键进行多次游戏,每次重复上面功能。
代码:
Panel类Main1的建立
class Main1 extends JPanel{ private JPanel panel1,panel2,panel3; private JLabel computer,vs,person; private ImageIcon ShiTou1,JianDao1,Bu1,kong; private JButton shitou2,jianDao2,bu2; private Random number; private int total=0,c=0,p=0; public Main1(){ number=new Random(); ShiTou1=new ImageIcon("石头.jpg");//定义图片 JianDao1=new ImageIcon("剪刀.jpg"); Bu1=new ImageIcon("布.jpg"); kong=new ImageIcon("VS.jpg"); person=new JLabel(new ImageIcon("玩家.jpg")); vs=new JLabel(kong); computer=new JLabel(new ImageIcon("电脑.jpg")); panel1=new JPanel();//定义面板 panel1.setBackground(Color.BLACK); panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS)); panel1.add(person); panel1.add(vs); panel1.add(computer); panel1.setPreferredSize(new Dimension(400, 200)); shitou2=new JButton("石头"); shitou2.addActionListener(new Mouse()); jianDao2=new JButton("剪刀"); jianDao2.addActionListener(new Mouse()); bu2=new JButton("布"); bu2.addActionListener(new Mouse()); panel2=new JPanel(); panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS)); panel2.add(shitou2); panel2.add(jianDao2); panel2.add(bu2); panel2.setPreferredSize(new Dimension(400, 200)); panel2.setBackground(new Color(100, 100, 100)); panel3=new JPanel(); panel3.setBackground(new Color(100, 100, 100)); panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS)); panel3.add(panel1); panel3.add(panel2); add(panel3); panel3.setPreferredSize(new Dimension(500, 300)); } private class Mouse implements ActionListener{//负责创建监视器的类 public void actionPerformed(ActionEvent event){//监视器 int num=number.nextInt(3); if(event.getSource()==shitou2){ person.setIcon(ShiTou1); if(num==0){ total=total+1; vs.setIcon(kong); p=p+1; computer.setIcon(JianDao1); } if(num==1){ total=total+1; vs.setIcon(kong); computer.setIcon(ShiTou1); } if(num==2){ total=total+1; c=c+1; vs.setIcon(kong); computer.setIcon(Bu1); } } if(event.getSource()==jianDao2){ person.setIcon(JianDao1); if(num==0){ total=total+1; vs.setIcon(kong); computer.setIcon(JianDao1); } if(num==1){ total=total+1; c=c+1; vs.setIcon(kong); computer.setIcon(ShiTou1); } if(num==2){ total=total+1; p=p+1; vs.setIcon(kong); computer.setIcon(Bu1); } } if(event.getSource()==bu2){ person.setIcon(Bu1); if(num==0){ total=total+1; vs.setIcon(kong); c=c+1; computer.setIcon(JianDao1); } if(num==1){ total=total+1; vs.setIcon(kong); p=p+1; computer.setIcon(ShiTou1); } if(num==2){ total=total+1; vs.setIcon(kong); computer.setIcon(Bu1); } } if(event.getSource()==jianDao2||event.getSource()==shitou2||event.getSource()==bu2) { int again=JOptionPane.showConfirmDialog(panel2,"比赛共进行:"+total+"次,玩家胜"+p+"次,电脑胜:"+c+"次。 是否继续?","提示", JOptionPane.YES_NO_OPTION); if(again==JOptionPane.NO_OPTION) System.out.println("玩家胜:"+p+"次,输:"+c+"次,电脑胜:"+c+"次,输:"+p+"次,共进行:"+total+"次"); if(again==JOptionPane.NO_OPTION) System.exit(0); } } } }
主类Main
public class Main{ public static void main(String[] args){ Main1 test=new Main1(); JFrame frame=new JFrame("石头剪刀布"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(test); frame.pack(); frame.setVisible(true);//窗口可见 } }
3.项目亮点
1.使用BoxLayout布局
2.确认框操作
3.ActionEvent事件
4.自动调节窗口适应组件大小及布局