質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

2回答

245閲覧

指定時間後にパネルの一部の「ボタン画像」を入れ替えるにはどうすれば良いですか?

Pcios

総合スコア5

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

1グッド

0クリップ

投稿2020/01/20 13:36

java

1import java.util.Random; 2import java.util.Timer; 3import java.util.TimerTask; 4import javax.swing.ImageIcon; 5import javax.swing.JButton; 6import javax.swing.JFrame; 7import javax.swing.JPanel; 8 9class TASK extends TimerTask{ 10 // タイマー処理のメソッドを誘発する 11 public void run(){ 12 Fishing Act = new Fishing(); 13 Act.ROLL(); 14 } 15} 16 17public class Fishing extends JFrame 18{ 19 private static final long serialVersionUID = 1L; 20 21//クラス変数定義 22static JButton[] Bbt={new JButton(new ImageIcon("しーん.jpg")), 23new JButton(new ImageIcon("!.jpg")),new JButton(new ImageIcon("!!.jpg")), 24new JButton(new ImageIcon("!!!.jpg")),new JButton(new ImageIcon("?.jpg"))}; 25static JButton CHANGEBt=Bbt[0]; 26JPanel p4 = new JPanel(); 27 28// タイマー処理(本命) 29public void ROLL(){ 30 int x= Atrandom(); 31  CHANGEBt.setBounds(0,0,0,0);   //この処理はうまくいく 32  CHANGEBt=Bbt[x]; 33  CHANGEBt.setBounds(378,200,375,280); // なにも起きない 34  p4.revalidate(); 35 p4.repaint(); 36} 37 38//1~4までの非等倍乱数を得る。 39 public int Atrandom(){ 40 int Ra[]={1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,4,4}; 41    Random r=new Random(); 42 int A=r.nextInt(20); int B = Ra[A]; 43 return B; 44 } 45 46// ゲーム画面 47public void MAINGAME(){ 48 setBounds(550, 100, 770, 800); 49  p4.setLayout(null); 50  p4.add(CHANGEBt); CHANGEBt.setBounds(378,200,375,280); 51 add("Center",p4); 52 setVisible(true); 53 Random r = new Random(); int R; 54 R=100*(r.nextInt(50) +1); // 100<= R <= 5000 55 Timer timer = new Timer(true); 56 57 timer.schedule(new TASK(),R); //タイマー処理は最後にコンパイラされる 58} 59//メインメソッド 60 public static void main(String[] args) 61 { 62 Fishing set = new Fishing(); 63 set.MAINGAME(); 64 } 65} 66 67 68 69 70 71
TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2020/01/20 14:25

コードは作られたままご張り付けてください. 回答の際, ご提示のコードをコピーペーストして再現致します. その際, 全角スペース等があると修正が必要になり, 何かの拍子に必要な個所を消してしまう等のトラブルによって再現できない場合があります.
guest

回答2

0

1ボタンで表示を切り替える形の一例です.
ボタンの処理を Action 化し, タイマーを javax.swing.Timer にしてみました.
変数名・メソッド名等も変更しています.
onClick メソッドを設けて, ボタンが押された時の動作を Fishing クラスで行えるようにしました.

java

1import java.awt.event.ActionEvent; 2import java.awt.event.ActionListener; 3import java.util.Random; 4 5import javax.swing.AbstractAction; 6import javax.swing.Action; 7import javax.swing.ImageIcon; 8import javax.swing.JButton; 9import javax.swing.JFrame; 10import javax.swing.JPanel; 11import javax.swing.Timer; 12 13public class Fishing extends JFrame { 14 15 public static void main(String[] args) { 16 new Fishing().setVisible(true); 17 } 18 19 private final String imageFiles[]={ "しーん.jpg", "!.jpg", "!!.jpg", "!!!.jpg", "?.jpg" }; 20 private final int randomTable[]={1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,4,4}; 21 22 // ゲーム画面 23 public Fishing(){ 24 super("Fishing"); 25 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 26 setBounds(550, 100, 770, 800); 27 28 ButtonAction buttonAction = new ButtonAction(imageFiles, randomTable); 29 30 JButton button = new JButton(buttonAction); 31 button.setBounds(378,200,375,280); 32 33 JPanel panel = new JPanel(null); 34 panel.add(button); 35 add(panel); 36 37 buttonAction.startTimer(); 38 } 39 40 public void onClick(int imageNumber) { 41 System.out.println("'"+imageFiles[imageNumber]+"' ボタンが押された. (imageNumber="+imageNumber+")"); 42 } 43 44 private class ButtonAction extends AbstractAction { 45 private ImageIcon buttonImages[]; 46 private int imageNumber; 47 48 private int randomTable[]; 49 private Random random = new Random(); 50 51 ButtonAction(String imageFiles[], int randomTable[]) { 52 buttonImages = new ImageIcon[imageFiles.length]; 53 for(int i=0; i<buttonImages.length; i++) { 54 buttonImages[i] = new ImageIcon(imageFiles[i]); 55 } 56 this.randomTable = randomTable; 57 putImage(0); 58 } 59 private void putImage(int number) { 60 imageNumber = number; 61 putValue(Action.LARGE_ICON_KEY, buttonImages[number]); 62 } 63 //タイマー起動 64 void startTimer() { 65 int delay = 100 * (random.nextInt(50) + 1); //100~5000[ms] 66 Timer timer = new Timer(delay, new ActionListener(){ 67 public void actionPerformed(ActionEvent e) { 68 putImage(randomTable[random.nextInt(randomTable.length)]); 69 } 70 }); 71 timer.setRepeats(false); 72 timer.start(); 73 } 74 @Override 75 public void actionPerformed(ActionEvent e) { 76 onClick(imageNumber); 77 } 78 } 79}

投稿2020/01/20 15:23

編集2020/01/20 16:08
jimbe

総合スコア12545

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Pcios

2020/01/20 15:39

回答ありがとうございます! まだ知らない技術が多かったのですが、参考にして勉強させていただきます。
guest

0

ベストアンサー

まずTimerTasknew Fishing()はよくないですね。
staticなものがあるので結果的には動いていますが、無駄にJFrameを作っています。
内部クラスにすればFishingのメンバが見えるので中に入れました。

サイズで表示非表示を表現するなら、ボタンは全部Addされていないといけません。

Java

1public class Fishing extends JFrame { 2 private static final long serialVersionUID = 1L; 3 4 private Random random = new Random(); 5 private JButton[] Bbt = { 6 new JButton("しーん"), 7 new JButton("!"), 8 new JButton("!!"), 9 new JButton("!!!"), 10 new JButton("?"), 11 }; 12 private JButton CHANGEBt = Bbt[0]; 13 private JPanel p4 = new JPanel(); 14 15 public void ROLL() { 16 int x = Atrandom(); 17 CHANGEBt.setBounds(0, 0, 0, 0); 18 CHANGEBt = Bbt[x]; 19 CHANGEBt.setBounds(378, 200, 375, 280); 20 } 21 22 private static final int[] Ra = { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 }; 23 24 public int Atrandom() { 25 return Ra[random.nextInt(Ra.length)]; 26 } 27 28 class TASK extends TimerTask { 29 public void run() { 30 SwingUtilities.invokeLater(() -> ROLL()); 31 } 32 } 33 34 public void MAINGAME() { 35 setBounds(550, 100, 770, 800); 36 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 37 p4.setLayout(null); 38 p4.add(Bbt[0]); 39 p4.add(Bbt[1]); 40 p4.add(Bbt[2]); 41 p4.add(Bbt[3]); 42 p4.add(Bbt[4]); 43 44 CHANGEBt.setBounds(378, 200, 375, 280); 45 add(p4); 46 setVisible(true); 47 48 int R = 100 * (random.nextInt(50) + 1); 49 Timer timer = new Timer(true); 50 timer.schedule(new TASK(), R); 51 } 52 53 public static void main(String[] args) { 54 Fishing set = new Fishing(); 55 set.MAINGAME(); 56 } 57}

投稿2020/01/20 14:40

編集2023/07/18 21:30
TN8001

総合スコア9244

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Pcios

2020/01/20 15:38

すべてaddすることで解決しました!! ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問