• 締切済み

[java初心者です]ボタンのイベントの追加

javaで、複数ボタンを表示し、クリックするとそれぞれのダイアログが表示されるプログラムを作っています。 サンプルを利用して、ボタンを追加するところまで出来たのですが そこからがうまくできません。 アクションリスナーがthisにしてあるため、どのボタンでも 同じダイアログが表示されてしまうのだと思いますが、 this以外にすることはできるのでしょうか? import javax.swing.*; import java.awt.BorderLayout; import java.awt.event.*; public class JOptionPaneTest8 extends JFrame implements ActionListener{ JLabel ansLabel; public static void main(String[] args){ JOptionPaneTest8 frame = new JOptionPaneTest8(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 300, 200); frame.setTitle("タイトル"); frame.setVisible(true); } JOptionPaneTest8(){ JButton ichiButton = new JButton("○○○な時"); ichiButton.addActionListener(this); JButton niButton = new JButton("○○●な時"); niButton.addActionListener(this); JPanel p = new JPanel(); p.add(ichiButton); p.add(niButton); ansLabel = new JLabel("今の気分をクリック!"); JPanel ansPanel = new JPanel(); ansPanel.add(ansLabel); getContentPane().add(p, BorderLayout.CENTER); getContentPane().add(ansPanel, BorderLayout.PAGE_END); } public void actionPerformed(ActionEvent e){ ImageIcon icon = new ImageIcon("aka.gif"); int option = JOptionPane.showConfirmDialog(this, "赤を身の回りに置いて元気に!", "OK?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, icon); if (option == JOptionPane.YES_OPTION){ ansLabel.setText("ほかには?"); }else if (option == JOptionPane.NO_OPTION){ ansLabel.setText("今の気分をクリック!"); } } 気分をボタンとして表示して そのボタンをクリックすると効果的な色のアイコン(gif)と解決策?を ダイアログとして表示させたいです。 高校でついこの間javaを始めた初心者です(>_<) 提出期限が近づいていて焦っています; お手数ですが回答お願いいたします・・・!!

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

みんなの回答

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

>ichiButton.addActionListener(this); >niButton.addActionListener(this); ここで同じActionListenerを実装したクラスのインスタンスを指定しているから、同じ動作をしています。 簡単な解決方法は、ActionListenerを実装したクラスをもう一つ定義し、そのインスタンスをaddActionListenerします。 public class NiButtonActionListener implements ActionListener{  public void actionPerformed(ActionEvent e){   //niButton用の処理  } } で、元のプログラムの下記を >niButton.addActionListener(this); niButton.addActionListener(new NiButtonActionListener()); に修正します。これでボタンごとの処理に分けられます。

関連するQ&A

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

  • コミュニケーション支援用の画面を作っております

    コミュニケーション画面を作っています。記号絵の表示です。絵で書いた紙を使っていましたが、かける絵が少なく、小さい表示しかできず不便を感じております。自分で次の紙をめくるのも手足が不自由で苦労しております。タブを付け、画面を表示したり、数個のボタンで絵記号を選ぶようにしたいです。(1)画面の北面に記号が意味する説明を表示し、中面には絵記号を並べ、南面には画面を押すたびに絵記号と説明が切り替わるようにしようとしています。(2)また、動作や物、人など分野別にインデックスを付け、そのインデックスも南面にのボタンで切り替わるようにしたいです。(1)間ではできたのですが、タブ(インデックス)上に(1)を置く方法がどうしても解りません。またタブをボタンで切り替える事もできません。コードは以下です。お詳しい方がおられましたら教えてください。 import javax.swing.*; import java.awt.CardLayout; import java.awt.BorderLayout; import java.awt.event.*; public class ekigou2 extends JFrame implements ActionListener{ JPanel cardPanel; JPanel cardPanel2; CardLayout layout; public static void main(String[] args){ ekigou2 frame = new ekigou2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 300, 200); frame.setTitle("タイトル"); frame.setVisible(true); } ekigou2(){ JTabbedPane tabbedpane = new JTabbedPane(); //tubb_on_panel JPanel tabPanel1 = new JPanel(); JPanel tabPanel2 = new JPanel(); //tabb_image ImageIcon tbicon1 = new ImageIcon("./img/post.gif"); ImageIcon tbicon2 = new ImageIcon("./img/mail.gif"); //tab_set tabbedpane.addTab("tab1", tbicon1, tabPanel1); tabbedpane.addTab("tab2", tbicon2, tabPanel2); tabbedpane.setIconAt(1, tbicon1); tabbedpane.setIconAt(1, tbicon2); //card_icon_image ImageIcon cardicon1 = new ImageIcon("./img/dousa/203025行く.jpg"); ImageIcon cardicon2 = new ImageIcon("./img/dousa/203026来る.jpg"); //smol_icon_image ImageIcon icon1s = new ImageIcon("./img/dousa/203025行くs.jpg"); ImageIcon icon2s = new ImageIcon("./img/dousa/203026来るs.jpg"); // card_icon_roop JPanel card1 = new JPanel(); card1.add(new JButton("行く",cardicon1)); JPanel card2 = new JPanel(); card2.add(new JButton("来る",cardicon2)); cardPanel = new JPanel(); layout = new CardLayout(); cardPanel.setLayout(layout); cardPanel.add(card1, "button1"); cardPanel.add(card2, "button2"); // smol_icon_switch JButton button1 = new JButton("行く",icon1s); button1.addActionListener(this); button1.setActionCommand("button1"); JButton button2 = new JButton("来る",icon2s); button2.addActionListener(this); button2.setActionCommand("button2"); //小さいボタンを乗せる JPanel btnPanel = new JPanel(); btnPanel.add(button1); btnPanel.add(button2); JButton nextButton = new JButton("Next"); nextButton.addActionListener(this); nextButton.setActionCommand("Next"); JPanel btnPanel2 = new JPanel(); btnPanel2.add(nextButton); getContentPane().add(cardPanel, BorderLayout.NORTH);//card_button getContentPane().add(btnPanel, BorderLayout.WEST);//smol_button getContentPane().add(btnPanel2, BorderLayout.PAGE_END);//next_button getContentPane().add(tabbedpane);//tub_panel } public void actionPerformed(ActionEvent e){ String cmd = e.getActionCommand(); if (cmd.equals("Next")){ layout.next(cardPanel); } String cmd2 = e.getActionCommand(); layout.show(cardPanel, cmd2); } }

    • ベストアンサー
    • Java
  • Jave Swing 画像表示わからない

    java swingでGUIをプログラミングしている者です。 JLabelにImageIconの画像を貼り付けるプログラムです。↓ //画像を表示するプログラム //インポート import javax.swing.*; import java.awt.*; import java.awt.event.*; class gazopro extends JFrame{ JLabel l1; JButton b1; JPanel p; public static void main(String args[]) { //ウィンドウを作成また設定 gazopro frame = new gazopro(); frame.setBounds(300,200,700,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } gazopro(){ //コンポーネントを作成 p = new JPanel(); add(p); b1 = new JButton("画像を表示"); p.add(b1); ImageIcon icon = new ImageIcon("image.jpg"); l1 = new JLabel(icon); p.add(l1); } }   わからないところは、ImageIcon icon = new ImageIcon("image.jpg");のところです。 ImageIconの引数は、プログラミングを行っているパソコン(OS は Windows)のマイコンピューター→ Cドライブ→ MyJava→ image.jpgを指定しましたがこれで合ってますか? このプログラムでは、画像が表示されませんでした。 どうすればいいでしょうか? 解説と、ご指導宜しくお願いします。

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

  • イベント処理

    javaでプログラムを作り始めて間がないのですが、イベント処理に関して行き詰ってしまいました。望む動作は、メニュー→ゲーム→じゃんけんと選択すると、初期の状態として「じゃんけん・・・・」という文字とボタンが表示され、どれかボタンを押すと対応して「~ボタンが押されました」と表示させたいのですが、作ったプログラムだと、ボタンを押しても反応しません。後々メニューにもいくつかアイテムを追加したり、最終的にはコンピュータとじゃんけんをするプログラムを書きたいと思っています。ifを使った方法しか思いつかないのですが、どこがおかしいのでしょうか? import javax.swing.*; import java.awt.event.*; import java.awt.BorderLayout; public class question extends JFrame implements ActionListener{       JLabel label1;       JPanel p2;       public static void main(String[] args){            question test = new question("質問");            test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            test.setVisible(true);       }       question(String title){            setTitle(title);            setBounds( 10, 10, 300, 250);            JMenuBar menuBar = new JMenuBar();            JMenu menu = new JMenu("メニュー");            JMenu submenu = new JMenu("ゲーム");            JMenuItem subitem1 = new JMenuItem("じゃんけん");            subitem1.addActionListener(this);            subitem1.setActionCommand("Start");            submenu.add(subitem1);            menu.add(submenu);            menuBar.add(menu);            setJMenuBar(menuBar);            JPanel p1 = new JPanel();            p2 = new JPanel();            label1 = new JLabel("");            JButton btn1 = new JButton("グー");            btn1.addActionListener(this);            btn1.setActionCommand("goo");            JButton btn2 = new JButton("チョキ");            btn2.addActionListener(this);            btn2.setActionCommand("cyoki");            JButton btn3 = new JButton("パー");            btn3.addActionListener(this);            btn3.setActionCommand("paa");            p1.add(label1);            p2.add(btn1);            p2.add(btn2);            p2.add(btn3);            getContentPane().add(p1, BorderLayout.PAGE_START);            getContentPane().add(p2, BorderLayout.PAGE_END);            p2.setVisible(false);      }      public void actionPerformed(ActionEvent e){            String cmd = e.getActionCommand();            if (cmd.equals("Start")){                 p2.setVisible(true);                 label1.setText("じゃんけん・・・・");                 if (cmd.equals("goo")){                      label1.setText("グーが押されました");                 }else if (cmd.equals("cyoki")){                      label1.setText("チョキが押されました");                 }else if (cmd.equals("paa")){                      label1.setText("パーが押されました");                 }            }       } }

    • ベストアンサー
    • Java
  • 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
  • アプレット初心者です。

    アプレット初心者です。 電卓版もどきを作りたいんですが、お手本を見ながらやったはずなのに、コンパイル時に「main」がないという類のエラーが出ます。 どうすれば正常に動くようになるのか、どなたかご指南いただけませんか? よろしくお願いします。 ↓↓以下がソースです。 import java.awt.*; import javax.swing.*; public class D1 extends JApplet{ private JPanel pn; public init(){ JPanel keyPanel=new JPanel(); keyPanel.setLayout(new GridLayout(4,4)); pn.add(keyPanel,BorderLayout.CENTER); keyPanel.add(new JButton("7"),0); keyPanel.add(new JButton("8"),1); keyPanel.add(new JButton("9"),2); keyPanel.add(new JButton("÷"),3); keyPanel.add(new JButton("4"),4); keyPanel.add(new JButton("5"),5); keyPanel.add(new JButton("6"),6); keyPanel.add(new JButton("×"),7); keyPanel.add(new JButton("1"),8); keyPanel.add(new JButton("2"),9); keyPanel.add(new JButton("3"),10); keyPanel.add(new JButton("-"),11); keyPanel.add(new JButton("AC"),12); keyPanel.add(new JButton("0"),13); keyPanel.add(new JButton("="),14); keyPanel.add(new JButton("+"),15); } }

  • JavaのSwingのレイアウト

    Swingを学習中ですが、うまくレイアウトできません。 添付した画像のようなレイアウトにしたいです。 作ってみたものは以下です。 import java.awt.BorderLayout; import java.awt.Color; import javax.swing.*; public class LayoutTest { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextField searchFiled = new JTextField("テキスト"); String[] comboboxString = {"C", "C++", "Java"}; JComboBox comboBox = new JComboBox(comboboxString); JButton button = new JButton("ボタン"); panel.add(comboBox, BorderLayout.EAST); panel.add(searchFiled, BorderLayout.CENTER); panel.add(button, BorderLayout.WEST); frame.add(panel, BorderLayout.PAGE_START); JPanel redPanel = new JPanel(); redPanel.setBackground(Color.RED); frame.add(redPanel); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } }

    • ベストアンサー
    • Java
  • JavaでのSwingを使った簡易電卓作成

    Javaについて勉強中の学生です。 よろしくお願いします。 CGIによるプログラムは一通り勉強しました。 次にGUIによるプログラムを勉強中です。 そこで電卓を作ってみようと思いましたが、よくわからないところがあります。 ご教示いただければ幸いです。 一応外見だけは作ることができましたが、イベントを登録するときに電卓のボタンをbtn1,btn2,btn3…とすればイベントを登録することはできるのですが、それだとボタンの分だけ作成しなければならず修正を加えるのが大変になってしまいます。 例)btn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ hyozi.setText(hyozi.getText() + "1"); } } そこで「btn」に統一しボタンに表示されている文字列を使い簡潔にプログラムをまとめたいのですがどのようにすればよいのかわかりません。以下のプログラムに追加できるようなものを教えていただけないでしょうか? import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Dentaku{ private JFrame frame; private JPanel panel_fun,panel_ten; private JTextField hyozi; private JButton btn; public Dentaku(){ //フレームの作成してレイアウトをセット frame = new JFrame("簡易電卓プログラム"); frame.setLayout(new BorderLayout()); //パネルを作成してレイアウトをセット panel_fun = new JPanel(); panel_fun.setLayout(new GridLayout(2,3)); panel_ten = new JPanel(); panel_ten.setLayout(new GridLayout(4,3)); //表示用テキストボックスを作成 hyozi = new JTextField(); //電卓のボタンを作成 panel_fun.add(btn = new JButton("CA")); panel_fun.add(btn = new JButton("+")); panel_fun.add(btn = new JButton("-")); panel_fun.add(btn = new JButton("*")); panel_fun.add(btn = new JButton("/")); panel_fun.add(btn = new JButton("=")); panel_ten.add(btn = new JButton("1")); panel_ten.add(btn = new JButton("2")); panel_ten.add(btn = new JButton("3")); panel_ten.add(btn = new JButton("4")); panel_ten.add(btn = new JButton("5")); panel_ten.add(btn = new JButton("6")); panel_ten.add(btn = new JButton("7")); panel_ten.add(btn = new JButton("8")); panel_ten.add(btn = new JButton("9")); panel_ten.add(btn = new JButton("0")); //フレームに表示用テキストボックスをセット(BorderLayoutの北側) frame.add(hyozi,BorderLayout.NORTH); //フレームにパネルをセット(BorderLayoutの中央,南側) frame.add(panel_fun,BorderLayout.CENTER); frame.add(panel_ten,BorderLayout.SOUTH); //フレームの詳細設定 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,250); frame.setVisible(true); } //mainメソッドの定義 public static void main(String[] args){ Dentaku application = new Dentaku(); } }

    • ベストアンサー
    • Java
  • 【Java】モーダルDLG内でリストを表示

    Javaを学習し始めて間もない初心者です。よろしくお願いします。 ======================================== 開発環境:Eclipse SDK(Version: 3.5.0) 開発言語:Java ======================================== にて、プログラムを作成しています。 メインDLGの中に配置したボタンを押下したタイミングで、 モーダルDLGを開き、そのDLG内にリストを表示させたいのですが、 参考サイトなどを見ながら、とりあえずモーダルDLGは表示されましたが、 リストが表示されません。 下記に作成ソースを記載しますので、解決法をご存知の方おられましたら、 どこが悪く、どう修正すれば良いかについて、ご教示お願い致します。 なお、当方学習中の身ですので、より良いコードを書くために、 その他の指摘などももしあれば幸いです。 【ソースコード】 import javax.swing.*; import java.awt.BorderLayout; import java.awt.Dialog; import java.awt.Frame; import java.awt.event.*; import java.awt.*; public class TestList extends JFrame implements ActionListener{ Dialog dlg; Frame frm; public static void main(String[] args){ TestList frame = new TestList(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 400, 100); frame.setTitle("リスト表示"); frame.setVisible(true); } TestList(){ { // リスト表示ボタンを追加 JButton btn = new JButton("リストDLGを表示"); btn.addActionListener(this); JPanel p2 = new JPanel(); p2.add(btn); getContentPane().add(p2, BorderLayout.CENTER); } } // ボタン押下イベントを取得する関数 public void actionPerformed(ActionEvent e){ // リスト表示ボタンが押下された時 if (frm == null) { frm = new Frame("リスト"); frm.setSize(200 , 200); frm.setVisible(true); String list_n[] = {"テスト1", "テスト2", "テスト3", "テスト4", "テスト5"}; JList list = new JList(list_n); dlg = new Dialog(frm, "リスト" , true); JScrollPane sp = new JScrollPane(); sp.getViewport().setView(list); sp.setPreferredSize(new Dimension(200, 80)); JPanel p = new JPanel(); p.add(sp); dlg.add(p, BorderLayout.CENTER); } } }

    • ベストアンサー
    • Java