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

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

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

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

Q&A

解決済

2回答

1155閲覧

ボタンを押すとランダム位置に新しいボタンが追加されるようにしたい

raiboz1115

総合スコア4

Java

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

0グッド

0クリップ

投稿2021/07/22 18:07

編集2021/07/22 18:13

JPanel上のボタンを押すとランダム位置に新しいボタンが追加されるようにしたく以下のコード書いたのですが、新しく追加されたボタンがマウスカーソルを合わせるまで見えるようになりません。改善方法が教えていただきたいです。加えて、以下の二点もできるようにしたいです。
・増やしたすべてのボタンを一斉に削除したい
・このコードでは100個のボタンしか作れないので、上限なしで作れるようにしたい

Java

1import java.awt.event.ActionEvent; 2import java.awt.event.ActionListener; 3import java.util.Random; 4 5import javax.swing.JButton; 6import javax.swing.JFrame; 7import javax.swing.JPanel; 8 9public class Ctest extends JFrame implements ActionListener { 10 private JButton b1; 11 private JButton[] tb = new JButton[100]; 12 int i = 0; 13 JPanel p = new JPanel(); 14 15 public static void main(String[] args) { 16 Ctest frame = new Ctest(); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setBounds(0, 0, 800, 800); 19 frame.setTitle("ButtonPanel"); 20 frame.setVisible(true); 21 22 } 23 24 Ctest() { 25 p.setLayout(null); 26 this.b1 = new JButton("増やす"); 27 this.b1.setBounds(80, 80, 100, 100); 28 p.add(b1); 29 30 this.b1.addActionListener(this); 31 this.getContentPane().add(p); 32 } 33 34 @Override 35 public void actionPerformed(ActionEvent e) { 36 Random random1 = new Random(); 37 int xrv = random1.nextInt(750); 38 Random random2 = new Random(); 39 int yrv = random2.nextInt(750); 40 41 this.tb[i] = new JButton(i + ""); 42 this.tb[i].setBounds(xrv, yrv, 50, 50); 43 p.add(tb[i]); 44 i++; 45 } 46}

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

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

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

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

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

m.ts10806

2021/07/22 21:11

固定値だとどうでしょう。
raiboz1115

2021/07/22 21:33

固定値でもマウスカーソルを合わせなければ可視化状態にはなりませんでした…。
guest

回答2

0

ベストアンサー

以下、TN8001さんの回答と適宜組合せてみてください。

新しく追加されたボタンがマウスカーソルを合わせるまで見えるようになりません

省略。

増やしたすべてのボタンを一斉に削除したい

diff

1@@ -24,6 +24,19 @@ 2 this.b1.setBounds(80, 80, 100, 100); 3 p.add(b1); 4 5+ final JButton delButton = new JButton("del all"); 6+ delButton.setBounds(180, 180, 100, 100); 7+ p.add(delButton); 8+ delButton.addActionListener((i) -> { 9+ Ctest.this.setVisible(false); 10+ for (int index = 0; index < tb.length; index++) { 11+ if (tb[index] != null) 12+ p.remove(tb[index]); 13+ tb[index] = null; 14+ } 15+ Ctest.this.setVisible(true); 16+ }); 17+ 18 this.b1.addActionListener(this); 19 this.getContentPane().add(p); 20 }

上限なしで作れるようにしたい

diff

1@@ -1,11 +1,13 @@ 2 import javax.swing.*; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5+import java.util.ArrayList; 6+import java.util.List; 7 import java.util.Random; 8 9 public class Ctest extends JFrame implements ActionListener { 10 private JButton b1; 11- private JButton[] tb = new JButton[100]; 12+ private List<JButton> tb = new ArrayList<>(); 13 int i = 0; 14 JPanel p = new JPanel(); 15 16@@ -30,14 +32,17 @@ 17 18 @Override 19 public void actionPerformed(ActionEvent e) { 20 Random random1 = new Random(); 21 int xrv = random1.nextInt(750); 22 Random random2 = new Random(); 23 int yrv = random2.nextInt(750); 24 25- this.tb[i] = new JButton(i + ""); 26- this.tb[i].setBounds(xrv, yrv, 50, 50); 27- p.add(tb[i]); 28+ final JButton jbutton = new JButton(i + ""); 29+ tb.add(jbutton); 30+ jbutton.setBounds(xrv, yrv, 50, 50); 31+ p.add(jbutton); 32 i++; 33 } 34 }

投稿2021/07/23 07:33

shiketa

総合スコア3990

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

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

TN8001

2021/07/23 07:42

補足ありがとうございます。 追加質問部分見ていませんでした^^;
raiboz1115

2021/07/23 08:24

追加質問にもわざわざ答えてくださってありがとうございます! 意外にもどれもシンプルにできるんですね、とても勉強になりました。 回答者の方々、ありがとうございました。
raiboz1115

2021/07/23 10:40

回答者さん方のを参考に書いてみたのですが、一つだけエラーが出ます。 配列からリストに変更する際でtb[index] = null;をtb.get(index) = null;としたのですが、「代入の左辺は変数でなければなりません」と出てきます。 どうやったら直りますか?
shiketa

2021/07/23 12:04

全部削除の例は、オリジナルの`private JButton[] tb = new JButton[100];`を対象としています。無制限のほうは`private List<JButton> tb = new ArrayList<>();`です。 `tb[index] = null;`ではなく、ループを抜けてから`tb.clear()`すれはよろしいかと。
shiketa

2021/07/23 12:15

あと、全削除ボタンの処理でsetVisible(false);/setVisible(true;でごまかしている部分は、TN8001さん回答のrepaint/validate/revalidateで対応したほうがいいでしょう。
guest

0

端的に言えばrepaintを呼んでください(p.repaint();

しかしレイアウトを使用している場合は、repaintでは更新されません。
validateあるいはrevalidateが必要です。

このあたり難しいというか、私はいつまでたっても覚えられませんね^^;

Java

1import java.awt.GridLayout; 2import java.awt.event.ActionEvent; 3import java.awt.event.ActionListener; 4import java.util.Random; 5import javax.swing.BorderFactory; 6import javax.swing.JButton; 7import javax.swing.JFrame; 8import javax.swing.JPanel; 9 10public class Sample extends JFrame { 11 public static void main(String[] args) { 12 Sample frame = new Sample(); 13 frame.setSize(800, 600); 14 frame.setLocationRelativeTo(null); 15 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 17 frame.setLayout(new GridLayout(1, 2)); 18 19 JPanel p1 = new NullLayout(); 20 p1.setBorder(BorderFactory.createTitledBorder("null Layout")); 21 frame.add(p1); 22 23 JPanel p2 = new UseLayout(); 24 p2.setBorder(BorderFactory.createTitledBorder("use Layout")); 25 frame.add(p2); 26 27 frame.setVisible(true); 28 } 29 30 31 static class NullLayout extends JPanel implements ActionListener { 32 NullLayout() { 33 setLayout(null); 34 JButton button = new JButton("増やす"); 35 button.setBounds(20, 20, 100, 100); 36 button.addActionListener(this); 37 add(button); 38 } 39 40 private Random random = new Random(); 41 private int i = 0; 42 43 @Override public void actionPerformed(ActionEvent e) { 44 i++; 45 int x = random.nextInt(getWidth() - 50); 46 int y = random.nextInt(getHeight() - 50); 47 48 JButton button = new JButton(i + ""); 49 button.setBounds(x, y, 50, 50); 50 add(button); 51 52 repaint(); 53 } 54 } 55 56 static class UseLayout extends JPanel implements ActionListener { 57 UseLayout() { 58 JButton button = new JButton("増やす"); 59 button.addActionListener(this); 60 add(button); 61 } 62 63 private Random random = new Random(); 64 private int i = 0; 65 66 @Override public void actionPerformed(ActionEvent e) { 67 i++; 68 int x = random.nextInt(getWidth() - 50); 69 int y = random.nextInt(getHeight() - 50); 70 71 JButton button = new JButton(i + ""); 72 add(button); 73 74 validate(); 75 // revalidate(); // あるいは 76 77 // repaint(); // 場合により必要? 78 } 79 } 80}

Component#repaint (Java Platform SE 8 )
Container#validate (Java Platform SE 8 )
JComponent#revalidate (Java Platform SE 8 )

Painting in AWT and Swing

投稿2021/07/23 03:15

TN8001

総合スコア9396

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問