JAVA实现计算器GUI

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

import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Calculate extends JFrame implements ActionListener {
	private JLabel leftLabel,rightLabel;
	String input = "";//输入的 式子 
	JTextField textField=new JTextField();//创建显示器
	public Calculate(JLabel leftLabel, JLabel rightLabel) throws HeadlessException {
		super();
		this.leftLabel=leftLabel;
		this.rightLabel=rightLabel;
		
	}

	public Calculate() throws HeadlessException {
		super();
		// TODO Auto-generated constructor stub
	}

	public Calculate(GraphicsConfiguration arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}

	public Calculate(String arg0, GraphicsConfiguration arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public Calculate(String arg0) throws HeadlessException {
		super(arg0);		
		this.setResizable(false);//设置窗体大小不可改变
		this.setBounds(600,200,230,230);
		Font f=new Font("楷体",Font.ITALIC,25);
		this.setFont(f);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口即为退出
		JPanel viewPanel=new JPanel();//设置显示器面板,采用默认的流布局//设置显示器面板,采用默认的流布局
		this.add(viewPanel,BorderLayout.NORTH);//将显示器面板添加到窗体顶部
		
		textField.setEditable(false);//设置显示器不可编辑
		textField.setHorizontalAlignment(SwingConstants.RIGHT);
		textField.setColumns(18);
		viewPanel.add(textField);//将显示器添加到显示器面板中
		JPanel buttonPanel=new JPanel();//创建按钮面板//创建按钮面板
		GridLayout gridLayout=new GridLayout(4,0);//网格布局
		gridLayout.setHgap(10);
		gridLayout.setVgap(10);
		
		buttonPanel.setLayout(gridLayout);//按钮面板采用网格布局
		this.add(buttonPanel,BorderLayout.CENTER);//将按钮面板添加到窗体中间
		String[][] names= {
				{"1","2","3","+"},{"4","5","6","-"},{"7","8","9","x"},{".","0","=","/"}
		};
		JButton[][] buttons=new JButton[4][4];
		for(int row=0;row<names.length;row++) {
			for(int col=0;col<names.length;col++) {
				buttons[row][col]=new JButton(names[row][col]);
				buttons[row][col].addActionListener(this);
				buttonPanel.add(buttons[row][col]);//将按钮添加到按钮面板中
			}
		}
		leftLabel=new JLabel();//创建左侧的占位标签
		leftLabel.setPreferredSize(new Dimension(10,0));//设置标签的宽度
		this.add(leftLabel,BorderLayout.WEST);//将标签添加到窗体的左侧
		rightLabel=new JLabel();//创建左侧的占位标签
		rightLabel.setPreferredSize(new Dimension(10,0));//设置标签的宽度
		this.add(rightLabel,BorderLayout.EAST);//将标签添加到窗体的右侧
		
	}
	private String compute(String input){ 
	String str[]; 		
	str = input.split(" "); 		
	Stack<Double> s = new Stack<Double>(); 		
	double m = Double.parseDouble(str[0]); 	
	s.push(m); 		
	for(int i=1;i<str.length;i++){ 	
	if(i%2==1) { //输入的符号位
	if(str[i].compareTo("+")==0) { 
	double help = Double.parseDouble(str[i+1]); //将+的下一位字符串变成浮点型
	s.push(help);
	 } 
	if(str[i].compareTo("-")==0) { 
	double help = Double.parseDouble(str[i+1]); 
	s.push(-help); 
	} 
	if(str[i].compareTo("x")==0) {
	 double help = Double.parseDouble(str[i+1]); 
	double ans = s.peek();
	//取出栈顶元素
	s.pop();
	//消栈 
	ans*=help; 
	s.push(ans);
	 }
	 if(str[i].compareTo("/")==0) {
	 double help = Double.parseDouble(str[i+1]); 
	double ans = s.peek();
	 s.pop();
	 ans/=help; 
	s.push(ans);
	} } } 
	double ans = 0d;
	 while(!s.isEmpty()) {
	 ans+=s.peek();
	 s.pop();
	 } 
	String result = String.valueOf(ans);
	 return result; 
		} 	
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		int cnt = 0; 
		String actionCommand =arg0.getActionCommand(); 
		if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("x") ||actionCommand.equals("/")) 		
			input +=" "+actionCommand+" ";//设置输入,把输入的样式改成 需要的样子 		
			else if(actionCommand.equals("C")) 			
			input = ""; 		
			else if(actionCommand.equals("="))//当监听到等号时,则处理 input 		
			{ 			
				input+= "="+compute(input); 			
				textField.setText(input); 		
				input=""; 			
				cnt = 1; 		
			} 	
				else //输入的是数字			
					input += actionCommand;//数字为了避免多位数的输入 不需要加空格 		
			if(cnt==0) 	//未计算
				textField.setText(input);
			 	} 	
			
	}


上面是功能类

下面的是主函数

(应用面向对象的方法)//刚学的GUI所以有的地方没有考虑周全 

public class TestCalculate {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Calculate c=new Calculate("计算器");
c.setVisible(true);   
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Famiglistimott

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值