因为在java入门就先做一下简单的来练一下手,这边我的图形界面使用windowbuilder制作的,这个可以托框,布局也比用手打方便很多。这个感觉图形界面不是主要的难点,重点还是在于对于四则运算的方法。不多说放代码,里面有注释。
图形界面代码
/* 图形界面代码,放在了一个Caculator类中,用window builder可以做出自己想要的布局*/
/*Caculator.java*/
package caculator;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.awt.Font;
public class Caculator {
private JFrame frame;
private JTextField text1;
private JTextField text2;
public String getText1() {
return text1.getText();
}
public String getText2() {
return text2.getText();
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Caculator window = new Caculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Caculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 746, 607);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text1 = new JTextField();
text1.setFont(new Font("宋体", Font.PLAIN, 26));
text1.setColumns(10);
text2 = new JTextField();
text2.setFont(new Font("宋体", Font.PLAIN, 26));
text2.setColumns(10);
JButton button_left = new JButton("(");
button_left.setFont(new Font("宋体", Font.PLAIN, 26));
button_left.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="(";
text1.setText(text1.getText()+s);
}
});
JButton button_right = new JButton(")");
button_right.setFont(new Font("宋体", Font.PLAIN, 26));
button_right.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s=")";
text1.setText(text1.getText()+s);
}
});
JButton button_result = new JButton("=");
button_result.setFont(new Font("宋体", Font.PLAIN, 26));
button_result.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Result s=new Result();
s.str=text1.getText();
text2.setText(String.valueOf(s.result()));
}
});
JButton button7 = new JButton("7");
button7.setFont(new Font("宋体", Font.PLAIN, 26));
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="7";
text1.setText(text1.getText()+s);
}
});
JButton button8 = new JButton("8");
button8.setFont(new Font("宋体", Font.PLAIN, 26));
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="8";
text1.setText(text1.getText()+s);
}
});
JButton button_point = new JButton(".");
button_point.setFont(new Font("宋体", Font.PLAIN, 26));
button_point.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s=".";
text1.setText(text1.getText()+s);
}
});
JButton button1 = new JButton("1");
button1.setFont(new Font("宋体", Font.PLAIN, 26));
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="1";
text1.setText(text1.getText()+s);
}
});
JButton button4 = new JButton("4");
button4.setFont(new Font("宋体", Font.PLAIN, 26));
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="4";
text1.setText(text1.getText()+s);
}
});
JButton button5 = new JButton("5");
button5.setFont(new Font("宋体", Font.PLAIN, 26));
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="5";
text1.setText(text1.getText()+s);
}
});
JButton button2 = new JButton("2");
button2.setFont(new Font("宋体", Font.PLAIN, 26));
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s="2";
text1.setText(text1.getText()+s);
}
});
JButton button0 = new JButton("0");
button0.setFont(new Font("宋体", Font.PLAIN, 26));
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="0";
text1.setText(text1.getText()+s);
}
});
JButton button9 = new JButton("9");
button9.setFont(new Font("宋体", Font.PLAIN, 26));
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="9";
text1.setText(text1.getText()+s);
}
});
JButton button6 = new JButton("6");
button6.setFont(new Font("宋体", Font.PLAIN, 26));
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="6";
text1.setText(text1.getText()+s);
}
});
JButton button3 = new JButton("3");
button3.setFont(new Font("宋体", Font.PLAIN, 26));
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="3";
text1.setText(text1.getText()+s);
}
});
JButton button_add = new JButton("+");
button_add.setFont(new Font("宋体", Font.PLAIN, 26));
button_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="+";
text1.setText(text1.getText()+s);
}
});
JButton button_sub = new JButton("-");
button_sub.setFont(new Font("宋体", Font.PLAIN, 26));
button_sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="-";
text1.setText(text1.getText()+s);
}
});
JButton button_mul = new JButton("*");
button_mul.setFont(new Font("宋体", Font.PLAIN, 26));
button_mul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="*";
text1.setText(text1.getText()+s);
}
});
JButton button_div = new JButton("/");
button_div.setFont(new Font("宋体", Font.PLAIN, 26));
button_div.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s="/";
text1.setText(text1.getText()+s);
}
});
JButton btnC = new JButton("C");
btnC.setFont(new Font("宋体", Font.PLAIN, 26));
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
text1.setText(null);
}
});
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(text1, GroupLayout.DEFAULT_SIZE, 724, Short.MAX_VALUE)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(button_left, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
.addGap(119)
.addComponent(button_right, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
.addGap(230)
.addComponent(text2, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button7, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button4, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button1, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button_point, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE))
.addGap(38)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button8, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button5, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button2, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button0, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE))
.addGap(33)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button9, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button6, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button3, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(btnC, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE))
.addGap(44)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button_div, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
.addComponent(button_result, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button_sub, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button_add, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
.addComponent(button_mul, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE))
.addContainerGap(157, Short.MAX_VALUE))))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(text1, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(text2, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button_right, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
.addComponent(button_left, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE))))
.addGap(34)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button7, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addComponent(button8, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addComponent(button9, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addComponent(button_add, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
.addGap(21)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button_sub, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button_mul, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button4, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button1, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_result, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
.addComponent(button_point, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button5, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button2, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button6, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button3, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button0, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addComponent(btnC, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
.addComponent(button_div, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
.addGap(23)))))
);
frame.getContentPane().setLayout(groupLayout);
}
}
接下来是计算,放在了一个Result类中,代码都有详细注释,可参考这篇文章的文字解释https://blog.csdn.net/zzucsliang/article/details/25282249
/*Result.java*/
package caculator;
import java.util.Stack;
public class Result {
String str;
Stack<String> s1=new Stack<String>();
Stack<String> s2=new Stack<String>();
public double result() {
//让每个字符先进栈
for(int i=0;i<str.length();i++) {
char c=str.charAt(i);
//数字则进栈
if(isNumber(c)==true) {
//数字前也是数字是就要与前面的数字一起存入
if(i!=0&&isNumber(str.charAt(i-1))==true)
{
s1.pop();
s1.push(String.valueOf(str.charAt(i-1))+String.valueOf(c));
}
else s1.push(String.valueOf(c));
}
//不是数字
if(isNumber(c)==false) {
// System.out.println(111);
//字符栈为空,则直接进栈
if(s2.empty()) {
s2.push(String.valueOf(c));
}
else {
//左括号直接进栈
if(c=='(')
s2.push(String.valueOf(c));
//右括号则进行计算
if(c==')') {
double a1=Double.parseDouble(s1.pop());
double a2=Double.parseDouble(s1.pop());
String s=s2.pop();
s1.push(String.valueOf(Caculate_1(a2, a1, s)));
s2.pop();
}
//如果是加减号
if(c=='+'||c=='-') {
//栈顶符号优先级高则先进行优先级高的
while(s2.peek().equals("*")||s2.peek().equals("/")||s2.peek().equals("-")||s2.peek().equals("+")) {
//System.out.println(111);
double a1=Double.parseDouble(s1.pop());
// System.out.println(a1);
double a2=Double.parseDouble(s1.pop());
// System.out.println(a2);
String s=s2.pop();
s1.push(String.valueOf(Caculate_1(a2, a1, s)));
if(s2.empty())
break;
}
s2.push(String.valueOf(c));
}
//乘除则直接进栈
if(c=='*'||c=='/') {
s2.push(String.valueOf(c));
}
}
}
}
//按字符栈顺序执行
while(!s2.empty()) {
double a1=Double.parseDouble(s1.pop());
double a2=Double.parseDouble(s1.pop());
String s=s2.pop();
//double q=Caculate_1(a2, a1, s);
s1.push(String.valueOf(Caculate_1(a2, a1, s)));
}
return Double.parseDouble(s1.pop());
}
//判断是不是数字
public boolean isNumber(char c) {
if(!Character.isDigit(c))
return false;
else
return true;
}
//每一步的计算
public double Caculate_1(double a, double b,String c) {
if(c.equals("*"))
return a*b;
else if(c.equals("/"))
return a/b;
else if(c.equals("+"))
return a+b;
else if(c.equals("-"))
return a-b;
else
return 0;
}
}
代码效果:
这个项目做图形界面不是关键,关键是对输入进去的式子进行运算,当然这代码还有好多可以优化的,比如添加小数,或者是输入不规范格式时会出现报错等。