利用java的swing组件制作简易计算器

1、首先页面制作


整体的页面布局可设为空布局,上下各一个面板,其中上面面板可放入文本输入域(jtf.setHorizontalAlignment(JTextField.RIGHT);)这句代码是将文本件右边往左边输出

,下面则是放入button,中间有个分隔线,是用画笔画上去的,好了,界面具体实现

2、功能实现

功能主要是实现了基本的加减乘除,至于加减则是则是从文本输入域中获取要操作的数据,其中要注意一点,加减乘除是特殊字符(减法除外),要通过转译才能,例如加法("\\+"),要使用这样的操作才能运行,否则会在控制台输出错误异常信息java.util.regex.PatternSyntaxException具体都介绍完了,代码如下:

package Simple_Calc;

import java.awt.BasicStroke;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 * 处理计算器界面
 * @author Administrator
 *
 */
public class Calc extends JFrame{
	private void initFrame() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setSize(300, 400);
		this.setTitle("SIMPLE_CALC");
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		// JTextField调用方法时传递
		JTextField jtf = this.topSurface();
		this.bottomSurface(jtf);
		this.setVisible(true);
	}
	private JTextField topSurface(){
		JPanel topPanel = new JPanel();
		topPanel.setBounds(-5,10,300,50);
		JTextField jtf = new JTextField(16);
		// 设置文件右边往左边输出
		jtf.setHorizontalAlignment(JTextField.RIGHT);
		jtf.setForeground(Color.red);
		jtf.setFont(new Font("SansSerif",Font.PLAIN,20));
		jtf.setEditable(false);
		jtf.setText("0");
		topPanel.add(jtf);
		this.add(topPanel);
		return jtf;
	}
	private void bottomSurface(JTextField jtf){
		JPanel bottomPanel = new JPanel();
		bottomPanel.setBounds(8,80,270,250);
		bottomPanel.setLayout(new GridLayout(5,4,1,1));
		
		String[] buttonValue = {"1","2","3","C",
								"4","5","6","CE",
								"7","8","9","←",
								"+","-","0",".",
								"*","/","%","="};
		// 监听器不能重复创建
		CalcListener cl = new CalcListener(jtf);
		for (int i = 0; i < buttonValue.length; i++) {
			JButton button = new JButton(buttonValue[i]);
			button.setBorder(BorderFactory.createRaisedBevelBorder());
			button.setFont(new Font("宋体", 0, 25));
			button.setForeground(Color.blue);
			bottomPanel.add(button);
			button.addActionListener(cl);
		}
		
		this.add(bottomPanel);
	}
	
	
	/**=============================计算器分割线========================================**/
	// 定义自己的画笔,把paint方法的Graphics对象强转为自定义画笔
	private Graphics2D g;
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		this.g = (Graphics2D) g;
		this.g.setStroke(new BasicStroke(3));
		this.g.setColor(new Color(18,18,9));
		this.g.drawLine(0, 95, this.getWidth(), 95);
	}
	public static void main(String[] args) {
		Calc calc = new Calc();
		calc.initFrame();
	}
}


/**
 * @author
 * @vision createtime:2016上午11:42:00
 */
package Simple_Calc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

/**
 * 处理计算器逻辑运算
 * 
 * @author Administrator
 *
 */
public class CalcListener implements ActionListener {
	private JTextField jtf;
	// 创建字符缓冲区域
	String content = "";
	private StringBuffer sb = new StringBuffer();

	public CalcListener(JTextField jtf) {
		this.jtf = jtf;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		switch (command) {
		// case同时写多个条件,数字的基本处理
		case "1":
		case "2":
		case "3":
		case "4":
		case "5":
		case "6":
		case "7":
		case "8":
		case "9":
		case "0":
		case ".":
		case "+":
		case "-":
		case "*":
		case "/":
		case "%":
			sb.append(command);
			jtf.setText(sb.toString().trim());
			break;

		// 清空输入框中内容
		case "C":
		case "CE":
			// 清空StringBuffer
			sb.setLength(0);
			jtf.setText("0");
			break;

		// 退格
		case "←":
			break;

		// 四则运算处理 处理两个运算符问题两个点 TODO
		case "=":
			String content = jtf.getText();
			if (content.contains("+")) {
				String[] string = content.split("\\+");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)+Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("-")) {
				String[] string = content.split("-");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)-Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("*")) {
				String[] string = content.split("\\*");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)*Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("/")) {
				String[] string = content.split("\\/");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)/Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			} else if (content.contains("%")) {
				String[] string = content.split("\\%");
				String frist = string[0];
				String next = string[1];
				double result = Double.valueOf(frist)%Double.valueOf(next);
				jtf.setText(String.valueOf(result));
			}
			break;
		}
	}
}


  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值