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);
}
}