按按钮事件处理
1 package ch11_3; 2 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.*; 8 9 public class EventTest extends JFrame implements ActionListener { 10 11 private JButton button1; 12 private JButton button2; 13 private JTextField Text1; 14 15 public EventTest() { 16 super("事件使用实例_单击事件"); 17 button1=new JButton("欢迎"); 18 button2=new JButton("再见"); 19 Text1=new JTextField("试试看☺",20); 20 Text1.setEditable(false); 21 this.setBounds(500, 500, 500, 300); 22 this.setLayout(new FlowLayout(FlowLayout.LEFT)); 23 this.add(button1); 24 this.add(button2); 25 this.add(Text1); 26 //注册监听器 27 button1.addActionListener(this); 28 button2.addActionListener(this); 29 30 } 31 32 33 @Override 34 public void actionPerformed(ActionEvent e) {//单击事件的响应处理方法 35 if(e.getSource()==button1) { 36 Text1.setText("欢迎欢迎!☺☺"); 37 } 38 else { 39 Text1.setText("拜拜┏(^0^)┛"); 40 } 41 } 42 public static void main(String[] args) { 43 EventTest a=new EventTest(); 44 a.setVisible(true); 45 a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 46 47 } 48 49 }
道阻且长,行则将至

浙公网安备 33010602011771号