java awt 事件監聽器

來源:互聯網
上載者:User
//TestActionEvent.javaimport java.awt.*;import java.awt.event.*;public class TestActionEvent{public static void main(String[] args){Frame f=new Frame("TestActionEvent");Button b=new Button("Press Me !");Monitor bh=new Monitor();b.addActionListener(bh);f.add(b,BorderLayout.CENTER);f.pack();
//TestTextField.javaimport java.awt.*;import java.awt.event.*;class TestTextField{public static void main(String[] args){new TFFrame();}}class TFFrame extends Frame{TFFrame(){TextField tf=new TextField();add(tf);tf.addActionListener(new TFActionListener());pack();setVisible(true);}}class TFActionListener implements ActionListener{public void actionPerformed(ActionEvent e){TextField tf=(TextField)e.getSource();System.out.println(tf.getText());tf.setText("");}}

//TestTextField.java 更改回顯字元import java.awt.*;import java.awt.event.*;class TestTextField{public static void main(String[] args){new TFFrame();}}class TFFrame extends Frame{TFFrame(){TextField tf=new TextField();add(tf);tf.addActionListener(new TFActionListener());tf.setEchoChar('*');pack();setVisible(true);}}class TFActionListener implements ActionListener{public void actionPerformed(ActionEvent e){TextField tf=(TextField)e.getSource();System.out.println(tf.getText());tf.setText("");}}

//加法計算機import java.awt.*;import java.awt.event.*;class TestTFMath{public static void main(String[] args){new TFMath();}}class TFMath extends Frame{TextField a=null;TextField b=null;TextField c=null;TFMath(){a=new TextField();b=new TextField();c=new TextField();Button plus=new Button("計算");Label lbPlus=new Label("+");Label lbIs=new Label("=");plus.addActionListener(new ActionListener_plus(this));setLayout(new FlowLayout());add(a);add(lbPlus);add(b);add(lbIs);add(c);add(plus);pack();setVisible(true);}}class ActionListener_plus implements ActionListener{/*TextField a,b,c;ActionListener_plus(TextField a,TextField b,TextField c){this.a=a;this.b=b;this.c=c;}*/TFMath tm=null;ActionListener_plus(TFMath tm){this.tm=tm;}public void actionPerformed(ActionEvent e){int i_a=Integer.parseInt(tm.a.getText());int i_b=Integer.parseInt(tm.b.getText());tm.c.setText(""+(i_a+i_b));}}

//匿名內部類事件監聽器import java.awt.*;import java.awt.event.*;class TestTFMath{public static void main(String[] args){new TFMath();}}class TFMath extends Frame{TFMath(){final TextField a=new TextField();final TextField b=new TextField();final TextField c=new TextField();Button plus=new Button("計算");Label lbPlus=new Label("+");Label lbIs=new Label("=");plus.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){c.setText(""+(Integer.parseInt(a.getText())+Integer.parseInt(b.getText())));}});setLayout(new FlowLayout());add(a);add(lbPlus);add(b);add(lbIs);add(c);add(plus);pack();setVisible(true);}}



//TestActionEvent2.javaimport java.awt.*;import java.awt.event.*;public class TestActionEvent2{public static void main(String[] args){Frame f=new Frame("TestActionEvent");Button btn_start=new Button("Start");Button btn_stop=new Button("Stop");Monitor bh=new Monitor();btn_start.addActionListener(bh);btn_stop.addActionListener(bh);f.add(btn_start,BorderLayout.WEST);f.add(btn_stop,BorderLayout.EAST);f.pack();f.setVisible(true);}}class Monitor implements ActionListener{public void actionPerformed(ActionEvent e){System.out.println("Button " + e.getActionCommand() + " Pressed!");}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.