//TestActionEvent.java import 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.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()); 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.java import 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!"); } }
java awt 事件监听器
最新推荐文章于 2023-01-31 14:25:52 发布