Javaコードでのウィンドウ表示と非表示についてのエラー

このQ&Aのポイント
  • eclipse環境でのJavaコードでウィンドウの表示と非表示を行う際、f.show();の使用方法が推奨されずエラーが発生します。
  • エラーが発生した後でも無理に動作させると正しく動作しません。
  • どのようにすればf.show();を有効にできるか、解決方法を教えてください。
回答を見る
  • ベストアンサー

下のソースを参考書をそのまま書いたのですが

下のソースを参考書をそのまま書いたのですが eclipse環境だと f.show(); のところが推薦されない使われ方 として実行できません 無理やり動作させたところ、エラーが返ってきました どうすれば、f.show(); が有効にできるでしょうか? import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class window { public static void main( String[] args ) { AppFrame f = new AppFrame(); f.setSize( 200, 200 ); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e) { System.exit(0); } } ); f.show(); } } class AppFrame extends Frame implements ActionListener { Button b1, b2; labelWindow window; AppFrame() { setLayout( new FlowLayout() ); b1 = new Button( "Display the window" ); add(b1); b1.addActionListener( this ); b2 = new Button( "Hide the window" ); add(b2); b2.addActionListener( this ); window = new labelWindow(this); window.setSize( 300,200 ); window.setLocation( 300, 300 ); } public void actionPerformed( ActionEvent event ) { if( event.getSource() == b1 ) { window.setVisible(true); } if( event.getSource() == b2 ) { window.setVisible(false); } } } class labelWindow extends Window { Label label; labelWindow( AppFrame af ) { super( af ); setLayout( new FlowLayout() ); label = new Label( "Hello from Java!" ); add( label ); } public void paint( Graphics g ) { int width = getSize().width; int heigth = getSize().heigth; g.drawRect( 0, 0, --width, --heigth ); } }

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

質問者が選んだベストアンサー

  • ベストアンサー
  • askaaska
  • ベストアンサー率35% (1455/4149)
回答No.1

これはJDK1.0時代のソースかしら。 現在は showメソッドは非推奨メソッドになっているわ。 setVisibleメソッドを使ってあげて。

dic555
質問者

お礼

解決しました ありがとうございます

関連するQ&A

  • 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
  • 整数を好きなだけ入力し、個数、合計、平均値を表示するプログラム

    appletviewerに、ボックスの中に整数を好きなだけ入力し、個数、合計、平均値を以下のように表示させるようなプログラムを作りたいです。(1,2,3を入力した場合)     データ□ (入力)        (計算)   個数=3   合計=6   平均値=2 ※□はボックスです。(入力)(計算)はクリックするボタンです。 どなたかご指導やアドバイスをお願いいたします。まだまだ初心者でとても困っています。。 ちなみに途中まで考えたのが以下のプログラムです。 import java.applet.*; import java.awt.Button; import java.awt.Label; import java.awt.TextField; import java.awt.event.*; public class Lec1106 extends Applet implements ActionListener { private TextField box1 = new TextField(5); private Label moji1 = new Label("データ"); private Label moji2 = new Label("個数="); private Label moji3 = new Label("合計="); private Label moji4 = new Label("平均値="); private Button nyuuryoku = new Button("入力"); private Button keisan = new Button("計算"); public void init() { resize(320,240); add(moji1); add(box1); add(nyuuryoku); nyuuryoku.addActionListener(this); add(keisan); keisan.addActionListener(this); add(moji2); add(moji3); add(moji4); } public void actionPerformed(ActionEvent e)  {int i; String t1=box1.getText(); i=Integer.parseInt(t1);

    • ベストアンサー
    • Java
  • jcpad

    『長方形をあらかじめ置き、、縮小、拡大のボタンを押すことで  縦横を1/1.05倍に縮小できるようにする』 という問題が教科書にあり、拡大、縮小ボタンを作るまではできたのですがそれ以降がうまく考えることができません。 //<applet code= "Counter3.class" width=300 height=150></applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class Counter3 extends Applet implements ActionListener { Font font; Button kakudaiButton, syukushoButton; public void init() { font = new Font("Serif", Font.BOLD, 72); kakudaiButton = new Button("拡大"); kakudaiButton.addActionListener(this); add(kakudaiButton); syukushoButton = new Button("縮小"); syukushoButton.addActionListener(this); add(syukushoButton); } public void paint(Graphics g) { g.drawOval(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == kakudaiButton) { ; } else if (e.getSource() == syukushoButton) { ; } repaint(); } }

  • Label を リスナーと結びつける方法

    フレーム上に (ボタンやリストやチェックボックスではなくて) ラベルを用いて、ラベルを押すことにより 背景が変わるプログラムを作りたいのですが どのようにイベント処理すれば、うまくいくのでしょうか。 プログラムは以下のように作りましたがイベントリスナを どのように加えればいいのかがわかりません>< よろしくお願いしますm(_ _)m import java.awt.*; import java.awt.event.*; public class Test{ public static void main(String args[]){ MyFrame f=new MyFrame("Test"); f.setVisible(true); } } class MyFrame extends Frame{ Label red, green, blue; MyFrame(String s){ super(s); setSize(200,200); setLocation(100,150); setLayout(new FlowLayout(FlowLayout.CENTER)); add(red=new Label("■")); red.setForeground(Color.red); add(green=new Label("■")); green.setForeground(Color.green); add(blue=new Label("■")); blue.setForeground(Color.blue); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } }

    • ベストアンサー
    • 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
  • add( )メソッドについて

    import java.applet.Applet; import java.awt.*; /* <applet code="Sample" width=400 height=250> </applet> */ public class Sample extends Applet { public void init() { // ラベルを作成する。 Label lb = new Label("こんにちは。", Label.LEFT); add(lb); } } 上記のプログラムのadd(lb)について質問です。上記のadd()メソッドは、lbはLabelクラスなので、LabelクラスかComponentクラスのパラメーターを一つ持つvoid型のadd()メソッドとなると思うのですが、調べてもそのようなメソッドが存在しませんどうしてでしょうか?上述のことが間違っているのでしょうか?教えてください。

    • ベストアンサー
    • Java
  • return;

    retrun;を取ると動作がおかしくなるんですが return;にはどういう働きがあるんでしょうか? お願いします。 import java.applet.Applet; import java.awt.*; import java.awt.event.*; //<applet code = "a.class" width = "300" height = "300"></applet> public class a extends Applet implements ActionListener{ Dialog d; Frame f; public void init() { setLayout( new GridLayout( 1, 1 ) ); Button b = ( Button )add( new Button( "frame" ) ); b.addActionListener( this ); } public void actionPerformed( ActionEvent e ){ if( e.getActionCommand() == "frame" ){ if ( f == null ){ f = new Frame( "Kitty on your lap" ); Button fb = (Button)f.add( new Button( "Kitty " ) ); fb.addActionListener( this ); f.setSize( 200 , 200 ); f.setVisible( true ); }else if( d == null ){ f.dispose(); f = null; } return; } if( e.getActionCommand() == "OK" ) { d.dispose(); d = null; return; } d = new Dialog( f, "Kitty", true ); d.setLayout( new GridLayout( 2, 1 ) ); d.setResizable( false ); d.add( new Label( "Kitty on your lap" ) ); Button b = (Button)d.add( new Button( "OK" ) ); b.addActionListener( this ); d.setSize( 400 , 200 ); d.setVisible( true ); } }

    • ベストアンサー
    • Java
  • カレンダーを作っているのですが

    import java.awt.*; import java.awt.event.*; public class GraphicsC4 extends Frame { public static void main(String ar[]){ Frame f=new GraphicsC4(); f.setTitle ("平成19年6月 (GridLayout)"); f.setSize(640,400); f.setVisible(true); } GraphicsC4(){ setFont(new Font("SansSerif",Font.BOLD+Font.ITALIC,30)); GridLayout gl=new GridLayout(6,7); setLayout(gl); String day[]={"SUN","MON","TUE","WED","THU","FRI","SAT"}; for(int j=0;j<=6;j++){ Button b1=new Button(day[j]); add(b1); } for(int e=1;e<=5;e++){ Button b2=new Button(""); add(b2); } for(int i=1;i<=30;i++){ Button b3=new Button(""+i+""); add(b3); } addWindowListener(new WinAdapter()); } class WinAdapter extends WindowAdapter{ public void windowClosing(WindowEvent we){System.exit(0);} } } ここまで書いたのですが、日曜日を赤く表示することが出来ません。どなたか教えてください。

  • シンボルを解決できません

    サンプルプログラムをjavacでコンパイルすると 以下のようなエラーが出ます。 ----------------------------- C:\MyJava>javac menudemo.java menudemo.java:57: シンボルを解決できません。 シンボル: コンストラクタ Menu (java.lang.String) 場所 : Menu の クラス menu1 = new Menu("ファイル"); ^ menudemo.java:70: シンボルを解決できません。 シンボル: メソッド add (java.awt.MenuItem) 場所 : Menu の クラス menu1.add(menuitem1); ^ menudemo.java:71: シンボルを解決できません。 シンボル: メソッド add (java.awt.MenuItem) 場所 : Menu の クラス menu1.add(menuitem2); ^ menudemo.java:72: シンボルを解決できません。 シンボル: メソッド add (java.awt.MenuItem) 場所 : Menu の クラス menu1.add(menuitem3); ^ menudemo.java:75: java.awt.MenuBar の add(java.awt.Menu) は (Menu) に適用できません。 menubar1.add(menu1); ^ エラー 5 個 --------------------------------------------- サンプルプログラムは以下の通りです。 --------------------------------------------- import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class menudemo extends Applet implements ActionListener { Button button1, button2; MenuFrame menuWindow; public void init() { button1 = new Button("ウィンドウの表示"); add(button1); button1.addActionListener(this); button2 = new Button("ウィンドウの非表示"); add(button2); button2.addActionListener(this); menuWindow = new MenuFrame("デモメニュー"); menuWindow.setSize(100, 100); } public void actionPerformed(ActionEvent event) { if (event.getSource() == button1) { menuWindow.setVisible(true); } if (event.getSource() == button2) { menuWindow.setVisible(false); } } } class MenuFrame extends Frame implements ActionListener { MenuBar menubar1; Menu menu1; MenuItem menuitem1, menuitem2, menuitem3; TextField text1; MenuFrame(String title) { super(title); setLayout(new GridLayout(1, 1)); text1 = new TextField(""); add(text1); //メニューバーの生成 menubar1 = new MenuBar(); //メニューの生成 menu1 = new Menu("ファイル"); //メニュー項目の生成 menuitem1 = new MenuItem("Javaの"); menuitem2 = new MenuItem("世界へ"); menuitem3 = new MenuItem("ようこそ"); //イベントリスナーの登録 menuitem1.addActionListener(this); menuitem2.addActionListener(this); menuitem3.addActionListener(this); //メニューへの追加 menu1.add(menuitem1); menu1.add(menuitem2); menu1.add(menuitem3); //メニューバーへの追加 menubar1.add(menu1); //メニューバーを追加 setMenuBar(menubar1); } public void actionPerformed(ActionEvent event) { if (event.getSource() == menuitem1) { text1.setText("Javaの"); } if (event.getSource() == menuitem2) { text1.setText("世界へ"); } if (event.getSource() == menuitem3) { text1.setText("ようこそ"); } } } --------------------------------------------- テキストどおりに入力しているのですが、 いろいろ調べてみても どこがおかしいか自分では分かりませんでした。 どのようにすれば解決するかお教えください。 よろしくお願いいたします。

    • ベストアンサー
    • 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(""); } } }

専門家に質問してみよう