setLayout(null)をした後setBoundsで位置指定して画像を貼ったJPanelにJButtonを設置すると、その位置に初めてマウスカーソルを合わせるまでボタンが出現しないのを解決したいです。ちなみにsetLayout(null)をした後のJPanelでもsetBoundsで画像を貼らなければボタンは最初から見えています。どういういう状態かは実際に[Game.png],[casino.png]の部分にお好きな画像を入れて試していただければわかるかを思います。
setBoundsで画像を貼るとダメなのでしょうか?できれば背景画像は設定したく、ボタンの位置指定もしたいためsetLayout(null)とsetBoundsを使いたいです(ほかの方法があれば教えていただけると幸いです)。
Java
import java.awt.CardLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class BJFrame extends JFrame implements ActionListener, ItemListener { JPanel panel; CardLayout layout; private JLabel casinol = new JLabel(""); private JLabel gamel = new JLabel(""); private JButton rb1, rb2, b1, b2, b3, b4, b5, b6; private String[] buyin = { "$10", "$50", "$1000", "$5000", "$10000" }; private JComboBox combo = new JComboBox(buyin); private int stack = 0; public static void main(String[] args) { BJFrame frame = new BJFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(-8, 0, 1940, 1050); frame.setTitle("BlackJack"); frame.setVisible(true); } BJFrame() { //HomeView(card1) JPanel card1 = new JPanel(); card1.setLayout(null); casinol.setIcon(new ImageIcon("casino.png")); casinol.setBounds(0, 0, 1980, 1050); card1.add(casinol); this.b1 = new JButton("START");//ホーム画面→BuyIn this.b1.setBounds(840, 680, 300, 100); this.b1.setBackground(Color.white); this.rb1 = new JButton("RULE");//ホーム画面でルール確認 this.rb1.setBounds(840, 800, 300, 100); this.rb1.setBackground(Color.white); card1.add(this.rb1); // BuyinView(card2) JPanel card2 = new JPanel(); card2.setBackground(Color.BLACK); card2.setLayout(null); this.b2 = new JButton("Back");//BuyIn→ホーム this.b3 = new JButton("Let's Play!");//BuyIn→ゲーム this.b2.setBounds(840, 900, 300, 100); this.b3.setBounds(840, 780, 300, 100); this.combo.setBounds(840, 550, 300, 100); card2.add(combo); //GameView(card3) JPanel card3 = new JPanel(); card3.setLayout(null); gamel.setIcon(new ImageIcon("Game.png")); gamel.setBounds(0, 0, 1980, 1050); card3.add(gamel); this.b4 = new JButton("H");//ゲーム画面→ホーム this.b4.setBounds(12, 18, 50, 50); this.b4.setBackground(Color.white); this.b5 = new JButton("$");//ゲーム画面→BuyIn this.b5.setBounds(11, 63, 50, 50); this.b5.setBackground(Color.white); this.rb2 = new JButton("R");//ゲーム画面でルール確認 this.rb2.setBounds(11, 113, 50, 50); this.rb2.setBackground(Color.white); card3.add(this.rb2); /* CardLayout準備 */ this.panel = new JPanel(); this.layout = new CardLayout();//CardLayoutの作成 this.panel.setLayout(this.layout); /* panelにViewを追加 */ this.panel.add(card1, "HomeView"); this.panel.add(card2, "BuyinView"); this.panel.add(card3, "GameView"); /* カード移動用ボタン */ this.b1.addActionListener(this); this.b1.setActionCommand("BuyinView"); this.b2.addActionListener(this); this.b2.setActionCommand("HomeView"); this.b3.addActionListener(this); this.b3.setActionCommand("GameView"); this.b4.addActionListener(this); this.b4.setActionCommand("HomeView"); this.b5.addActionListener(this); this.b5.setActionCommand("BuyinView"); card1.add(this.b1); card2.add(this.b2); card2.add(this.b3); card3.add(this.b4); card3.add(this.b5); getContentPane().add(this.panel, BorderLayout.CENTER); combo.addItemListener(this); this.rb1.addActionListener(this); this.rb2.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == this.rb1 || e.getSource() == this.rb2) {//ルール確認ボタンがどちらか押されたとき System.out.println("a"); new Rule(); } String cmd = e.getActionCommand(); layout.show(this.panel, cmd); } @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == 1) { String str = ((String) combo.getSelectedItem()).replace("$", ""); this.stack = Integer.parseInt(str); } } } import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Rule extends JFrame implements ActionListener { JPanel panel; CardLayout layout; private JLabel r1l = new JLabel(""); private JLabel r2l = new JLabel(""); private JLabel r3l = new JLabel(""); private JLabel r4l = new JLabel(""); public Rule() { this.setSize(300, 700); //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(355, 100, 300, 580); this.setTitle("BJ's Rule"); JPanel rcard1 = new JPanel(); rcard1.setBackground(new Color(215, 245, 215)); rcard1.setLayout(null); this.r1l.setIcon(new ImageIcon("rule1.png")); this.r1l.setBounds(0, 0, 300, 500); rcard1.add(this.r1l); JPanel rcard2 = new JPanel(); rcard2.setBackground(new Color(215, 245, 215)); rcard2.setLayout(null); this.r2l.setIcon(new ImageIcon("rule2.png")); this.r2l.setBounds(0, 0, 300, 500); rcard2.add(this.r2l); JPanel rcard3 = new JPanel(); rcard3.setBackground(new Color(215, 245, 215)); rcard3.setLayout(null); this.r3l.setIcon(new ImageIcon("rule3.png")); this.r3l.setBounds(0, 0, 300, 500); rcard3.add(this.r3l); JPanel rcard4 = new JPanel(); rcard4.setBackground(new Color(215, 245, 215)); rcard4.setLayout(null); this.r4l.setIcon(new ImageIcon("rule4.png")); this.r4l.setBounds(0, 0, 300, 500); rcard4.add(this.r4l); /* CardLayout準備 */ this.panel = new JPanel(); this.layout = new CardLayout();//CardLayoutの作成 this.panel.setLayout(this.layout); //panelにViewを追加 this.panel.add(rcard1, "r1View"); this.panel.add(rcard2, "r2View"); this.panel.add(rcard3, "r3View"); this.panel.add(rcard4, "r4View"); /* カード移動用ボタン */ JButton rb1 = new JButton("1"); rb1.addActionListener(this); rb1.setActionCommand("r1View"); JButton rb2 = new JButton("2"); rb2.addActionListener(this); rb2.setActionCommand("r2View"); JButton rb3 = new JButton("3"); rb3.addActionListener(this); rb3.setActionCommand("r3View"); JButton rb4 = new JButton("4"); rb4.addActionListener(this); rb4.setActionCommand("r4View"); JPanel btnPanel = new JPanel(); btnPanel.add(rb1); btnPanel.add(rb2); btnPanel.add(rb3); btnPanel.add(rb4); getContentPane().add(this.panel, BorderLayout.CENTER); getContentPane().add(btnPanel, BorderLayout.PAGE_END); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); layout.show(this.panel, cmd); } }
まだ回答がついていません
会員登録して回答してみよう