actionListenerについて

このQ&Aのポイント
  • actionListenerについての質問文です。labelにActionListenerが登録できない理由について知りたいです。
  • 質問文章から要約文を生成すると、labelにActionListenerが登録できない理由について知りたいです。
  • labelにActionListenerが登録できない理由について教えてください。
回答を見る
  • ベストアンサー

actionListenerについて

ど素人です 改行の関係でimportは省略してます labelにActionListenerが登録できないのですが、なぜでしょうか class test extends JFrame{ test() { getContentPane().setLayout(new FlowLayout()); JLabel label = new JLabel("てすと"); JButton b1 = new JButton("OK"); b1.addActionListener(label); getContentPane().add(b1); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("JButtonTest"); setSize(200, 100); setVisible(true); } public static void main(String[] args) { new test(); } } class LetLabel extends JLabel implements ActionListener{ public void LetLabe(){ } public void actionPerformed(ActionEvent ae){ } }

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

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

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

b1.addActionListener(label); 引数にJLabelを指定していますが JLabelはActionListenerを継承していないので これはできません やるのなら LetLabel label = new LetLabel(); こうしなければダメ LetLabelはActionListenerを継承しているので b1.addActionListener(label); することができます new LetLabel("てすと"); にしたい場合はLetLabelにコンストラクタを追加してください

JoyWorld
質問者

お礼

ありがとうございました おかげで解決しました

関連するQ&A

  • 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
  • Eclipseから実行すると画像が読み込まれない

    下記のコードをEclipseから実行すると画像が読み込まれないのですが、コマンドプロンプトから実行すると画像が読み込まれます。 コードが同じなのに不思議です…。 原因はどんなことが考えられるのでしょうか? class test extends JFrame{ public static void main(String args[]){ test frame = new test("タイトル"); frame.setVisible(true); } test(String title){ setTitle(title); setBounds(100, 100, 500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); ImageIcon icon1 = new ImageIcon("img.jpg"); JLabel label1 = new JLabel(icon1); p.add(label1); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); } }

    • ベストアンサー
    • Java
  • JavaのSwingで別のフォームを閉じる方法

    こんにちは。 JavaのSwingにおいて,以下のようなプログラムがあるとします。 その場合,A.javaでボタンを押すと,B.javaのフォームが閉じるようにしたいのですがどのようにしたらいいのでしょうか?よろしくお願いします。 ****A.java**** import ☆省略☆; public class A extends JFrame implements ActionListener{ //ボタン JButton end; public A(){ //フレームの設定 setVisible(true); setTitle("テスト"); //コンテナの設定 con = getContentPane(); //ボタン end = new JButton(" 終了 "); end.addActionListener(this); end.setActionCommand("end"); con.add(end); pack(); } public void actionPerformed(ActionEvent e){ String m = e.getActionCommand(); if(m == "end"){ ※ここに書くべき処理 } } } ****B.java**** import ☆省略☆; public class B extends JFrame{ //コンテナ Container con; public Answer(){ //フレームの設定 setVisible(true); setTitle("B"); //コンテナに関わる設定 con = getContentPane(); } }

  • ボタンが表示されません

    //テキストパッドをつくっています。 //このプログラムはボタンが表示されませんなぜ? 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); } }

  • 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
  • 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
  • フレームを消したい

    こんにちは。 いつもお世話になっております。 また、どうしてもわからないことが出てきてしまいました。 質問よろしいでしょうか? 以下のプログラムを実行して 「次へ」と書かれたボタンをクリックし 次に現れるフレーム上の「このフレームだけ消す」 と書かれたボタンをクリックしたときに 「f2」(2つ目のフレーム)だけ閉じるようにしたいのです。 以下の場合は両方とも閉じてしまうのです。 何かよい方法ご存知ありませんか? よろしくお願いいたします。 ================================================== import java.awt.*; import javax.swing.*; import java.awt.event.*; class unload { public static void main(String[] args) { JFrame f=new JFrame("kiki"); Origin p=new Origin(); Container ct=f.getContentPane(); ct.add(p); f.pack(); f.setVisible(true); } } class Origin extends JPanel implements ActionListener { JButton b; Origin() { b=new JButton("次へ"); this.add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource()==b) { JFrame f2=new JFrame(); Origin2 p2=new Origin2(); Container ct=f2.getContentPane(); ct.add(p2); f2.pack(); f2.setVisible(true); } } } class Origin2 extends JPanel implements ActionListener { JButton b2; Origin2() { b2=new JButton("このフレームだけ消す"); this.add(b2); b2.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b2) { 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
  • 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
  • JFrameのエラーの原因が分かりません。

     java初心者です、宜しくお願いします。  JFrameのコードを下記のように書いてやりましたが、「サーバーにデプロイするものが見つかり ません。」というエラーが出ます。   色々と参考書を見ながら勉強していますが、エラーの原因がさっぱりわかりません。    どなたか宜しくお願いします。 ============================================================================ public class JF4 extends JFrame { public void init() { Container cnt = getContentPane(); setBackground(Color.RED); setSize(400,400); setTitle("title"); setVisible(true); } } ================================================================================= public class JF4 extends JFrame { public void init() { Container cnt = getContentPane(); JPanel panel = new JPanel(); panel.setBackground(Color.RED); getContentPane().add(panel); setSize(400,400); setTitle("title"); setVisible(true); } }

専門家に質問してみよう