• 締切済み

java起動時にボタンが表示されない

Javaの初心者です。以下のプログラムを起動するとJTextAreaとJLabel は、表示されるのですが、JButtonが表示されません。起動後にボタンの配置したところを触るとボタンが表示されるようになるのですが、ボタンのアクションリスナーが動作してしまいます。起動時にボタンも表示できるようにするには、どうしたらよいでしょうか? public class test_pro extends JFrame{ private static final long serialVersionUID = 1L; private JLabel label1;     JButton buton1,button2;     public JTextArea lt1; public test_pro() { this.setUndecorated(true);    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); device.setFullScreenWindow(this); Container contentPane = getContentPane(); contentPane.setLayout(null); label1 = new JLabel("menu"); Font f1=new Font("Serif",1,22); label1.setFont(f1); label1.setForeground(Color.red);     contentPane.add(label1); lt1=new JTextArea(3,3); JScrollPane pane=new JScrollPane(lt1); pane.setBounds(new Rectangle(50,300,550,150)); contentPane.add(pane);     button1 = new JButton("SendRequestTest!"); button2 = new JButton("ReceiveResponseTest!"); label1.setBounds(new Rectangle(50,20,200,20)); button1.setBounds(new Rectangle(50,80,230,20)); button2.setBounds(new Rectangle(50,110,230,20));  contentPane.add(button1); contentPane.add(button2); button1.addActionListener(new MyJsendActionAdapter()); button2.addActionListener(new MyJreceiveActionAdapter());     this.setDefaultCloseOperation(EXIT_ON_CLOSE);     this.setVisible(true);    } /*以下省略*/

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

みんなの回答

  • yamada_g
  • ベストアンサー率68% (258/374)
回答No.1

当方の環境で実行したところ、最初から表示されましたよ。 MyJsendActionAdapterクラスの実装が不明なので、 各ボタンのActionListenerにはnullをセットするところだけ変えて起動しました。

関連するQ&A

  • javaのボタン表示等に関する質問

    下記のコードはフォームをNORTH、CENTER、SOUTHEに分割して、それぞれのpaneに ラベルとボタンを表示するものです。 Q1)myFrame.setBounds(350,0, total_x, total_y)をコンストラクターの最初に記述しますと  フォームの表示が正常でなく、コンストラクターの最後では、正常に行なわれます。  この理由等について、コメント頂けますと有り難いです。 Q2)mainに記述してある、下記のコードは無くても、Xでクロースできますが,このコード  の記述は正しいでしょうか GridLayout_new frame = new GridLayout_new(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 以上お手数ですが、コメント頂けますと大変助かります。 //============================================= import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class GridLayout_new extends JFrame{ int panelA_x=200, panelA_y=50; int panelB_x=200, panelB_y=200; int panelC_x=200, panelC_y=50; int total_x=panelA_x; int total_y=panelA_y+panelB_y+panelC_y; public static void main(String[] args){ GridLayout_new frame = new GridLayout_new(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } GridLayout_new(){ JFrame myFrame=new JFrame(); myFrame.setTitle("panelA"); // myFrame.setBounds(350,0, total_x, total_y); //正常なformの表示が出来ない myFrame.setVisible(true); LineBorder border = new LineBorder(Color.BLACK, 1, true); //======================== JLabel button1=new JLabel("Label1"); GLabel_Layout(button1, border); JButton button2 = new JButton("button2"); button_Layout(button2, border); //======================== JLabel button3=new JLabel("Label3"); GLabel_Layout(button3, border); JButton button4 = new JButton("button4"); button_Layout(button4, border); //======================== JLabel button5=new JLabel("Label5"); GLabel_Layout(button5, border); JButton button6 = new JButton("button6"); //======================== JLabel button7=new JLabel("Label7"); GLabel_Layout(button7, border); JButton button8 = new JButton("button8"); //======================== JLabel button9=new JLabel("Label9"); GLabel_Layout(button9, border); JButton button10 = new JButton("button10"); button_Layout(button10, border); //======================== JLabel button11=new JLabel("Label11"); GLabel_Layout(button11, border); JButton button12 = new JButton("button12"); button_Layout(button12, border); //================================== JPanel p1 = new JPanel(); GridLayout layout1 = new GridLayout(2,2); //2行、2列の設定 layout1.setHgap(2); layout1.setVgap(10); p1.setLayout(layout1); p1.setBackground(Color.GREEN); p1.add(button1); p1.add(button2); p1.add(button3); p1.add(button4); myFrame.setSize(panelA_x, panelA_y); myFrame.getContentPane().add(p1, BorderLayout.NORTH); //============================== JPanel p2 = new JPanel(); GridLayout layout2 = new GridLayout(2,2); //2行、2列の設定 layout2.setHgap(2); layout2.setVgap(10); p2.setLayout(layout2); p2.setBackground(Color.GREEN); p2.add(button5); p2.add(button6); p2.add(button7); p2.add(button8); myFrame.setSize(panelB_x, panelB_y); myFrame.getContentPane().add(p2, BorderLayout.CENTER); //================================== JPanel p3 = new JPanel(); GridLayout layout3 = new GridLayout(2,2); //2行、2列の設定 layout3.setHgap(2); layout3.setVgap(10); p3.setLayout(layout3); p3.setBackground(Color.GREEN); p3.add(button9); p3.add(button10); p3.add(button11); p3.add(button12); myFrame.setSize(panelC_x, panelC_y); myFrame.getContentPane().add(p3, BorderLayout.SOUTH); myFrame.setBounds(350,0, total_x, total_y); //正常なformの表示 } //constructor void GLabel_Layout(JLabel label, LineBorder border){ label.setPreferredSize(new Dimension(80,20)); label.setBorder(border); label.setBackground(Color.lightGray); label.setOpaque(true); } void button_Layout(JButton button, LineBorder border){ button.setPreferredSize(new Dimension(80,20)); button.setBorder(border); } } //main class....GridLayout_new

    • ベストアンサー
    • Java
  • java修正

    以下のDentaku.javaのソースコードには引き算と割り算の部分が抜けてしまっているのですが引き算と割り算の部分を付け加えてくれる方お願いします。。(うまく手を加えることができませんでした) // Dentaku.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Dentaku extends JFrame implements ActionListener { JTextField text1, text2; JButton button1, button2, button3; JLabel label; // コンストラクタ public Dentaku() { // コンテントペインを取得 Container contentPane = getContentPane(); // レイアウトマネージャーを設定 contentPane.setLayout(new GridLayout(6,1)); // テキストフィールド生成 text1 = new JTextField("", 20); text2 = new JTextField("", 20); // テキストフィースドを追加 contentPane.add(text1); contentPane.add(text2); // ボタンを作成 button1 = new JButton("+"); button2 = new JButton("x"); button3 = new JButton("Clear"); // アクションリスナーを登録 button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); // ボタンを追加 contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); // ラベルを生成 label = new JLabel(""); // 文字を真ん中に表示 label.setHorizontalAlignment(JLabel.CENTER); // ラベルを追加 contentPane.add(label); // ウインドウが閉じられたときにアプリケーションを終了するように設定 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // サブコンポーネントの推奨サイズおよびレイアウトに合わせて // この Window をサイズ変更するように設定 pack(); // 表示します setVisible(true); } public static void main(String[] args) { // フレームを作成 Dentaku frame = new Dentaku(); } /** * アクションが発生すると呼び出されます。 */ public void actionPerformed(ActionEvent ae){ // イベントが指定されたボタンで発生した場合 //足し算 if (ae.getSource().equals(button1)) { try{ String tx1=text1.getText(); String tx2=text2.getText(); double d1=Double.parseDouble(tx1); double d2=Double.parseDouble(tx2); String answer=Double.toString(d1+d2); label.setText(answer); }catch (NumberFormatException e) { label.setText("数字を正しく入力してください。"); } } //掛け算 if (ae.getSource().equals(button2)) { try{ String tx1=text1.getText(); String tx2=text2.getText(); double d1=Double.parseDouble(tx1); double d2=Double.parseDouble(tx2); String answer=Double.toString(d1*d2); label.setText(answer); }catch (NumberFormatException e) { label.setText("数字を正しく入力してください。"); } } if (ae.getSource().equals(button3)) { text1.setText(""); text2.setText(""); label.setText(""); } } }

  • java アプレット

    学校の授業でアプレットを学んでいます。 しかし・・・ 下のプログラムで、計算した結果をlabel3にはりつけたいのですが、計算結果がでてくれません・・・ボタンを押しても何もでてきません。 コマンドプロンプトで、エラーはでてきません。 お手数ですが、誰かどこがいけないのか教えて下さい。 import java.awt.*; import java.awt.event.*; public class B extends java.applet.Applet implements ActionListener { Label label3 = new Label(); Button button2 = new Button(); TextField textfield1= new TextField(10); TextField textfield2= new TextField(10); public void init() { button2.setLabel("入力したらクリック!"); button2.setBounds(new Rectangle(50, 465, 120, 20)); this.setLayout(null); label3.setBackground(Color.blue); label3.setBounds(new Rectangle(325, 80, 200, 35)); textfield1.setBounds(100, 425, 100, 25); textfield2.setBounds(150, 350, 100, 25); this.add(textfield2, null); this.add(label3, null); this.add(textfield1, null); this.add(button2, null);} public void actionPerformed(ActionEvent event) {if(event.getSource() == button2) {int diff = Integer.parseInt(textfield1.getText()) - Integer.parseInt(textfield2.getText()); String otsuri; otsuri ="お釣りは"+ diff +"円です。"; label3.setText(otsuri);} }} よろしくお願いします。

    • ベストアンサー
    • 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); } } ソースはこのようになっています。これからどうすれば背景画像が表示されるか教えていただきたいです。 よろしくお願いします。

  • JAVAプログラミング

    プログラミングについて質問があります 割り勘ソフトを作りたいのですが button1を押したときに int i = Integer.parseInt(kazu.getText()); int e = Integer.parseInt(kane.getText()); int a = e/i; String s = String.valueOf(a); this.an.setText(s); というプログラムを実行したいのですが button1のクリック処理?イベント処理?というのはどのように書けばいいのでしょうか レイアウトは完成しているので ↓に付け足すような形でお願いします プログラミングの質問をするのは初めてなので何かおかしなところや足りないところがあれば教えてください public class jFram { public static void main(String[] args) { JFrame mainFrame = new JFrame("1"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setSize(500,500); mainFrame.setResizable(false); mainFrame.setVisible(true); Container cont = mainFrame.getContentPane(); JButton button1 = new JButton("計算"); final JTextField kane = new JTextField(); final JTextField kazu = new JTextField(); JLabel mony = new JLabel("料金"); JLabel ninz = new JLabel("人数"); JLabel an = new JLabel("a"); button1.setLayout(null); cont.add(button1); button1.setBounds(40,380,100,30); button1.setBackground(Color.blue); kane.setLayout(null); cont.add(kane); kane.setBounds(160,340,100,30); kazu.setLayout(null); cont.add(kazu); kazu.setBounds(160,300,100,30); mony.setLayout(null); cont.add(mony); mony.setBounds(40,340,100,30); ninz.setLayout(null); cont.add(ninz); ninz.setBounds(40,300,100,30); an.setLayout(null); cont.add(an); an.setBounds(160,380,100,30); cont.setLayout(null); JPanel a = new JPanel(); a.setLayout(null); mainFrame.setVisible(true); } }

    • ベストアンサー
    • Java
  • JButtonの配置

    FlowLayoutによって、JFrameにボタン6個を2×3に配列するプログラムを考えています。ボタンを配置することはできたのですが、ウインドウの大きさを変えるとボタンが3×2になったりと変更してしまいます。 ウインドウの大きさを変えても、ボタンの大きさは変えず、ボタン6個が2×3配列になるプログラムはどうしたらよいのでしょうか?以下は、途中のプログラムです。 import java.awt.*; import javax.swing.*; public class sample { public static void main(String[] args){ MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } public class MyFrame extends JFrame { public static final int WIDTH = 200; public static final int HEIGHT = 300; public MyFrame(){ setSize(WIDTH,HEIGHT); Container contentPane = getContentPane(); MyPanel panel = new MyPanel(); contentPane.add(panel); } } public class MyPanel extends JPanel{ public static final int MESSAGE_X = 200; public static final int MESSAGE_Y = 200; setLayout(new FlowLayout()); JButton button1 = new JButton("Button1"); contentPane.add(button1); JButton button2 = new JButton("Button2"); contentPane.add(button2); JButton button3 = new JButton("Button3"); contentPane.add(button3); JButton button4 = new JButton("Button4"); contentPane.add(button4); JButton button5 = new JButton("Button5"); contentPane.add(button5); JButton button6 = new JButton("Button6"); contentPane.add(button6); }

    • ベストアンサー
    • Java
  • GUIを使用した電卓のボタンの表示について

    Javaの課題で電卓を制作しています。 まず見た目だけ完成させるべくボタンを配置を配置しましたが、 実行してみると正しく表示されるときと、されないときがあります(画像参照)。 現時点でのコードを掲載いたしますので、是非アドバイスをいただけると幸いです。よろしくお願いします。 ■以下コード package add; import java.awt.Button; import java.awt.Frame; import java.awt.TextField; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Calc extends Frame{ public static void main(String[] args){ Calc frame = new Calc(); } public Calc(){ super("電卓"); setSize(317,360); setVisible(true); addWindowListener(new CalcWindow()); //下記のウィンドウを閉じる用 //ボタンとフィールドの追加 TextField result = new TextField(""); result.setBounds(20,50,270,50); add(result); result.setFocusable(false); Button button1 = new Button("1"); button1.setBounds(20,120,70,30); add(button1); Button button2 = new Button("2"); button2.setBounds(100,120,70,30); add(button2); Button button3 = new Button("3"); button3.setBounds(180,120,70,30); add(button3); Button button4 = new Button("4"); button4.setBounds(20,165,70,30); add(button4); Button button5 = new Button("5"); button5.setBounds(100,165,70,30); add(button5); Button button6 = new Button("6"); button6.setBounds(180,165,70,30); add(button6); Button button7 = new Button("7"); button7.setBounds(20,210,70,30); add(button7); Button button8 = new Button("8"); button8.setBounds(100,210,70,30); add(button8); Button button9 = new Button("9"); button9.setBounds(180,210,70,30); add(button9); Button button0 = new Button("0"); button0.setBounds(100,255,70,30); add(button0); Button buttonC = new Button("C"); buttonC.setBounds(20,255,70,30); add(buttonC); Button buttonasta = new Button("."); buttonasta.setBounds(180,255,70,30); add(buttonasta); Button buttonplus = new Button("+"); buttonplus.setBounds(260,120,30,30); add(buttonplus); Button buttonminus = new Button("-"); buttonminus.setBounds(260,165,30,30); add(buttonminus); Button buttonkakeru = new Button("×"); buttonkakeru.setBounds(260,210,30,30); add(buttonkakeru); Button buttonsla = new Button("/"); buttonsla.setBounds(260,255,30,30); add(buttonsla); Button buttonE = new Button("="); buttonE.setBounds(20,300,270,30); add(buttonE); //ボタンとフィールドの追加ここまで } class CalcWindow extends WindowAdapter //ウィンドウを閉じる { public void windowClosing(WindowEvent e) { System.exit(0); } } } ※OKWAVEより補足:「Webシステム開発」についての質問です。

    • ベストアンサー
    • 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.*; 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); となっています。「ゲームを始める」ボタンだけを残して、周りのパネルを透過して画像を表示させたいのですが、うまくいきません!解決方法をご存知の方どうか教えていただきたいです!よろしくお願いします。

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

    //テキストパッドをつくっています。 //このプログラムはボタンが表示されませんなぜ? 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); } }

  • javaで起こったバグ?!

    私が普段プログラムを書いてコンパイルすると、ほぼ確実といってもいいくらいにおかしなことが起きます。 ボタンを配置して、処理を書きます(ここではJTableが現れるという処理にします)、そしてコンパイルします、コンパイルは通ります、ウィンドウが現れます、ボタンを押したら処理が起きてJTableが現れるはずなのですが、ボタンを押すとコマンドプロンプトでわけわからん英語や数字が現れます(出力される)。 自分の書いたプログラムをeclipseでコピペしても、問題はありませんでした。 コマンドプロンプトを一度消して、直ったプログラムもあります。また、その逆に何度やっても直らないものもあります。 ↓私の書いたプログラムです。おかしいところ等がありましたらご指摘下さい。また解答者様のPCでコピペして、試してみて下さい。 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Sukejuru extends JFrame implements ActionListener { JPanel p; public static void main(String args[]) { Sukejuru ske = new Sukejuru(); ske.setBounds(200,200,600,300); //大きさ&ウィンドウの位置 ske.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//クローズ ske.setVisible(true); } Sukejuru() { super("Myスケジュール"); JPanel p = new JPanel(); p.setBackground(Color.green); add(p); JButton b1 = new JButton("一月"); b1.addActionListener(this); b1.setActionCommand("b1"); p.add(b1); JButton b2 = new JButton("二月"); b2.addActionListener(this); b2.setActionCommand("b2"); p.add(b2); JButton b3 = new JButton("三月"); b3.addActionListener(this); b3.setActionCommand("b3"); p.add(b3); } public void actionPerformed(ActionEvent e) { JTable table = new JTable(5,5); JScrollPane sp = new JScrollPane(table); p.add(sp); } }

    • ベストアンサー
    • Java