ボタンとダイアログの関連性

このQ&Aのポイント
  • ボタンを押すとダイアログを表示させる方法について
  • ダイアログ内でのボタンの操作方法について
  • ダイアログの定義とフレーム内のボタンとの関係について
回答を見る
  • ベストアンサー

ボタンとダイアログの関連性

import java.awt.*; import java.awt.event.*; public class ActionListenerTest extends Frame implements ActionListener { ActionListenerTest() { super("ActionListenerTest"); Button b1 = new Button("BUTTON"); b1.addActionListener(this); add(b1); setSize(200, 100); setVisible(true); } public void actionPerformed(ActionEvent evt) { String ac = evt.getActionCommand(); if( ac == "BUTTON") { /*ここでの操作でダイアログを表示させたい*/ } } public static void main(String [] args) { new ActionListenerTest(); } } 表示されたフレーム内のボタンを押したときに、ダイアログを表示させたいです。コメントアウトした部分でダイアログを表示させたいのですが、ダイアログの定義はActionListenerTest() 内にて行うのでしょうか? またダイアログ内でボタンを作り、そのボタンの操作を行うときは、フレームでのボタンと同様にString ac = evt.getActionCommand(); ---でいいのでしょうか? どなたかご教授願います。 汚いプログラムすいません。

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

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

  • ベストアンサー
  • nknk80
  • ベストアンサー率65% (17/26)
回答No.1

> ダイアログの定義はActionListenerTest() 内にて行うのでしょうか? プログラムが読みにくくなるため、一般的にはここではダイアログの定義はしません。 ActionListenerTestクラス定義の外側で、ダイアログの定義するのが一般的だと思います。 > またダイアログ内でボタンを作り、そのボタンの操作を行うときは、 > フレームでのボタンと同様にString ac = evt.getActionCommand(); > ---でいいのでしょうか? そのような書き方でも大丈夫ですが、下記のTestDialogクラスとして 定義したような書き方もできます。 この方法では、イベントのソース(イベント発生源)が定義した ボタンと同じであることを見ています。 /* --- 以下プログラム --- */ import java.awt.*; import java.awt.event.*; public class ActionListenerTest extends Frame implements ActionListener { ActionListenerTest() { super("ActionListenerTest"); Button b1=new Button("BUTTON"); b1.addActionListener(this); add(b1); setSize(200,100); setVisible(true); } public void actionPerformed(ActionEvent evt) { String ac=evt.getActionCommand(); if(ac=="BUTTON") { /* ここでの操作でダイアログを表示させたい */ TestDialog dialog=new TestDialog(this); dialog.setVisible(true); } } public static void main(String[] args) { new ActionListenerTest(); } } /* --- 以下、テスト用ダイアログ --- */ class TestDialog extends Dialog implements ActionListener { private Button b2; TestDialog(Frame frame) { super(frame,true); b2=new Button("button"); b2.addActionListener(this); add(b2); setLocation(50,50); setSize(200,200); setTitle("Test Dialog"); } public void actionPerformed(ActionEvent evt) { if(evt.getSource()==b2) { /* b2ボタンを押したときの処理 */ setVisible(false); dispose(); } } }

maru1021
質問者

お礼

正しく実行できました。 ありがとうございました

関連するQ&A

  • 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); } } *******************************************************************

  • 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
  • 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
  • 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
  • アプレットでボタンを押したときの処理について質問です。

    JAVAで2つのクラスを作成しました。 1つは初期画面で「入力」「検索」等と言ったボタンを持ち、ボタンを押されると別の画面に切り替えるクラスです。もう1つは初期画面で「入力ボタン」を押されて表示する画面のクラスです。 初期画面でボタンが押されたときの処理を次のようにしました。 public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("入力")){ Input input = new Input(); } ですが、Inputクラスの画面が表示されません。 Inputクラスのソースは import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="Input" width=250 height=250> </applet> */ public class Input extends Applet { Panel panel; TextArea textarea; public void init()//部品を初期化する { panel = new Panel(); textarea = new TextArea(); setLayout(new BorderLayout()); add(textarea,BorderLayout.CENTER); add(panel,BorderLayout.SOUTH); } } です。ボタンを押された時のイベントを受け取るまでの処理は正しいと思うのですが、どのように改良すればよろしいでしょうか?

    • ベストアンサー
    • Java
  • ダイアログボックスの出し方

    Javaを勉強中の初心者なのですが、 ダイアログボックスの出し方を教えて頂きたいのです。 Java入門にサンプルがあったので、その通り入力し実行したところPCが止まっちゃいました。なぜでしょうか? それと下のサンプルの中に書いてある ユーザークラスとは何でしょうか? よろしくお願いいたします。 ーーーサンプルソースーーー import java.awt.*; public class myDialog extends Dialog{ public myDialog(Frame parent){ super(parent,"My Dialog",true); } public void init(String s){ setSize(300,150); setVisible(true); } } 作成したユーザークラスを呼び出すには次のようにします。 myDialog d; d=new myDialog(new Frame()); d.init("ウェイトの合計が100になっていません!!");

    • ベストアンサー
    • Java
  • ボタンクリック後パネルを再描画repaintしたい

    ボタンをクリック後にstaticで保持していた値にプラス1して その値をパネルに再描画したいと思っています。 下記のプログラムだとボタンをクリックすると (<1<2<3<4)となって画像と数字が増えてしまいます 行いたいのは数字の部分だけが再描画されて カウントされて<3と表示されるだけになってもらいたい クラス1つ目 import java.awt.BorderLayout; import java.awt.Cursor; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Sample extends JFrame{ static int value=1; JPanel work = new JPanel(); public static void main(String[] args) { Sample frame = new Sample(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 200, 200, 100); frame.add(frame.createPanel(), BorderLayout.LINE_END); frame.setVisible(true); } public JPanel createPanel() { ImageIcon icon = new ImageIcon("./img/left.gif"); JLabel label = new JLabel(); Cursor c = new Cursor(Cursor.HAND_CURSOR); label.setCursor(c); label.setIcon(icon); JLabel strLabel = new JLabel(); String str = Integer.toString(value); strLabel.setText(str); work.add(label); work.add(strLabel); label.addMouseListener(new myListener()); return work; } class myListener extends MouseAdapter{ public void mouseClicked(MouseEvent e){ System.out.print(Sample.value++); createPanel().repaint(); setVisible(true); } } } クラス2つ目 import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class MouseAdapter implements MouseListener{ public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} }

    • ベストアンサー
    • Java
  • javaのmainの中のループに割込を掛ける

    毎度、お世話になります。 javaのmainの中のループに割込を掛ける方法をお教えください。 添付コードの『Thread.currentThread().interrup()』は、旨く機能しません。 以上、宜しくお願いします。 =========== import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class T_try_loop extends JFrame{ JFrame frame=new JFrame(); T_try_loop(){ System.out.println("aaaa"); JPanel p1=new JPanel(); JButton button1=new JButton("button1"); button1.addActionListener(new TimButton1()); p1.add(button1); getContentPane().add(p1, BorderLayout.CENTER); } public static void main(String args[]){ T_try_loop frame=new T_try_loop(); frame.setTitle("TTTT"); frame.setBounds(10,10,400,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); for(int j = 0;j < 80; j++){ try{ Thread.sleep(80); System.out.print("*"); } catch(InterruptedException e){ System.out.println(": main_loopに、今割り込まれました"); } } }//public static void main public class TimButton1 implements ActionListener{ @Override public void actionPerformed(ActionEvent ae){ String cmd =ae.getActionCommand(); if(cmd.equals("button1")){ Thread.currentThread().interrupt(); System.out.println("Button clicked"); } } }//public class TimButton1 }

    • ベストアンサー
    • 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);} } } ここまで書いたのですが、日曜日を赤く表示することが出来ません。どなたか教えてください。

  • J#でjava.awt.frameからSystem.Windows.Forms.Formを呼び出す

    Windows XP SP2にVisual J# 2005 Express Editionをインストールして使用しています。 今まで、J2SE 5.0を使用していて、GUIの部分はSwingで書いていたのですが、J#に移行したいと思っています。 J#はSwingを一部サポートしているのですが、JTextPaneが使えないので、その部分だけ、J#のSystem.Windows.Forms.FormとSystem.Windows.Forms.RichTextBoxを使って作り替えようと思っています。そこで、java.awt.Frameにjava.awt.Buttonを配置してそのボタンが押されたときにFormを表示させたいのですが、Form.Show()すると表示されたFormがきちんと表示されず、フリーズしてしまいます。 以下に、ソースコードを載せますので、問題点を指摘していただけたら幸いです。 import System.Windows.Forms.Form; import System.Windows.Forms.RichTextBox; import System.Drawing.*; import System.ComponentModel.*; import java.awt.*; import java.awt.event.*; class test implements ActionListener{  Frame frame;  Form f;  RichTextBox tb;  Button b;  IContainer components;  void testGUI(){   this.b=new Button("test");   this.b.addActionListener(this);   this.tb=new RichTextBox();   this.f=new Form();   this.f.get_Controls().Add(this.tb);   this.f.set_Name("Form1");   this.f.set_Text("Form1");   this.frame=new Frame();   this.frame.setSize(100,100);   this.frame.add(this.b);   this.frame.show();  }  public static void main(String[] args){   test2 t2=new test2();   t2.testGUI();  }  private void f_Load(Object sender, System.EventArgs e){}  public void actionPerformed(ActionEvent ae){   if(ae.getSource()==this.b){    this.f.Show();   }  } }

専門家に質問してみよう