您好,登錄后才能下訂單哦!
使用Java制作一個石頭剪刀布小游戲?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
代碼:
package com.neusoft.test; import java.awt.BorderLayout; import java.awt.Choice; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Test05 extends JFrame implements ActionListener, ItemListener{ int m = 1; int n = 2; int peace = 0; int win = 0; int lose = 0; int all = 0; JButton jb1,jb2; JLabel jl1,jl2,jl2i,jl3,jl3i,jl4,jl5; ImageIcon img1,img2; Choice ch; JPanel jp1,jp2,jp3,jp4,jp5,jp6; public Test05(){ jb1 = new JButton("確定出拳"); jb2 = new JButton("退出"); //設置監聽 jb1.addActionListener(this); jb2.addActionListener(this); jl1 = new JLabel("猜拳小游戲"); jl2 = new JLabel("我方出拳"); jl3 = new JLabel("敵方出拳"); jl4 = new JLabel("請出拳..."); jl5 = new JLabel("您玩了"+all+"局,平了"+peace+"局,贏了"+win+"局,輸了"+lose+"局"); System.out.println(m); img1 = new ImageIcon("D:/mkn/test/img/"+m+".png"); img2 = new ImageIcon("D:/mkn/test/img/"+n+".png"); jl2i = new JLabel(img1); jl3i = new JLabel(img2); ch = new Choice(); ch.add("石頭"); ch.add("剪刀"); ch.add("布"); ch.addItemListener(this); jp1 = new JPanel(new BorderLayout()); jp2 = new JPanel(); jp3 = new JPanel(new BorderLayout()); jp4 = new JPanel(new BorderLayout()); jp5 = new JPanel(new GridLayout(5,1)); jp6 = new JPanel(new BorderLayout()); jp2.setSize(600, 25); jp2.setBackground(Color.red); jp3.setSize(280, 300); jp4.setSize(280, 300); jp5.setSize(40, 300); jp6.setSize(600, 25); // jp6.setBackground(Color.GRAY); jp2.add(jl1); //標題 jp3.add(jl2,BorderLayout.NORTH); // jp3.add(jl2i,BorderLayout.CENTER); jp4.add(jl3,BorderLayout.NORTH); jp4.add(jl3i, BorderLayout.CENTER);//敵方出拳 jp5.add(ch); jp5.add(jb1); jp5.add(jb2); jp6.add(jl4,BorderLayout.WEST); jp6.add(jl5,BorderLayout.EAST);//結果 jp1.add(jp2,BorderLayout.NORTH); jp1.add(jp3,BorderLayout.WEST); jp1.add(jp4,BorderLayout.CENTER); jp1.add(jp5,BorderLayout.EAST); jp1.add(jp6,BorderLayout.SOUTH); this.add(jp1); this.setTitle("猜拳小游戲"); this.setSize(540,300); // this.setLayout(new BorderLayout()); this.setLocationRelativeTo(null); //居中 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); //窗口大小可變 } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand()=="確定出拳"){ n = (int)(1+Math.random()*(3)); jl3i.setIcon(new ImageIcon("D:/mkn/test/img/"+n+".png")); if (m - n == 0) { jl4.setText("您和計算機平局"); peace++; } if (m - n == -1||m - n == 2) { jl4.setText("您贏了!!!"); win++; } if (m - n == -2||m - n == 1) { jl4.setText("您輸了!!!"); lose++; } all++; jl5.setText("您玩了"+all+"局,平了"+peace+"局,贏了"+win+"局,輸了"+lose+"局"); // System.out.println(n); }else{ this.dispose(); } } public static void main(String[] args) { Test05 test05 = new Test05(); } @Override public void itemStateChanged(ItemEvent e) { if(e.getSource()==ch){ Choice temp=(Choice)e.getSource(); if (temp.getSelectedItem() == "石頭") { m = 1; } if (temp.getSelectedItem() == "剪刀") { m = 2; } if (temp.getSelectedItem() == "布") { m = 3; } jl2i.setIcon(new ImageIcon("D:/mkn/test/img/"+m+".png")); // System.out.println(m); } } }
看完上述內容,你們掌握使用Java制作一個石頭剪刀布小游戲的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。