• ベストアンサー

finalの意味

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ExerciseD8L1{ public static void main(String args[]){ final JFrame myFrame = new JFrame(); myFrame.getContentPane().setLayout(new BorderLayout()); final JLabel myLabel = new JLabel(); JPanel myPanel = new JPanel(); myFrame.getContentPane().add(myLabel,BorderLayout.CENTER); myFrame.getContentPane().add(myPanel,BorderLayout.NORTH); JButton btn1 = new JButton("btn1"); btn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ myLabel.setText("btn1がクリックされました"); } }); }); myPanel.add(btn1); myFrame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); myFrame.setSize(400,100); myFrame.setVisible(true); } } これで、finalをつけないとコンパイルできません。 なぜ、fianlが必要なんでしょう。 どのようなときにfinalを用いるのでしょう。 お願いします。

  • Java
  • 回答数3
  • ありがとう数4

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

  • ベストアンサー
  • Harry_
  • ベストアンサー率55% (36/65)
回答No.3

JFrame myFrame の前の final は必要ありません。 なくてもコンパイルできます。試してみてください。 JLabel myLabel の前の final は、 public void actionPerformed(ActionEvent ae){  myLabel.setText("btn1がクリックされました"); } というローカル内部クラス内で参照されているので、 final でなければならず、 そのより詳しい説明が(下手ですが) No.2 の回答です。

ballack
質問者

お礼

詳しい説明ありがとうございました。

その他の回答 (2)

  • Harry_
  • ベストアンサー率55% (36/65)
回答No.2

うまく説明できるかわかりませんが。 ローカル内部クラスからアクセスされる ローカル変数は final でなければならない、 ということになっています。 その理由は、ローカル変数はスコープを外れた時点で なくなってしまいますが、 それを利用しているローカル内部クラスのインスタンスは、 フィールドに代入するなり、メソッドの戻り値にするなり して、ずっと存続させることができるからです。 もしローカル内部クラスが参照しているローカル変数の 内容を変更できるとしたら、ローカル内部クラスが アクセスするローカル変数というのは、一体どの時点に おけるものなのか、と考えると、曖昧さを排除するには 変更不可能にするしかない、というのが何となくわかると思います。 多分バイトコードレベルでは、 ローカル内部クラスはローカル変数を参照しているのでは なく、直接その参照先の値を利用しているのだと思います。

ballack
質問者

お礼

JFrameとJLabelにはなぜfinalをつけなければならないのか、具体的に教えていただけるとより良かったのですが... まだまだ自分は勉強不足ですね。 回答ありがとうございました。

  • Sequel
  • ベストアンサー率41% (23/56)
回答No.1

Javaのプログラミングを行った事が無いので、まちがっているかもしれません。的外れでしたらごめんなさい…。 http://www2s.biglobe.ne.jp/~yuuki_ki/java_class12.htm 上記ページを参考にしました。 おそらく、質問のコードは「メンバ変数宣言時にfinalを使用」というケースに該当すると思います。 参照ページの説明によると、final修飾子をつけることによって、変数生成以降に異なる値が入ることを防いでいるっぽいです。 いろいろなケースでfinal修飾子をつけることがあるみたいですね。参考URLでは、まさにそのことについての記事が掲載されていますので、ご覧になってください。

参考URL:
http://www2s.biglobe.ne.jp/~yuuki_ki/java_class12.htm
ballack
質問者

お礼

JFrameとJLabelにはなぜfinalをつけなければならないのか、具体的に教えていただけるとより良かったのですが... まだまだ自分は勉強不足ですね。 回答ありがとうございました。

関連するQ&A

  • ActionListener について

    JFrame の上に JPanel があり、JPanel の上に JButton があるとします。 JButton が押されたことを JFrame に知らせるにはどうしたらいいでしょうか。 わかるところまで書いてみました。 class MyFrame extends JFrame implements ActionListener { MyPanel myPanel = new MyPanel(); MyFrame() { myPanel.addActionListener(this); add(myPanel); } public void actionPerformed(ActionEvent e) { System.out.println("The button on the MyPanel was pressed!"); } public static void main(String[] args) { new MyFrame(); } } class MyPanel extends JPanel implements ActionListener { JButton button1 = new JButton(); MyPanel() { button1.addActionListener(this); add(button1); } public void actionPerformed(ActionEvent e) { } }

    • ベストアンサー
    • 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
  • 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での背景画像表示

    現在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について質問

    import javax.swing.*; import java.awt.Color; import java.awt.Graphics; import java.awt.event.*; public class tt extends JFrame { public tt(){ setTitle("hogehoge"); setBounds( 10, 10, 300, 200); JButton btn1 = new JButton("続ける"); final JPanel p = new JPanel(); p.setBackground(Color.white); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event){ } } ); p.add(btn1); getContentPane().add(p); } public static void main(String[] args){ tt test = new tt(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); /* 実際に表示する */ } } Graphics g; g.drawString("ほげさんじょう",130,120);を追加しても コンパイルが通りません。 どうすれば画面に文字列を表示するんですか? あとテキストフィールドを追加するさいに、座標していとかして追加できないんですか?

  • 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での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でプログラムを作り始めて間がないのですが、イベント処理に関して行き詰ってしまいました。望む動作は、メニュー→ゲーム→じゃんけんと選択すると、初期の状態として「じゃんけん・・・・」という文字とボタンが表示され、どれかボタンを押すと対応して「~ボタンが押されました」と表示させたいのですが、作ったプログラムだと、ボタンを押しても反応しません。後々メニューにもいくつかアイテムを追加したり、最終的にはコンピュータとじゃんけんをするプログラムを書きたいと思っています。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でストップウォッチが上手く作れません

    あるサイトを参考にして作ってみたんですが うまく動いてくれません、多分おかしいところだらけですが どこがダメか教えてもらえるとありがたいです package timeP; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class timeC extends JFrame implements Runnable { private JPanel contentPane; private JTextField textField; private Thread th = null; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { timeC frame = new timeC(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public timeC() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 85); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); JPanel panel_3 = new JPanel(); contentPane.add(panel_3); panel_3.setLayout(new BorderLayout(0, 0)); textField = new JTextField(); panel_3.add(textField, BorderLayout.CENTER); textField.setColumns(10); JPanel panel_2 = new JPanel(); contentPane.add(panel_2); panel_2.setLayout(new BorderLayout(0, 0)); JButton JButtonstart = new JButton(" 開始 "); JButtonstart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if ( th == null ) { th = new Thread ( ); th.start(); } } }); panel_2.add(JButtonstart, BorderLayout.CENTER); JPanel panel_1 = new JPanel(); contentPane.add(panel_1); panel_1.setLayout(new BorderLayout(0, 0)); JButton JButtonstop = new JButton(" 停止 "); JButtonstop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if ( th != null ){ th = null; } } }); panel_1.add(JButtonstop, BorderLayout.CENTER); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(new BorderLayout(0, 0)); JButton btnNewButton_2 = new JButton("New button"); panel.add(btnNewButton_2, BorderLayout.CENTER); } @Override public void run() { //TODO 自動生成されたメソッド・スタブ int i; while ( th != null ){ i = Integer.parseInt( textField.getText() ); try { Thread.sleep(10000); if ( th == null ) break; } catch ( InterruptedException e ){ break; } textField.setText( Integer.toString( i + 1 ) ); } } }

  • [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を始めた初心者です(>_<) 提出期限が近づいていて焦っています; お手数ですが回答お願いいたします・・・!!

専門家に質問してみよう