ItemEvent事件,简单计算器

Example10.java文件:

public class Example10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WindowOperation win = new WindowOperation();
		win.setBounds(100,100,390,360);
		win.setTitle("简单计算器");
	}

}
WindowOperation.java文件:

import java.awt.*;

import javax.swing.*;


public class WindowOperation extends JFrame{
	JTextField inputNumber1,inputNumber2;
	JComboBox choiceFuhao;
	JTextArea textShow;
	JButton button;
	OperatorListener operator;//监视ItemEvent事件的监视器
	ComputerListener computer;//监视ActionEvent事件的监视器
	public WindowOperation(){
		init();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	void init() {
		// TODO Auto-generated method stub
		setLayout(new FlowLayout());
		inputNumber1 = new JTextField(5);
		inputNumber2 = new JTextField(5);
		choiceFuhao = new JComboBox();
		button = new JButton("计算");
		choiceFuhao.addItem("选择运算符号:");
		String[]a = {"+","-","*","/"};
		for(int i = 0;i < a.length;i++)
			choiceFuhao.addItem(a[i]);
		textShow = new JTextArea(9,30);
		operator = new OperatorListener();
		computer = new ComputerListener();
		operator.setJComboBox(choiceFuhao);
		operator.setWorkTogether(computer);
		computer.setJTextField1(inputNumber1);
		computer.setJTextField2(inputNumber2);
		computer.setJTextArea(textShow);
		choiceFuhao.addItemListener(operator);
		button.addActionListener(computer);
		add(inputNumber1);
		add(choiceFuhao);
		add(inputNumber2);
		add(button);
		add(new JScrollPane(textShow));
	}
}
OperatorListener.java文件:

import java.awt.event.*;

import javax.swing.*;


public class OperatorListener implements ItemListener{
	JComboBox choice;
	ComputerListener workTogether;
	public void setJComboBox(JComboBox box){
		choice = box;
	}
	public void setWorkTogether(ComputerListener computer){
		workTogether = computer;
	}
	public void itemStateChanged(ItemEvent e){
		String fuhao = choice.getSelectedItem().toString();
		workTogether.setFuhao(fuhao);
	}
}
ComputerListener.java文件:

import java.awt.event.*;

import javax.swing.*;


public class ComputerListener implements ActionListener{
	JTextField inputNumber1,inputNumber2;
	JTextArea textShow;
	String fuhao;
	public void setJTextField1(JTextField t){
		inputNumber1 = t;
	}
	public void setJTextField2(JTextField t){
		inputNumber2 = t;
	}
	public void setJTextArea(JTextArea area){
		textShow = area;
	}
	public void setFuhao(String s){
		fuhao = s;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		try{
			double number1 = Double.parseDouble(inputNumber1.getText());
			double number2 = Double.parseDouble(inputNumber2.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");
		}
	}
}
效果图:

ItemEvent事件,简单计算器_第1张图片

你可能感兴趣的:(JAVA学习)