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

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

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

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

Q&A

解決済

2回答

1454閲覧

JButtonを設置したJPanelに、その位置に初めてマウスカーソルを合わせるまでボタンが出現しないのを解決したい

raiboz1115

総合スコア4

Java

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

1グッド

0クリップ

投稿2021/07/22 04:40

編集2021/07/22 09:01

setLayout(null)をした後setBoundsで位置指定して画像を貼ったJPanelにJButtonを設置すると、その位置に初めてマウスカーソルを合わせるまでボタンが出現しないのを解決したいです。ちなみにsetLayout(null)をした後のJPanelでもsetBoundsで画像を貼らなければボタンは最初から見えています。どういういう状態かは実際に[Game.png],[casino.png]の部分にお好きな画像を入れて試していただければわかるかを思います。
setBoundsで画像を貼るとダメなのでしょうか?できれば背景画像は設定したく、ボタンの位置指定もしたいためsetLayout(null)とsetBoundsを使いたいです(ほかの方法があれば教えていただけると幸いです)。

Java

1 2import java.awt.CardLayout; 3import java.awt.Color; 4import java.awt.event.ActionEvent; 5import java.awt.event.ActionListener; 6import java.awt.event.ItemEvent; 7import java.awt.event.ItemListener; 8 9import javax.swing.ImageIcon; 10import javax.swing.JButton; 11import javax.swing.JComboBox; 12import javax.swing.JFrame; 13import javax.swing.JLabel; 14import javax.swing.JPanel; 15 16public class BJFrame extends JFrame implements ActionListener, ItemListener { 17 JPanel panel; 18 19 CardLayout layout; 20 private JLabel casinol = new JLabel(""); 21 private JLabel gamel = new JLabel(""); 22 private JButton rb1, rb2, b1, b2, b3, b4, b5, b6; 23 private String[] buyin = { "$10", "$50", "$1000", "$5000", "$10000" }; 24 private JComboBox combo = new JComboBox(buyin); 25 private int stack = 0; 26 27 public static void main(String[] args) { 28 BJFrame frame = new BJFrame(); 29 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 30 frame.setBounds(-8, 0, 1940, 1050); 31 frame.setTitle("BlackJack"); 32 frame.setVisible(true); 33 } 34 35 BJFrame() { 36 //HomeView(card1) 37 JPanel card1 = new JPanel(); 38 card1.setLayout(null); 39 casinol.setIcon(new ImageIcon("casino.png")); 40 casinol.setBounds(0, 0, 1980, 1050); 41 card1.add(casinol); 42 43 this.b1 = new JButton("START");//ホーム画面→BuyIn 44 this.b1.setBounds(840, 680, 300, 100); 45 this.b1.setBackground(Color.white); 46 this.rb1 = new JButton("RULE");//ホーム画面でルール確認 47 this.rb1.setBounds(840, 800, 300, 100); 48 this.rb1.setBackground(Color.white); 49 card1.add(this.rb1); 50 51 // BuyinView(card2) 52 JPanel card2 = new JPanel(); 53 card2.setBackground(Color.BLACK); 54 card2.setLayout(null); 55 this.b2 = new JButton("Back");//BuyIn→ホーム 56 this.b3 = new JButton("Let's Play!");//BuyIn→ゲーム 57 this.b2.setBounds(840, 900, 300, 100); 58 this.b3.setBounds(840, 780, 300, 100); 59 this.combo.setBounds(840, 550, 300, 100); 60 card2.add(combo); 61 62 //GameView(card3) 63 JPanel card3 = new JPanel(); 64 card3.setLayout(null); 65 gamel.setIcon(new ImageIcon("Game.png")); 66 gamel.setBounds(0, 0, 1980, 1050); 67 card3.add(gamel); 68 69 70 this.b4 = new JButton("H");//ゲーム画面→ホーム 71 this.b4.setBounds(12, 18, 50, 50); 72 this.b4.setBackground(Color.white); 73 this.b5 = new JButton("$");//ゲーム画面→BuyIn 74 this.b5.setBounds(11, 63, 50, 50); 75 this.b5.setBackground(Color.white); 76 77 this.rb2 = new JButton("R");//ゲーム画面でルール確認 78 this.rb2.setBounds(11, 113, 50, 50); 79 this.rb2.setBackground(Color.white); 80 card3.add(this.rb2); 81 82 /* CardLayout準備 */ 83 this.panel = new JPanel(); 84 this.layout = new CardLayout();//CardLayoutの作成 85 this.panel.setLayout(this.layout); 86 /* panelにViewを追加 */ 87 this.panel.add(card1, "HomeView"); 88 this.panel.add(card2, "BuyinView"); 89 this.panel.add(card3, "GameView"); 90 91 /* カード移動用ボタン */ 92 this.b1.addActionListener(this); 93 this.b1.setActionCommand("BuyinView"); 94 this.b2.addActionListener(this); 95 this.b2.setActionCommand("HomeView"); 96 this.b3.addActionListener(this); 97 this.b3.setActionCommand("GameView"); 98 this.b4.addActionListener(this); 99 this.b4.setActionCommand("HomeView"); 100 this.b5.addActionListener(this); 101 this.b5.setActionCommand("BuyinView"); 102 103 card1.add(this.b1); 104 card2.add(this.b2); 105 card2.add(this.b3); 106 card3.add(this.b4); 107 card3.add(this.b5); 108 getContentPane().add(this.panel, BorderLayout.CENTER); 109 combo.addItemListener(this); 110 this.rb1.addActionListener(this); 111 this.rb2.addActionListener(this); 112 } 113 114 public void actionPerformed(ActionEvent e) { 115 if (e.getSource() == this.rb1 || e.getSource() == this.rb2) {//ルール確認ボタンがどちらか押されたとき 116 System.out.println("a"); 117 new Rule(); 118 } 119 String cmd = e.getActionCommand(); 120 layout.show(this.panel, cmd); 121 } 122 123 @Override 124 public void itemStateChanged(ItemEvent e) { 125 if (e.getStateChange() == 1) { 126 String str = ((String) combo.getSelectedItem()).replace("$", ""); 127 this.stack = Integer.parseInt(str); 128 } 129 } 130} 131 132 133 134import java.awt.BorderLayout; 135import java.awt.CardLayout; 136import java.awt.Color; 137import java.awt.event.ActionEvent; 138import java.awt.event.ActionListener; 139 140import javax.swing.ImageIcon; 141import javax.swing.JButton; 142import javax.swing.JFrame; 143import javax.swing.JLabel; 144import javax.swing.JPanel; 145 146public class Rule extends JFrame implements ActionListener { 147 JPanel panel; 148 CardLayout layout; 149 private JLabel r1l = new JLabel(""); 150 private JLabel r2l = new JLabel(""); 151 private JLabel r3l = new JLabel(""); 152 private JLabel r4l = new JLabel(""); 153 154 public Rule() { 155 this.setSize(300, 700); 156 //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 157 this.setBounds(355, 100, 300, 580); 158 this.setTitle("BJ's Rule"); 159 160 JPanel rcard1 = new JPanel(); 161 rcard1.setBackground(new Color(215, 245, 215)); 162 rcard1.setLayout(null); 163 this.r1l.setIcon(new ImageIcon("rule1.png")); 164 this.r1l.setBounds(0, 0, 300, 500); 165 rcard1.add(this.r1l); 166 167 JPanel rcard2 = new JPanel(); 168 rcard2.setBackground(new Color(215, 245, 215)); 169 rcard2.setLayout(null); 170 this.r2l.setIcon(new ImageIcon("rule2.png")); 171 this.r2l.setBounds(0, 0, 300, 500); 172 rcard2.add(this.r2l); 173 174 JPanel rcard3 = new JPanel(); 175 rcard3.setBackground(new Color(215, 245, 215)); 176 rcard3.setLayout(null); 177 this.r3l.setIcon(new ImageIcon("rule3.png")); 178 this.r3l.setBounds(0, 0, 300, 500); 179 rcard3.add(this.r3l); 180 181 JPanel rcard4 = new JPanel(); 182 rcard4.setBackground(new Color(215, 245, 215)); 183 rcard4.setLayout(null); 184 this.r4l.setIcon(new ImageIcon("rule4.png")); 185 this.r4l.setBounds(0, 0, 300, 500); 186 rcard4.add(this.r4l); 187 188 /* CardLayout準備 */ 189 this.panel = new JPanel(); 190 this.layout = new CardLayout();//CardLayoutの作成 191 this.panel.setLayout(this.layout); 192 //panelにViewを追加 193 this.panel.add(rcard1, "r1View"); 194 this.panel.add(rcard2, "r2View"); 195 this.panel.add(rcard3, "r3View"); 196 this.panel.add(rcard4, "r4View"); 197 198 /* カード移動用ボタン */ 199 JButton rb1 = new JButton("1"); 200 rb1.addActionListener(this); 201 rb1.setActionCommand("r1View"); 202 203 JButton rb2 = new JButton("2"); 204 rb2.addActionListener(this); 205 rb2.setActionCommand("r2View"); 206 207 JButton rb3 = new JButton("3"); 208 rb3.addActionListener(this); 209 rb3.setActionCommand("r3View"); 210 211 JButton rb4 = new JButton("4"); 212 rb4.addActionListener(this); 213 rb4.setActionCommand("r4View"); 214 215 JPanel btnPanel = new JPanel(); 216 btnPanel.add(rb1); 217 btnPanel.add(rb2); 218 btnPanel.add(rb3); 219 btnPanel.add(rb4); 220 221 getContentPane().add(this.panel, BorderLayout.CENTER); 222 getContentPane().add(btnPanel, BorderLayout.PAGE_END); 223 this.setVisible(true); 224 } 225 226 @Override 227 public void actionPerformed(ActionEvent e) { 228 String cmd = e.getActionCommand(); 229 layout.show(this.panel, cmd); 230 } 231 232} 233 234
TN8001👍を押しています

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

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

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

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

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

jimbe

2021/07/22 08:32

タイトルを見れば分かるだろうというお考えかと思いますが、どのようなイメージをされていて何を試されて何が分からなかったのか等の説明をして頂けないでしょうか。 また、コードは見るだけでなくコピペして動作の確認にも使用します。枠の右上にある [+] をクリックするだけでコピーできますのでそのまま開発環境で張り付けられますが、複数のファイルを一つに繋げたり("---"のように)ソースコードとしては不要なモノを付加されるなどしてしまうと、コピーしたコードを修正しなければならなくなり、もしその過程で間違いが起きれば、最悪質問された状況を再現できない場合も考えられます。 ですので、コードは極力「そのまま」でご提示ください。
raiboz1115

2021/07/22 09:14

ご指摘ありがとうございます。質問を変更いたしました。コードも無駄な部分を消去いたしましたので、ご確認いただけると幸いです。
guest

回答2

0

Home/Buyin/Game の各パネルをクラス化しつつ、背景用に BackImagePanel クラスを作ってみました。

java

1import java.awt.BorderLayout; 2import java.awt.CardLayout; 3import java.awt.Color; 4import java.awt.Dimension; 5import java.awt.Graphics; 6import java.awt.Image; 7import java.awt.LayoutManager; 8import java.awt.event.ActionEvent; 9import java.awt.event.ActionListener; 10import java.awt.event.ItemEvent; 11import java.awt.event.ItemListener; 12import java.io.File; 13import java.io.IOException; 14 15import javax.imageio.ImageIO; 16import javax.swing.JButton; 17import javax.swing.JComboBox; 18import javax.swing.JFrame; 19import javax.swing.JPanel; 20 21public class BJFrame extends JFrame { 22 23 private int stack = 0; 24 25 public static void main(String[] args) { 26 BJFrame frame = new BJFrame(); 27 frame.setVisible(true); 28 } 29 30 BJFrame() { 31 super("BlackJack"); 32 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 33 setBounds(-8, 0, 1940, 1050); 34 35 getContentPane().add(new MainPanel(), BorderLayout.CENTER); 36 } 37 38 private class MainPanel extends JPanel implements ActionListener { 39 private CardLayout layout; 40 41 MainPanel() { 42 super(null); 43 44 /* CardLayout準備 */ 45 layout = new CardLayout();//CardLayoutの作成 46 setLayout(layout); 47 48 /* Viewを追加 */ 49 add(new HomePanel(this), HomePanel.PANEL_NAME); 50 51 BuyinPanel buyinPanel = new BuyinPanel(this); 52 buyinPanel.setBuyinListener(new BuyinPanel.BuyinListener() { 53 @Override 54 public void changeBuyin(int stack) { 55 BJFrame.this.stack = stack; 56 } 57 }); 58 add(buyinPanel, BuyinPanel.PANEL_NAME); 59 60 add(new GamePanel(this), GamePanel.PANEL_NAME); 61 } 62 63 public void actionPerformed(ActionEvent e) { 64 String cmd = e.getActionCommand(); 65 if(cmd.equals("Rule")) { 66 System.out.println("a"); 67 new Rule(); 68 } else { 69 layout.show(this, cmd); 70 } 71 } 72 } 73 74 /** 背景画像を設定できるパネル */ 75 private static class BackImagePanel extends JPanel { 76 private Image background; 77 public BackImagePanel(LayoutManager layout) { 78 super(layout); 79 } 80 public void setBackground(String filename) throws IOException { 81 if(filename != null) { 82 background = ImageIO.read(new File(filename)); 83 } 84 } 85 @Override 86 public void paintComponent(Graphics g) { 87 super.paintComponent(g); 88 if(background != null) { 89 g.drawImage(background, 0, 0, null); 90 } 91 } 92 } 93 94 /** ホームパネル */ 95 private static class HomePanel extends BackImagePanel { 96 public static String PANEL_NAME = "HomeView"; 97 98 HomePanel(ActionListener listener){ 99 super(null); 100 try { 101 setBackground("casino.png"); 102 } catch (IOException e) { 103 e.printStackTrace(); 104 } 105 setPreferredSize(new Dimension(1980, 1050)); 106 107 JButton startButton = new JButton("START");//ホーム画面→BuyIn 108 startButton.setBackground(Color.white); 109 startButton.setActionCommand(BuyinPanel.PANEL_NAME); 110 startButton.addActionListener(listener); 111 startButton.setBounds(840, 680, 300, 100); 112 add(startButton); 113 114 JButton ruleButton = new JButton("RULE");//ホーム画面でルール確認 115 ruleButton.setBackground(Color.white); 116 ruleButton.setActionCommand("Rule"); 117 ruleButton.addActionListener(listener); 118 ruleButton.setBounds(840, 800, 300, 100); 119 add(ruleButton); 120 } 121 } 122 123 /** 掛け金パネル */ 124 private static class BuyinPanel extends JPanel { 125 public static String PANEL_NAME = "BuyinView"; 126 127 public interface BuyinListener { 128 void changeBuyin(int stack); 129 } 130 private BuyinListener buyinListener; 131 132 private static String[] buyin = { "$10", "$50", "$1000", "$5000", "$10000" }; 133 private int stack = 0; 134 135 BuyinPanel(ActionListener listener) { 136 super(null); 137 setBackground(Color.BLACK); 138 139 JButton backButton = new JButton("Back");//BuyIn→ホーム 140 backButton.setActionCommand(HomePanel.PANEL_NAME); 141 backButton.addActionListener(listener); 142 backButton.setBounds(840, 900, 300, 100); 143 add(backButton); 144 145 JButton homeButton = new JButton("Let's Play!");//BuyIn→ゲーム 146 homeButton.setActionCommand(GamePanel.PANEL_NAME); 147 homeButton.addActionListener(listener); 148 homeButton.setBounds(840, 780, 300, 100); 149 add(homeButton); 150 151 JComboBox<String> buyinCombo = new JComboBox<String>(buyin); 152 buyinCombo.addActionListener(new ActionListener() { 153 @Override 154 public void actionPerformed(ActionEvent e) { 155 String str = ((String)buyinCombo.getSelectedItem()).replace("$", ""); 156 int v = Integer.parseInt(str); 157 if(buyinListener != null && v != stack) { 158 stack = v; 159 buyinListener.changeBuyin(stack); 160 } 161 } 162 }); 163 buyinCombo.setBounds(840, 550, 300, 100); 164 add(buyinCombo); 165 } 166 void setBuyinListener(BuyinListener l) { 167 buyinListener = l; 168 } 169 } 170 171 /** ゲームパネル */ 172 private static class GamePanel extends BackImagePanel { 173 public static String PANEL_NAME = "GameView"; 174 175 GamePanel(ActionListener listener) { 176 super(null); 177 try { 178 setBackground("Game.png"); 179 } catch (IOException e) { 180 e.printStackTrace(); 181 } 182 setPreferredSize(new Dimension(1980, 1050)); 183 184 JButton homeButton = new JButton("H");//ゲーム画面→ホーム 185 homeButton.setBackground(Color.white); 186 homeButton.setActionCommand(HomePanel.PANEL_NAME); 187 homeButton.addActionListener(listener); 188 homeButton.setBounds(12, 18, 50, 50); 189 add(homeButton); 190 191 JButton buyinButton = new JButton("$");//ゲーム画面→BuyIn 192 buyinButton.setBackground(Color.white); 193 buyinButton.setActionCommand(BuyinPanel.PANEL_NAME); 194 buyinButton.addActionListener(listener); 195 buyinButton.setBounds(11, 63, 50, 50); 196 add(buyinButton); 197 198 JButton ruleButton = new JButton("R");//ゲーム画面でルール確認 199 ruleButton.setBackground(Color.white); 200 ruleButton.setActionCommand("Rule"); 201 ruleButton.addActionListener(listener); 202 ruleButton.setBounds(11, 113, 50, 50); 203 add(ruleButton); 204 } 205 } 206}

投稿2021/07/22 11:46

編集2021/07/22 13:56
jimbe

総合スコア12648

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

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

0

ベストアンサー

基本的に先にaddしたものが上に、後にaddしたものが下になります。

一番手軽な解消法はJLabelのaddを、JButtonのaddより後に回すことです。

ほかにも手はあるでしょうが、ここまでできているんであれば↑でいいでしょう。

投稿2021/07/22 09:34

TN8001

総合スコア9326

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

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

raiboz1115

2021/07/22 09:39

完璧に解決しました。感動しました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問