用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java 实现计算器

2013-01-19 作者: 海大软件1102班举报

[java]代码库

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Java计算器 Calculater
 */
public class Calculater extends JFrame implements ActionListener {
	JFrame jf;
	JTextField jtf;
	String[] b = new String[] { "+", "-", "*", "/", "." };
	JButton jis = new JButton("=");
	private int temp = 0;
	private float result = 0;
	private float fnumber = 0, bnumber = 0;

	public Calculater() {
		init();
	}

	public void init() {
		jf = new JFrame();
		jtf = new JTextField();

		Container c = jf.getContentPane();// Returns the contentPane object for
											// this frame.
		jtf.setHorizontalAlignment(JTextField.RIGHT);// 设置文本的水平对齐方式
		jtf.setLayout(new GridLayout());
		c.add(jtf, BorderLayout.NORTH);//

		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(4, 4));// 对于GridLayout(int rows,int
											// cols)详情请参见API
		c.add(jp, BorderLayout.CENTER);/* 这里刚开始犯了一低级错误,将c写成了jp,逻辑调用都出现了问题 */

		JButton[] ja = new JButton[10];
		JButton[] jb = new JButton[5];
		for (int i = 0; i < 10; i++) {
			ja[i] = new JButton("" + i);
			ja[i].addActionListener(this);
		}
		for (int j = 0; j < 5; j++) {
			jb[j] = new JButton("" + b[j]);
			jb[j].addActionListener(this);
		}

		for (int i = 0; i < 10; i++) {
			jp.add(ja[i]);
		}
		for (int j = 0; j < 5; j++) {
			jp.add(jb[j]);
		}

		jp.add(jis);
		jis.addActionListener(this);
		jf.setSize(200, 300);
		jf.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		String s = e.getActionCommand();
		if (s.equals("+")) {
			this.fnumber = Float.parseFloat(this.jtf.getText());
			this.jtf.setText("");
			this.temp = 1;
		} else if (s.equals("-")) {
			this.fnumber = Float.parseFloat(this.jtf.getText());
			this.jtf.setText("");
			this.temp = 2;
		} else if (s.equals("*")) {
			this.fnumber = Float.parseFloat(this.jtf.getText());
			this.jtf.setText("");
			this.temp = 3;
		} else if (s.equals("/")) {
			this.fnumber = Float.parseFloat(this.jtf.getText());
			this.jtf.setText("");
			this.temp = 4;
		} else if (s.equals(".")) {
			if (jtf.equals("")) {
				jtf.setText("0.");
			} else {
				jtf.setText(jtf.getText() + ".");
			}
		} else if (s.equals("=")) {
			this.bnumber = Integer.parseInt(this.jtf.getText());
			switch (this.temp) {
			case 1:
				result = fnumber + bnumber;
				this.jtf.setText(String.valueOf(result));
				break;
			case 2:
				result = fnumber - bnumber;
				this.jtf.setText(String.valueOf(result));
				break;
			case 3:
				result = fnumber * bnumber;
				this.jtf.setText(String.valueOf(result));
				break;
			case 4:
				result = fnumber / bnumber;
				this.jtf.setText(String.valueOf(result));
				break;
			}
		} else {
			jtf.setText(jtf.getText() + e.getActionCommand());
		}
	}

	public static void main(String[] args) {
		new Calculater();
	}
}

[代码运行效果截图]


java 实现计算器


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...