###前提・実現したいこと
閲覧ありがとうございます。
初めて質問をさせていただきます。
マナー違反等無いよう心がけます。至らぬ点あればご指摘ください。
現在、JavaのSwingを使用し、簡単なゲームを作成しようと考えています。
Webを参考にフレーム内のパネルを切替え画面遷移を行えるようになりました。
次に、先ほど画面遷移に使用したパネルに、画像を格納したラベルを追加することで
画像の表示を実現しようとしました。
しかし、画像が表示されていません。
回答どうかよろしくお願いします。
-追記-
masaya_ohashiさんの指摘により、パスがあっていることは確認できました。
現在はbackGroundLabelに格納できているか確認中です。
アドバイスお待ちしてますm(__)m
###発生している問題
・画像が表示されていない
・ImageIconへの画像格納ができていない(?)
![
↑パスは確認済みです。
画像貼り付け部分のみ
//画像の貼り付け backGroundIcon = new ImageIcon(); try { backGroundIcon.setImage(ImageIO.read(new File("./title02.jpg"))); //backGroundIcon.setImage(ImageIO.read(new File("./title.png"))); } catch (IOException e1) { e1.printStackTrace(); } //ImageIcon backGroundIcon = new ImageIcon("./title.png"); backGroundLabel = new JLabel(); backGroundLabel.setIcon(backGroundIcon); this.add(backGroundLabel);
以下全体の処理
フレームの作成、パネル切り替えを行う
public class MainFrame extends JFrame { //画面遷移用に画面名を配列に保存 public String[] PanelNames = { "titlePanel", "modeSelectPanel", "stageSelectPanel" }; TitlePanel titlePanel = new TitlePanel(this, PanelNames[0]); ModeSelectPanel modeSelectPanel = new ModeSelectPanel(this, PanelNames[1]); StageSelectPanel stageSelectPanel = new StageSelectPanel(this,PanelNames[2]); //フレームの作成 public MainFrame() { this.add(titlePanel); titlePanel.setVisible(true); this.add(modeSelectPanel); modeSelectPanel.setVisible(false); this.add(stageSelectPanel); stageSelectPanel.setVisible(false); this.setBounds(400, 200, 800, 600); } //ウィンドウを作成し、指定したパネルの処理を行える(メインメソッド) public static void main(String[] args) { MainFrame mainFrame = new MainFrame(); mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); mainFrame.setVisible(true); } //パネルの切替による画面遷移を行う /** * * @param jp 作成されたパネルを格納 * @param str パネル名 */ public void PanelChange(JPanel jp, String str) { System.out.println(jp.getName()); String name = jp.getName(); if (name == PanelNames[0]) { titlePanel = (TitlePanel) jp; titlePanel.setVisible(false); } else if (name == PanelNames[1]) { modeSelectPanel = (ModeSelectPanel) jp; modeSelectPanel.setVisible(false); } else if (name == PanelNames[2]) { stageSelectPanel = (StageSelectPanel) jp; stageSelectPanel.setVisible(false); } if (str == PanelNames[0]) { titlePanel.setVisible(true); } else if (str == PanelNames[1]) { modeSelectPanel.setVisible(true); } else if (str == PanelNames[2]) { stageSelectPanel.setVisible(true); } } }
タイトル画面のパネル
public class TitlePanel extends JPanel { JButton btn, btn2; JLabel paneltitle; MainFrame mainFrame; String str; ImageIcon backGroundIcon; JLabel backGroundLabel; TitlePanel(MainFrame m, String s) { mainFrame = m; str = s; this.setName("titlePanel"); this.setLayout(null); this.setSize(800, 600); paneltitle = new JLabel(getClass().getCanonicalName() + "クラスパネル"); paneltitle.setBounds(0, 5, 400, 40); this.add(paneltitle); //モードセレクトへの移動ボタン作成 btn = new JButton("ModeSelectPanelに移動"); btn.setBounds(20, 50, 300, 80); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panelChange(mainFrame.PanelNames[1]); } }); this.add(btn); //ステージセレクトへの移動ボタン作成 btn2 = new JButton("SubPanel2に移動"); btn2.setBounds(20, 300, 300, 80); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panelChange(mainFrame.PanelNames[2]); } }); this.add(btn2); //背景色の設定、背景画像に置き換え //this.setBackground(Color.getHSBColor(65, 0.7f, 0.9f)); //画像の貼り付け backGroundIcon = new ImageIcon(); try { backGroundIcon.setImage(ImageIO.read(new File("./title02.jpg"))); //backGroundIcon.setImage(ImageIO.read(new File("./title.png"))); } catch (IOException e1) { e1.printStackTrace(); } //ImageIcon backGroundIcon = new ImageIcon("./title.png"); backGroundLabel = new JLabel(); backGroundLabel.setIcon(backGroundIcon); this.add(backGroundLabel); } /** *遷移したいパネル名を受け取り、フレーム内の画面遷移メソッドを呼び出す * @param str パネル名 */ public void panelChange(String str) { mainFrame.PanelChange((JPanel) this, str); } }
回答2件
あなたの回答
tips
プレビュー