码迷,mamicode.com
首页 > 编程语言 > 详细

java简单计算器

时间:2015-06-08 17:28:25      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:java简单计算器   java计算器   

自己写着写着 忘了。。顺便看看书 哈哈~~
Window:

import java.awt.*;
public class Window {
	public static void main(String args[]){
		WindowInit win=new WindowInit();
		Container con=win.getContentPane();
		con.setBackground(Color.green);
	}
}


WindowInit:

import java.awt.*;
import javax.swing.*;
public class WindowInit extends JFrame{
	JTextField input1,input2;
	JComboBox choicefuhao;
	JButton button;
	JTextArea showarea;
	OperatorListener operator;
	ComputerListener computer;
	public WindowInit(){
		init();
		setBounds(100,100,429,329);
		setTitle("计算器");
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	void init(){
		setLayout(new FlowLayout());
		input1=new JTextField(5);
		input2=new JTextField(5);
		button=new JButton("确定");
		choicefuhao=new JComboBox();
		choicefuhao.addItem("请选择符号");
		choicefuhao.addItem("+");
		choicefuhao.addItem("-");
		choicefuhao.addItem("*");
		choicefuhao.addItem("/");
		showarea=new JTextArea(9,36);
		operator=new OperatorListener();
		computer=new ComputerListener();
		operator.setChoicefuhao(choicefuhao);
		operator.setWorktogether(computer);
		computer.setInput1(input1);
		computer.setInput2(input2);
		computer.setTextArea(showarea);
		button.addActionListener(computer);//感觉这里也应该加上
                  //input1.addActionListener,input2.addActionListener
		choicefuhao.addItemListener(operator);
		add(input1);
		add(choicefuhao);
		add(input2);
		add(button);
		add(showarea);
	}
}


OperatorListener:

import javax.swing.*;
import java.awt.event.*;
public class OperatorListener implements ItemListener{
 JComboBox choicefuhao;
 ComputerListener worktogether;
 public void setChoicefuhao(JComboBox e){
  choicefuhao=e;
 }
 public void setWorktogether(ComputerListener e){
  worktogether=e;
 }
 public void itemStateChanged(ItemEvent e){
  String fuhao=choicefuhao.getSelectedItem().toString();
  worktogether.setfuhao(fuhao);
 }
}
ComputerListener:

import java.awt.event.*;
import javax.swing.*;
public class ComputerListener implements ActionListener{
	JTextField input1,input2;
	JButton button;
	JTextArea showarea;
	String fuhao;
	public void setInput1(JTextField e){
		input1=e;
	}
	public void setInput2(JTextField e){
		input2=e;
	}
	public void setfuhao(String e){
		fuhao=e;
	}
	public void setTextArea(JTextArea e){
		showarea=e;
	}
	public void actionPerformed(ActionEvent e){
		double num1=Double.parseDouble(input1.getText());
		double num2=Double.parseDouble(input2.getText());
		double result=0;
		if(fuhao.equals("+"))
			result=num1+num2;
		else if(fuhao.equals("-"))
			result=num1-num2;
		else if(fuhao.equals("*"))
			result=num1*num2;
		else 
			result=num1/num2;
		showarea.append(num1+fuhao+num2+"="+result+"\n");
	}

}


这是运行结果:

技术分享

java简单计算器

标签:java简单计算器   java计算器   

原文地址:http://blog.csdn.net/su20145104009/article/details/46413891

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!