java事件处理
1.ActionEven事件
文本框,按钮,菜单项,密码框,单选按钮都可以出发ActionEvent事件
使用
addActionListener(ActionListener listen1)
来注册监视器
ActionListener本来是一个接口,我们必须写类(或者其他接口)来实现它的唯一方法
actionPerformed(ActionEvent e)
这里的e是事件源给该方法的一个参数,ActionEvent类有两个方法
public Object getSource();//返回事件源的上转型对象的引用 public String getActionCommand()//返回一个相关的“命令”字符串,比如这个ActionEvent是文本框,那就是返回他的文本内容
统计单词字数的一个代码
class Component extends JFrame{ JTextField test1; JButton button1; JTextArea testArea1; JRadioButton radioButton1,radioButton2; ButtonGroup group1; Component(){ init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ setLayout(new FlowLayout()); test1=new JTextField(8); button1=new JButton("huang"); testArea1=new JTextArea(8,24); add(test1); add(button1); add(new JScrollPane(testArea1)); AListener a=new AListener(test1,button1,testArea1); test1.addActionListener(a);//需要import java.awt.event.ActionListener; button1.addActionListener(a); } } class AListener implements ActionListener{ JTextField test1; JButton button1; JTextArea testArea1; AListener(JTextField test,JButton button,JTextArea testArea){ test1=test; button1=button; testArea1=testArea; } public void actionPerformed(ActionEvent e){ String a=test1.getText(); testArea1.append(a+"的长度"+a.length()); } }
ItemEvent事件
选择框和下拉列表都可以触发ItemEvent事件
注册监视器
addItemListener(ItemListener listen1)
ItemListener接口也只有一个
对应事件触发的,可以用 combo1.getSelectedItem()获得事件的事件源
public void itemStateChanged(ItemEvent e)
简单计算器代码,(有个bug,下拉框要换了其他才能激活
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyEvent; public class test{ public static void main(String args[]){ Component window1=new Component(); window1.setBounds(40,40,300,200); } } class Component extends JFrame{ JTextField test1,test2; JButton button1; JTextArea testArea1; JRadioButton radioButton1,radioButton2; ButtonGroup group1; JComboBox combo1; Component(){ init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ setLayout(new FlowLayout()); test1=new JTextField(4); test2=new JTextField(4); button1=new JButton("go"); testArea1=new JTextArea(5,20); combo1=new JComboBox(); combo1.addItem("+"); combo1.addItem("-"); combo1.addItem("*"); combo1.addItem("/"); add(test1); add(combo1); add(test2); add(button1); add(new JScrollPane(testArea1)); Computer computer1=new Computer(test1,test2,testArea1); Fuhao fuhao1=new Fuhao(computer1,combo1); combo1.addItemListener(fuhao1); button1.addActionListener(computer1); } } class Fuhao implements ItemListener{ Computer computer1; JComboBox combo1; Fuhao(Computer a,JComboBox b){ computer1=a; combo1=b; } public void itemStateChanged(ItemEvent e){ // System.out.println(combo1.getSelectedItem().toString()); computer1.getFuhao(combo1.getSelectedItem().toString()); } } class Computer implements ActionListener{ JTextField a,b; JTextArea c; String fuhao; double result; void getFuhao(String a){ fuhao=a; } Computer(JTextField a,JTextField b,JTextArea c){ this.a=a; this.b=b; this.c=c; } public void actionPerformed(ActionEvent e){ try{ // System.out.println(fuhao); // System.out.println(a.getText()); // System.out.println(b.getText()); double a1=Double.parseDouble(a.getText()); double b1=Double.parseDouble(b.getText()); if(fuhao.equals("+")){ result=a1+b1; }else if(fuhao.equals("-")){ result=a1-b1; }else if(fuhao.equals("*")){ result=a1*b1; }else if(fuhao.equals("/")){ result=a1/b1; } c.append(a1+" "+fuhao+" "+b1+"="+" "+result+"\n"); } catch(Exception exp){ c.append("\n请输入数字\n"); } } }