ItemEvent事件源:
选择框,下拉列表都可以触发ItemEvent事件
注册监视器:
能够触发ItemEvent事件的组件使用addItemListener(ItemListener listen)
将实现ItemListener的接口的类的实例注册为事件源的监视器
ItemListener接口:
接口中仅有一个方法,public void itemStateChanged(ItemEvent e)
对发生事件做出处理
返回发生ItemEvent的事件源:
getSource();
getItemSelectable();
下面的例子是简单的计算器,实现如下功能
1、用户在窗口中的两个文本框中输入参与运算的两个操作数
2、用户在下拉列表中选择运算符将触发ItemEvent事件,ItemEvent事件的监视器(operatorListener类负责创建)获得运算符,并将运算符传递给ActionEvent事件的监视器computer
3、用户点击按钮触发ActionEvent事件,监视器computer(computerListener类负责创建)给出运算结果
设置字体:
Font font =new Font(“宋体”,Font.BOLD,22);
inputNumberOne.setFont(font);
public class Example9_8 {public static void main(String args[]){WindowOperation win = new WindowOperation();win.setBounds(100,100,390,360);win.setTitle("简单计算器");}}
import java.awt.*;
import javax.swing.*;public class WindowOperation extends JFrame{private static final long serialVersionUID = 3530745896462734297L;JTextField inputNumberOne,inputNumberTwo;//添加两个输入文本框JComboBox<String> choiceFuhao;//下拉列表JTextArea textShow;//文本区JButton button;//按钮OperatorListener operator;//监视itemEventComputerListener computer;//监视ActionEventpublic WindowOperation()//构造函数{init();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}void init(){setLayout(new FlowLayout());//设置布局Font font =new Font("宋体",Font.BOLD,22);//Font font = new Font("宋体",Font.BOLD,22);inputNumberOne = new JTextField(5);//inputNumberOne.setFont(font);inputNumberTwo = new JTextField(5);//分配空间inputNumberTwo.setFont(font);choiceFuhao = new JComboBox<String>();//重点后面的<>,不能省略choiceFuhao.setFont(font);button = new JButton("计算");button.setFont(font);choiceFuhao.addItem("选择运算符号:");//将后面的字符串添加到下拉列表中去String []a = {"+","-","*","/"};for(int i=0;i<a.length;i++){choiceFuhao.addItem(a[i]);//将后面的字符串添加到下拉列表中去}textShow = new JTextArea(9,30);//textShow.setFont(font);operator = new OperatorListener();computer = new ComputerListener();operator.setJComboBox(choiceFuhao);operator.setWorkTogether(computer);computer.setJTextFieldOne(inputNumberOne);computer.setJTextFieldTwo(inputNumberTwo);computer.setJTextArea(textShow);choiceFuhao.addItemListener(operator);button.addActionListener(computer);this.add(inputNumberOne);this.add(choiceFuhao);this.add(inputNumberTwo);this.add(button);this.add(new JScrollPane(textShow));}}
import java.awt.event.*;
import javax.swing.*;public class OperatorListener implements ItemListener{JComboBox<String> choice;//下拉列表ComputerListener workTogether;//监视器public void setJComboBox(JComboBox<String> box){choice = box;}public void setWorkTogether(ComputerListener computer){workTogether = computer;}public void itemStateChanged(ItemEvent e)//下拉列表后触发该事件{//作用:在复选框中得到符号,并且传递给文本框的监视器String fuhao = choice.getSelectedItem().toString();workTogether.setFuhao(fuhao);}}
import java.awt.event.*;import javax.swing.*;public class ComputerListener implements ActionListener{JTextField inputNumberOne,inputNumberTwo;//文本框JTextArea textShow;//文本区String fuhao;//字符串public void setJTextFieldOne(JTextField t){inputNumberOne = t;}public void setJTextFieldTwo(JTextField t){inputNumberTwo = t;}public void setJTextArea(JTextArea area){textShow = area;}public void setFuhao(String s){fuhao = s;}public void actionPerformed(ActionEvent e)//用户单击鼠标,触发事件,做出处理{try{double number1 = Double.parseDouble(inputNumberOne.getText());double number2 = Double.parseDouble(inputNumberTwo.getText());double result =0;if(fuhao.equals("+")){result = number1+number2;}else if(fuhao.equals("-")){result = number1-number2;}else if(fuhao.equals("*")){result = number1*number2;}else if(fuhao.equals("/")){result = number1/number2;}textShow.append(number1+" "+fuhao+" "+number2+" = "+result+"\n");}catch(Exception exp){textShow.append("\n请输入数字字符\n");}}}