前提・実現したいこと
Javaを始めたばかりの初心者です。いまGUIのswingを使ってじゃんけんアプリを作ろうとしています。
じゃんけんの回数は10回までに制限し、勝敗によって画像を変更したいです。
発生している問題・エラーメッセージ
じゃんけんの回数を10回に制限できない、また勝敗によって画像を変更できない。
該当のソースコード
Java
1 2import javax.swing.JFrame; 3import javax.swing.JLabel; 4import javax.sound.sampled.Clip; 5import javax.swing.Icon; 6import javax.swing.ImageIcon; 7import javax.swing.JButton; 8import javax.swing.JPanel; 9 10import java.awt.Color; 11import java.awt.Dimension; 12import java.awt.Font; 13import java.awt.event.ActionEvent; 14import java.awt.event.ActionListener; 15import java.io.File; 16import java.util.Random; 17 18public class Janken extends JFrame implements ActionListener { 19 20 private static final long serialVersionUID = 1L; 21 22 JButton rock; 23 JButton scissors; 24 JButton paper; 25 JLabel label; 26 JLabel image; 27 JLabel image2; 28 29 public Janken(String title) { 30 super(title); 31 setBounds(900, 900, 900, 900); 32 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 33 JPanel p = new JPanel(); 34 35 p.setLayout(null); 36 label = new JLabel("サザエさんと、じゃんけんしましょ", JLabel.CENTER); 37 label.setFont(new Font(null, Font.BOLD, 30)); 38 image = new JLabel(new ImageIcon("C:\amslssn\pleiades\workspace\Lesson\src\javaHumanNet\sazae.jpg")); 39 image.setBounds(200, 100, 400, 400); 40 41 add(image); 42 add("North", label); 43 44 rock = new JButton(new ImageIcon("rock.gif")); 45 rock.setBounds(200, 500, 100, 100); 46 rock.addActionListener(this); 47 add(rock); 48 49 scissors = new JButton(new ImageIcon("scissors.gif")); 50 scissors.setBounds(340, 500, 100, 100); 51 scissors.addActionListener(this); 52 add(scissors); 53 54 paper = new JButton(new ImageIcon("paper.gif")); 55 paper.setBounds(470, 500, 100, 100); 56 paper.addActionListener(this); 57 add(paper); 58 59 p.setBackground(Color.gray); 60 getContentPane().add("Center", p); 61 setVisible(true); 62 63 } 64 65 public static void main(String[] args) { 66 new Janken("じゃんけんゲーム"); 67 68 } 69 70 public void actionPerformed(ActionEvent e) { 71 int countNum = 0; 72 73 int iYou = 0; 74 75 SoundTest st = new SoundTest(); 76 if (e.getSource() == scissors) { 77 iYou = 1; 78 79 } else if (e.getSource() == paper) { 80 iYou = 2; 81 } 82 Random r = new Random(); 83 int j = r.nextInt(3); 84 85 String[] Hand = { "グー", "チョキ", "パー" }; 86 87 if (j == iYou) { 88 label.setText("サザエさんは" + Hand[j] + "、引き分けです"); 89 90 Clip clip = st 91 .createClip(new File("C:\amslssn\pleiades\workspace\Lesson\src\javaHumanNet\hikiwake.wav")); 92 clip.start(); 93 94 } else if ((iYou == 0 && j == 1) || (iYou == 1 && j == 2) || (iYou == 2 && j == 0)) { 95 label.setText("サザエさんは" + Hand[j] + "、あなたの勝ちです"); 96 Clip clip = st 97 .createClip(new File("C:\amslssn\pleiades\workspace\Lesson\src\javaHumanNet\seikai.wav")); 98 clip.start(); 99 // Thread.sleep(3000); 100 101 } else { 102 label.setText("サザエさんは" + Hand[j] + "、サザエさんの勝ちです"); 103 Clip clip = st 104 .createClip(new File("C:\amslssn\pleiades\workspace\Lesson\src\javaHumanNet\fuseikai.wav")); 105 clip.start(); 106 107 } 108 109 } 110 111}
試したこと
actionPerformedメソッド内でfor文やWhile文を使ってじゃんけんの回数を制限しようとしましたが、上手くいきませんでした。また勝敗と引き分けの条件文内で新しくimageインスタンスを作成し、addメソッドを使って新しい画像に更新しようとしましたが駄目でした。
回答1件
あなたの回答
tips
プレビュー