• 締切済み

ボタンが表示されず困ってます・・・。

javaで「カップル」というトランプゲームを作っているのですが、背景色を設定する所までは行きましたが、表示されるはずのボタン(スタート、ランキング、ログアウト)が表示されず困っています・・・。 import java.awt.Color; import java.awt.Dimension; import java.awt.*; import javax.swing.*; import java.io.*; import javax.imageio.*; import java.awt.Graphics; import java.awt.Font; import javax.swing.JButton; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.event.*; public class narabi2 extends JPanel{ JButton btn1,btn2,btn3; static Image img[] = new Image[52]; public static void main(String[] args) { JFrame f = new JFrame(); f.setSize(900,840); f.getContentPane().setBackground(new Color(0,100,0)); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setResizable(true); f.setVisible(true); new narabi2(); } public narabi2(){ btn1 = new JButton("スタート"); this.add(btn1); btn1.addActionListener(new ev()); btn2 = new JButton("ランキング"); this.add(btn2); btn2.addActionListener(new ev()); btn3 = new JButton("ログアウト"); this.add(btn3); btn3.addActionListener(new ev()); JFrame f = new JFrame(); f.setSize(900,840); f.getContentPane().setBackground(new Color(0,100,0)); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setResizable(true); f.setVisible(true); } public void paintComponent(Graphics g){ super.paintComponent(g); int i; int x = 0,y=0; for (i = 0; i < 52; i++) { g.drawImage(img[i], x, y, null); x = x + 100; if(x>=900){ x=0; y=y+140; } } } class ev implements ActionListener{ public void actionPerformed(ActionEvent m){ if(m.getSource()==btn1) //スタート { // 画像ファイルの読み込み int i, j; int no = 0; String c[] = {"s","h","c","d"}; try { for(i = 0; i < 4; i++) { for(j = 1; j <= 13; j++) { //String filename = "cards.png"; String filename = String.format("cards/%s%02d.png", c[i], j); img[no] = ImageIO.read(new File(filename)); no++; } } } catch(Exception e) { System.out.println(e); System.exit(0); } // シャッフル Image w; for (i = 51; i >= 0; i--) { j = (int)(Math.random() * (i + 1)); w = img[i]; img[i] = img[j]; img[j] = w; } } /*else if(m.getSource()==btn2) //ランキング { } else if(m.getSource()==btn3) //ログアウト { } } }*/ btn2,3はやらなくていい所です。更にはbtn1(スタート)が押されたときに、トランプ52枚が全て表示されるようにしたいのですが、ボタンが表示されないがため確認出来ておりません。 色々とご不便をお掛けしますが例などを挙げてもらえるととても助かります。宜しくお願い致します。  

  • Java
  • 回答数1
  • ありがとう数1

みんなの回答

  • teketon
  • ベストアンサー率65% (141/215)
回答No.1

略 : f.setSize(900,840); f.getContentPane().setBackground(new Color(0,100,0)); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setResizable(true); f.setVisible(true); f.add(this);//パネルコンポーネントをフレームへ追加する } : 略

jyojisan
質問者

補足

teketonさん、回答ありがとうございます。 ご指摘頂いたように修正したところ、ボタンが表示されました。 早速、スタートボタンをクリックしてトランプ52枚表示されるか試してみたのですが、 1枚も表示されず、苦労してます。原因があるとすればどのあたりでしょうか? 何度も質問してしまって申し訳ございませんが、何卒よろしくお願い致します。 以下は、修正したプログラムです。 import java.awt.Color; import java.awt.Dimension; import java.awt.*; import javax.swing.*; import java.io.*; import javax.imageio.*; import java.awt.Graphics; import java.awt.Font; import javax.swing.JButton; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.event.*; public class narabi2 extends JPanel{ JButton btn1,btn2,btn3; static Image img[] = new Image[52]; public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setResizable(true); f.setVisible(true); new narabi2(); } public narabi2(){ btn1 = new JButton("スタート"); this.add(btn1); btn1.addActionListener(new ev()); btn2 = new JButton("ランキング"); this.add(btn2); btn2.addActionListener(new ev()); btn3 = new JButton("ログアウト"); this.add(btn3); btn3.addActionListener(new ev()); JFrame f = new JFrame(); f.setSize(900,840); f.getContentPane().setBackground(new Color(0,100,0)); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setResizable(true); f.setVisible(true); f.add(this); } public void paintComponent(Graphics g){ super.paintComponent(g); btn1 = new JButton("スタート"); this.add(btn1); btn1.addActionListener(new ev()); btn2 = new JButton("ランキング"); this.add(btn2); btn2.addActionListener(new ev()); btn3 = new JButton("ログアウト"); this.add(btn3); btn3.addActionListener(new ev()); setBackground(new Color(0,100,0)); int i;               //カードを並べる処理 int x = 0,y=0; for (i = 0; i < 52; i++) { g.drawImage(img[i], x, y, null); x = x + 100; if(x>=900){ x=0; y=y+140; } } } class ev implements ActionListener{ public void actionPerformed(ActionEvent m){ if(m.getSource()==btn1) //スタート { //public static void main(String[] args) { // 画像ファイルの読み込み int i, j; int no = 0; String c[] = {"s","h","c","d"}; try { for(i = 0; i < 4; i++) { for(j = 1; j <= 13; j++) { //String filename = "cards.png"; String filename = String.format("cards/%s%02d.png", c[i], j); img[no] = ImageIO.read(new File(filename)); no++; } } } catch(Exception e) { System.out.println(e); System.exit(0); } // シャッフル Image w; for (i = 51; i >= 0; i--) { j = (int)(Math.random() * (i + 1)); w = img[i]; img[i] = img[j]; img[j] = w; } } /*public void paintComponent(Graphics g) { super.paintComponent(g); int i; int x = 0,y=0; for (i = 0; i < 52; i++) { g.drawImage(img[i], x, y, null); x = x + 100; if(x>=900){ x=0; y=y+140; } } }*/ } /*else if(m.getSource()==btn2) //ランキング { } else if(m.getSource()==btn3) //ログアウト { } }*/ } }

関連するQ&A

  • イベント処理がうまくいきません。

    java初心者です。 スタートボタンをクリックするとトランプ52枚表示されるようにしたいのですが、 1枚も表示されず、原因もよく分からないため苦労してます。 よろしくお願い致します。 以下は、プログラムです。 import java.awt.Color; import java.awt.Dimension; import java.awt.*; import javax.swing.*; import java.io.*; import javax.imageio.*; import java.awt.Graphics; import java.awt.Font; import javax.swing.JButton; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.event.*; public class narabi2 extends JPanel{ JButton btn1,btn2,btn3; static Image img[] = new Image[52]; public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setResizable(true); f.setVisible(true); new narabi2(); } public narabi2(){ btn1 = new JButton("スタート"); this.add(btn1); btn1.addActionListener(new ev()); btn2 = new JButton("ランキング"); this.add(btn2); btn2.addActionListener(new ev()); btn3 = new JButton("ログアウト"); this.add(btn3); btn3.addActionListener(new ev()); JFrame f = new JFrame(); f.setSize(900,840); f.getContentPane().setBackground(new Color(0,100,0)); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); f.setResizable(true); f.setVisible(true); f.add(this); } public void paintComponent(Graphics g){ super.paintComponent(g); btn1 = new JButton("スタート"); this.add(btn1); btn1.addActionListener(new ev()); btn2 = new JButton("ランキング"); this.add(btn2); btn2.addActionListener(new ev()); btn3 = new JButton("ログアウト"); this.add(btn3); btn3.addActionListener(new ev()); setBackground(new Color(0,100,0)); int i;               //カードを並べる処理 int x = 0,y=0; for (i = 0; i < 52; i++) { g.drawImage(img[i], x, y, null); x = x + 100; if(x>=900){ x=0; y=y+140; } } } class ev implements ActionListener{ public void actionPerformed(ActionEvent m){ if(m.getSource()==btn1) //スタート { //public static void main(String[] args) { // 画像ファイルの読み込み int i, j; int no = 0; String c[] = {"s","h","c","d"}; try { for(i = 0; i < 4; i++) { for(j = 1; j <= 13; j++) { //String filename = "cards.png"; String filename = String.format("cards/%s%02d.png", c[i], j); img[no] = ImageIO.read(new File(filename)); no++; } } } catch(Exception e) { System.out.println(e); System.exit(0); } // シャッフル Image w; for (i = 51; i >= 0; i--) { j = (int)(Math.random() * (i + 1)); w = img[i]; img[i] = img[j]; img[j] = w; } } /*public void paintComponent(Graphics g) { super.paintComponent(g); int i; int x = 0,y=0; for (i = 0; i < 52; i++) { g.drawImage(img[i], x, y, null); x = x + 100; if(x>=900){ x=0; y=y+140; } } }*/ } /*else if(m.getSource()==btn2) //ランキング { } else if(m.getSource()==btn3) //ログアウト { } }*/ } }

  • ボタンが表示されません

    //テキストパッドをつくっています。 //このプログラムはボタンが表示されませんなぜ? import java.awt.*; import javax.swing.*; class Test_Text extends JFrame{ public static void main(String args[]){ Sample_text st = new Sample_text(); JFrame jf = new JFrame("Test_Text"); JTextArea jt = new JTextArea(50,30); Container c = jf.getContentPane(); c.add(jt,BorderLayout.PAGE_START); jf.pack(); jf.setVisible(true); jf.setDefaultCloseOperation(EXIT_ON_CLOSE); } public void Sample_text(){ JButton j1,j2; j1 = new JButton("編集"); j2 = new JButton("保存"); setLayout(new FlowLayout()); this.add(j1); this.add(j2); } }

  • イメージボタンを作成したいのですが。。

    イメージアイコンを利用してイメージつきのボタンを作成したいのですが、 ボタン上にイメージが描画されません。 ソースは以下のとおりです。 import javax.swing.*; import java.awt.event.*; class Dtsess_trmer extends JFrame implements ActionListener { JButton img_btn; ImageIcon imgic; public static void main( String args[] ) { String com = args[0]; String img = args[1]; String tit = args[2]; String mes = args[3]; Dtsess_trmer dt = new Dtsess_trmer(com,img,tit,mes); } Dtsess_trmer(String com,String img,String tit,String mes) { super(tit); makeicon(img); setting(); img_btn = new JButton(imgic); getContentPane().add(img_btn); } public void setting() { setSize(200,200); setVisible(true); } public void makeicon(String IMG) { imgic = new ImageIcon(IMG); } public void actionPerformed(ActionEvent AE) { } } コンパイルはとおります。どこが間違っているのでしょうか?

  • javaでスタート画面を作っていて困っています。

    現在javaでゲームのスタート画面を作っているのですが、パネルが透過されずに困っています。 プログラムソースは import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.*; import java.awt.Font; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; class Start最新版 extends JFrame{ public static void main(String args[]){ Start最新版 frame = new Start最新版("タイトル"); frame.setVisible(true); } Start最新版(String title){ setTitle(title); setBounds(10, 10, 1024, 768); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); p.setOpaque( false ); ImageIcon icon1 = new ImageIcon("Start.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); p.add(label1); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); JButton button1 = new JButton("ゲームを始める"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel n = new JPanel(); n.setOpaque(false); n.setLayout(new BoxLayout(n, BoxLayout.Y_AXIS)); n.add(Box.createRigidArea(new Dimension(290,30))); n.add(button1); となっています。「ゲームを始める」ボタンだけを残して、周りのパネルを透過して画像を表示させたいのですが、うまくいきません!解決方法をご存知の方どうか教えていただきたいです!よろしくお願いします。

  • javaのBoxlayoutについて

    下記はBoxLayout用ののコードです。 Q1)Buttonのサイズを設定する方法がありますか? Q2)Buttonの配置を設定する方法がありますか? //===================================== import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.LayoutManager; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class AlignmentX extends JFrame{ JButton button1; JButton button2; JButton button3; public static void main(String[] args){ AlignmentX frame = new AlignmentX(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("タイトル"); frame.setVisible(true); } AlignmentX(){ button1 = new JButton("AAAAA"); button1.setAlignmentY(0.5f); button1.setPreferredSize(new Dimension(80,20)); button2 = new JButton("BBB"); button2.setAlignmentY(0.5f); button2.setPreferredSize(new Dimension(80,20)); button3 = new JButton("CC"); button3.setAlignmentY(0.5f); button3.setPreferredSize(new Dimension(80,20)); JPanel p = new JPanel(); p.setLayout((LayoutManager) new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(button1); p.add(button2); p.add(button3); getContentPane().add(p, BorderLayout.CENTER); setBounds(10, 10, 300, 200); } } //以上,宜しくお願いします。

    • ベストアンサー
    • Java
  • なんかエラーがでた

    とあるプログラムをeclipseで作ったら問題なく動きました。しかし学校のPCでやると実行時にエラーがでました。それが以下です。 class javax.swing.JFrameにはExit_ON_CLOSEは定義されていません ちなみにソースは以下です import javax.swing.*; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.*; import java.util.Vector; import java.awt.event.* ; public class f_sotu3 extends JFrame { JPanel p; JButton btn1; JTextField tx1; public f_sotu3(){ p = new JPanel(); p.setLayout(null); btn1 = new JButton("検索"); tx1 = new JTextField(); setTitle("卒論"); setBounds( 10, 10, 1100, 1100); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event){ System.out.println(tx1.getText()); } } ); btn1.setBounds(500,50,100,60); tx1.setBounds(50,170,900,60); tx1.setFont(new Font("MS ゴシック",Font.BOLD,18)); p.add(tx1); p.add(btn1); getContentPane().add(p); } public static void main(String[] args){ /* 自分自身を作成 */ f_sotu3 test = new f_sotu3(); /* 終了処理を追加 */ test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); /* 実際に表示する */ } }

    • ベストアンサー
    • Java
  • JFrame の再表示を禁止したい。

    親ウィンドウのボタンを押して、子ウインドウが表示される仕組みなのですが、1 つ表示した後は、 表示させないようにしたいと考えコードを組みましたが、以下コードでは、null 例外が出てしまいます。 良い方法はありませんでしょうか? みなさん、どうぞよろしくお願い致します。 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class FramesTest { public static void main(String[] args) { JFrame frameMain = new JFrame("Main"); frameMain.setSize(256, 128); frameMain.getContentPane().add( new BT() ); frameMain.setVisible(true); } } class BT extends JPanel implements ActionListener { /** * */ private static final long serialVersionUID = 1L; /*-------------------------* * Variables. * *-------------------------*/ static String cmdName; static JButton button; static JFrame subJFrame; /*-------------------------* * Methods. * *-------------------------*/ BT() { super( new BorderLayout() ); button = new JButton("button"); button.addActionListener(this); add( button ); setPreferredSize(new Dimension(320, 100)); } void createFrame() { subJFrame = new JFrame( "sub" ); subJFrame.setVisible(true); subJFrame.setSize(256, 128); } @Override public void actionPerformed(ActionEvent e) { cmdName = e.getActionCommand(); if ("button".equals(cmdName)) { if ( !subJFrame.isActive() ) { createFrame(); } } } }

    • ベストアンサー
    • Java
  • JAVAでの背景画像表示

    現在javaでゲームのメニュー画面を作っているのですが、ボタンを配置する所まではできたんですが、背景に画像を表示することができなくて困っています! import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; public class Mati extends JFrame{ public static void main(String[] args){ Mati frame = new Mati(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10,650,650); frame.setTitle("街"); frame.setVisible(true); JPanel h = new JPanel(); h.setOpaque(false); ImageIcon icon1 = new ImageIcon("Mati.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); h.add(label1); } Mati(){ JButton button1 = new JButton("宿屋"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button2 = new JButton("道具屋"); button2.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button3 = new JButton("武器屋"); button3.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button4 = new JButton("街を出る"); button4.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(Box.createRigidArea(new Dimension(10,30))); p.add(button1); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button2); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button3); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button4); getContentPane().add(p, BorderLayout.CENTER); } } ソースはこのようになっています。これからどうすれば背景画像が表示されるか教えていただきたいです。 よろしくお願いします。

  • jFrameを使って複数の画面を生成するにはどうすれば良いのでしょうか?

    eclipseのVE機能を使ってjFrameの画面生成を勉強しているのですが、 複数の画面を生成することができません。。 以下のソースで、ボタンが押された時に新しく画面を生成するように しているのですが、なぜか生成できません。エラーもありません。 何が足りなかったのか、悪かったのかを指摘していただけますでしょうか? import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.GridBagLayout; import javax.swing.JButton; import java.awt.GridBagConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class jFrameTest extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JButton jButton = null; /** * This is the default constructor */ public jFrameTest() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(300, 200); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; jContentPane = new JPanel(); jContentPane.setLayout(new GridBagLayout()); jContentPane.add(getJButton(), gridBagConstraints); } return jContentPane; } /** * This method initializes jButton * * @return javax.swing.JButton */ private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setText("jButton"); jButton.addActionListener(this); } return jButton; } //ボタン入力リスナー public void actionPerformed(ActionEvent event) { String cmd = event.getActionCommand(); if(cmd == "jButton"){ jFrameTest jtest = new jFrameTest(); System.out.println("new"); } } }

    • ベストアンサー
    • Java
  • javaについて質問

    import javax.swing.*; import java.awt.Color; import java.awt.Graphics; import java.awt.event.*; public class tt extends JFrame { public tt(){ setTitle("hogehoge"); setBounds( 10, 10, 300, 200); JButton btn1 = new JButton("続ける"); final JPanel p = new JPanel(); p.setBackground(Color.white); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event){ } } ); p.add(btn1); getContentPane().add(p); } public static void main(String[] args){ tt test = new tt(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); /* 実際に表示する */ } } Graphics g; g.drawString("ほげさんじょう",130,120);を追加しても コンパイルが通りません。 どうすれば画面に文字列を表示するんですか? あとテキストフィールドを追加するさいに、座標していとかして追加できないんですか?

専門家に質問してみよう