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
回答2件
あなたの回答
tips
プレビュー