• 締切済み

ボタンが表示されません

//テキストパッドをつくっています。 //このプログラムはボタンが表示されませんなぜ? 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
  • 回答数2
  • ありがとう数2

みんなの回答

noname#94983
noname#94983
回答No.2

ボタンが表示されません、の前に、とりあえず「ちゃんとコンパイルできるソース」を書いてください。動かないソースを掲載されても、何をしたいのかわからない。 とりあえずは、まあ、こんな感じのことをやりたかったんですかね。(JDK 5.0以降) import java.awt.*; import javax.swing.*; class Test_Text extends JFrame { public static void main(String args[]) { new Test_Text(); } public Test_Text(){ Sample_text st = new Sample_text(); this.add(st,BorderLayout.NORTH); JTextArea jt = new JTextArea(50, 30); this.add(new JScrollPane(jt), BorderLayout.CENTER); this.pack(); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } class Sample_text extends Panel { public Sample_text() { JButton j1, j2; j1 = new JButton("編集"); j2 = new JButton("保存"); setLayout(new FlowLayout()); this.add(j1); this.add(j2); } } }

yuuitirou
質問者

お礼

コンパイルできるコードを書いていますが? ↑参考にさせていただきます。

  • jyugemu55
  • ベストアンサー率68% (11/16)
回答No.1

setLayout(new FlowLayout()); ではなく getContentPane().setLayout(new FlowLayout()); ですね。 同じように this.add(j1); ではなく this.getContentPane().add(j1); です。 JFrameのAPIドキュメントを読み直してください。

yuuitirou
質問者

お礼

APIドキュメント勉強します。

関連するQ&A

  • 参照されない

    クラスSentenceActionperformedのtext1,textの部分がエラーとなります。クラスは呼ばれています。二つとも参照?されていないのです。 参考になるページをみると同じような書き方しているのに問題ないのでどこが違うのかわかりません。この書き方では参照でなきないのだと思いますが正しい書き方がわかりません。別のクラスにしていく方法でいきたいです。 うまく行けばtextの文字がtext1に表示されます。 よろしくお願いします。 import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextArea; import javax.swing.JTextField; public class sample extends javax.swing.JFrame{ public sample() { init(); } private void init() { JTextField text = new JTextField(25); JTextArea text1 = new JTextArea(10,25); Container mainFrame = getContentPane(); mainFrame.setLayout(new FlowLayout()); mainFrame.add(text); mainFrame.add(text1); mainFrame.setBounds(0, 0, 500, 500); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); SentenceActionPerformed aa = new SentenceActionPerformed(); text.addActionListener(aa); } class SentenceActionPerformed implements ActionListener{ public void actionPerformed(ActionEvent evt) { text1.setText(text.getText()); } } public static void main(String args[]) { new sample().setVisible(true); } }

    • ベストアンサー
    • Java
  • FlowLayoutについて

    こんにちは。 FlowLayoutをについて、どうしても理解できないので教えて頂けないでしょうか。 import java.awt.*; import javax.swing.*; public class FlowLayoutEn2 extends JFrame{   public FlowLayoutEn2(){   super("Flow_Layout");   setDefaultCloseOperation(EXIT_ON_CLOSE);   getContentPane().setLayout(new FlowLayout());      JButton first = new JButton("first");   JButton second= new JButton("second");   JButton third = new JButton("third");   getContentPane().add(first);   getContentPane().add(second);   getContentPane().add(third);   pack();   }   public static void main(String args[]){   new FlowLayoutEn2().setVisible(true);   } }  上記でコンパイルすると、「FlowLayoutEn2はpublicであり、ファイルFlowLayoutEn2.javaで宣言しなければなりません。・・・FlowLayoutにアクセスできません・・」と表示されます。  しかし、「import java.awt.*」の部分を、「import java.awt.FlowLayout」に変更すると正常にコンパイルが終了し求めていた結果が表示されます。同じパッケージ内であるの事と、参考書類を見てもFlowLayoutを「import java.awt.*」のみで使用している本もありましたので使用可との認識だったのですが、どうしても理解できません。 基本的な事かもしれませんが、どうぞご教授下さい。

    • ベストアンサー
    • Java
  • java(swing)で、登録ボタンを押すとファイルを読み込みDBに登

    java(swing)で、登録ボタンを押すとファイルを読み込みDBに登録する処理です。 その処理で、登録ボタンを押すと、ダイアログを起動して、処理中とメッセージ表示したいのですが、データが多く、画面が固まった状態になり、メッセージが表示されないのです。何かいい方法ありますでしょうか? ******************************************************************* import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class topMain { public static void main(String[] args) { //メニュー画面を起動する topMain frame = new topMain(); frame.topFrame(); } private void topFrame() { JFrame jf = new JFrame(); //フレームのサイズ設定 jf.setSize(1000, 600); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); jf.getContentPane().setLayout(null); JButton b1 = new JButton("登録ボタン"); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //ダイアログボックスを開く     //ダイアログボックスは起動するが、 //メッセージが表示されない、 //おそらく大きいデータを読むとき画面が固まるため //重いファイルを読み込む //DB登録処理 //ダイアログを閉じる } }); jf.getContentPane().add(b1); b1.setBounds(40, 400, 240, 100); jf.setVisible(true); } } *******************************************************************

  • 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
  • Jframeの中にJframeを表示させたい

    Jframeの中にボタンを作り、ボタンを押すとJInternalFrameではなくJframeを表示させるようにしたいんですが、なかなかできません。 検索したりしてサンプル探したんですが、 Jframeを表示させ、ラベルを貼ったりしたいんですが どなたかお願いします!! public class Browser extends JFrame{ public static void main(String args[]) { Browser frame = new Browser(); frame.setTitle(""); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setVisible(true); } //ボタン private JButton bt = new JButton("~~"); public Browser() { bt.addActionListener(this); JToolBar tb = new JToolBar(); tb.setFloatable(false); getContentPane().add(tb, BorderLayout.NORTH); JPanel panel2 = new JPanel(); panel2.add(bt); panel2.setLayout(new FlowLayout(FlowLayout.LEFT)); tb.add(panel2); } //↓この辺りの処理の記述で頭が混乱してきました(泣 public class actionPerformed(ActionEvent e){ if(e.getSource() == bt){ Frame FW = new Frame(); FW.setSize(500,400); FW.setVisible(true); } } class Frame extends Frame{ public Frame(){ //コンストラクタの定義 } }

    • ベストアンサー
    • Java
  • ボタンが表示されず困ってます・・・。

    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枚が全て表示されるようにしたいのですが、ボタンが表示されないがため確認出来ておりません。 色々とご不便をお掛けしますが例などを挙げてもらえるととても助かります。宜しくお願い致します。  

  • テキストフィールドの文字をテキストエリアに表示する

    エラーも例外もないです。 但し今回の内容の"テキストフィールドに打ち込みエンターキーを文字をテキストフィールドに表示" が満たしてないですとりあえずソース乗せます mport java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class MyMemo { public static void main(String[] args) { JFrame Frame = new JFrame("課題"); Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Frame.setSize(500, 500); Frame.setLocationRelativeTo(null); Container contentPane = Frame.getContentPane(); // テキストフィールドのインスタンスを生成 final JTextField text = new JTextField();// テキストエリアのインスタンスを生成 text.addActionListener(new MyActionListener()); Frame.setVisible(true); final JTextArea Area = new JTextArea(5,0); // スクロールペインにテキストエリアを追加 JScrollPane scrollPane = new JScrollPane(Area); // パネルをコンポーネントに配置 contentPane.add(text, BorderLayout.NORTH); contentPane.add(scrollPane, BorderLayout.CENTER); Frame.setVisible(true); } static class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { } } } どう文字をテキストエリアに表示したらいいのかわからなくて質問させていただきました。 よろしくお願いします。

    • ベストアンサー
    • Java
  • JEditorPaneで表示したHTMLを、初期化/変更するには

    プログラム中で生成したHTMLを随時、JEditorPaneに表示しようとしています。また、表示したHTMLをすべて消す(初期化する)こともあります。 以下のようなコードを書いたのですが、うまくいきません。 1.画面が初期化されませんし、初期化した後は追加もされなくなります。 2.追加するとHTMLタグが解釈されずに、そのまま表示されます。 どうすればよいでしょうか? import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; //4openURL import java.awt.Desktop; import java.net.URI; import javax.swing.text.*; import javax.swing.text.html.HTMLDocument; class tstFrame extends JFrame { private final String strHead = "<HTML>" + "<HEAD>" + "<META http-equiv=\"Content-Type\" content=\"text/html\">" + "</HEAD>"; private String strBgColor = "#CCCCCC"; // private String strHTML = "<html><body bgcolor=\"#CCCCFF\">google<br><br><a href=\"http://www.google.co.jp/\">click here!!</a>"; // //ログ表示部 javax.swing.JEditorPane jEdtLogHtml; JButton jbtnA = new JButton ("追加"); JButton jbtnC = new JButton ("変更"); public tstFrame(){ getContentPane().setLayout(new FlowLayout()); //実際にログを表示する窓 jEdtLogHtml = new javax.swing.JEditorPane("text/html" , strHTML); this.getContentPane().add(new JScrollPane(jEdtLogHtml)); jbtnA.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ java.util.Date d = new java.util.Date(); addText("<font color=\"#FFFFFF\">"+d.toString()+"</font>" ); } }); jbtnC.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ initALL(); } }); this.getContentPane().add(jbtnA); this.getContentPane().add(jbtnC); this.setSize(400 , 400); this.setVisible(true); // jEdtLogHtml.setEditable(false); } // 画面を初期化する public void initALL(){ //??? jEdtLogHtml = new javax.swing.JEditorPane("text/html" , strHTML); } // 文字列をログに追加する public void addText(String argStrlog){ jEdtLogHtml.setContentType("text/html"); HTMLDocument doc = (HTMLDocument)jEdtLogHtml.getDocument(); SimpleAttributeSet sas = new SimpleAttributeSet(); try{ doc.insertString(doc.getLength(), argStrlog+"\n", sas); }catch(BadLocationException e) { e.printStackTrace(); } jEdtLogHtml.setContentType("text/html"); this.setVisible(true); } public static void main(String[] args) { new tstFrame().setVisible(true); } }

  • 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);    } /*以下省略*/

  • キャレットの幅を設定したい。

    キャレットの幅を設定したい。 以下のソースで色を設定するメソッドはありますが、 キャレットの幅の設定の仕方が解りませんでした。 ご存じの方がおられましたら教えてください。 import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; class test extends JFrame{ public static void main(String args[]){ new test(); } test(){ this.setSize(300, 250); JPanel p = new JPanel(); JTextArea area2 = new JTextArea("test", 10, 10); area2.setCaretColor(Color.RED); p.add(area2); this.add(p, BorderLayout.CENTER); this.setVisible(true); } }

    • ベストアンサー
    • Java

専門家に質問してみよう