本人初学者,自己的想法加上网上的想法做了个简单的计算器。
菜-勿喷!
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
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;
import java.awt.FlowLayout;
public class Calculator extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private double value;//计算的结果
private String operator;//运算符
JTextField text;//文本框
JPanel p1,p2;//面板
Container c;//容器
JButton clear;// 清除按钮
public Calculator(){
text=new JTextField(20);
//设置文本框不可编辑
text.setEditable(false);
//设置文本框内容从右边起
text.setHorizontalAlignment (JTextField.RIGHT);
clear =new JButton("清除");
//清除按钮的单击事件
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text.setText("");
}
});
p2=new JPanel();
p2.setLayout(new FlowLayout() );
p2.add(text);
p2.add(clear);
p1=new JPanel();
p1.setLayout(new GridLayout(4,4));
p1.add(getButton("7"));
p1.add(getButton("8"));
p1.add(getButton("9"));
p1.add(getButton("+"));
p1.add(getButton("4"));
p1.add(getButton("5"));
p1.add(getButton("6"));
p1.add(getButton("-"));
p1.add(getButton("1"));
p1.add(getButton("2"));
p1.add(getButton("3"));
p1.add(getButton("*"));
p1.add(getButton("0"));
p1.add(getButton("."));
p1.add(getButton("="));
p1.add(getButton("/"));
c=this.getContentPane();
c.setLayout(new BorderLayout());
c.add(p2,BorderLayout.NORTH);
c.add(p1,BorderLayout.SOUTH);
init();
//5、是否显示
this.setVisible(true);
}
/**
* 初始化窗体的基本信息
*/
public void init(){
//1、标题
this.setTitle("计算器");
//2、大小
//this.setSize(350,200);
this.pack();//自动适应大小
//3、关闭放大功能
this.setResizable(false);
//4、位置
this.setLocationRelativeTo(null);
//5、是否显示
//this.setVisible(true);
//6、关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 此方法用于生成多个按钮,并添加单击事件
*/
private final JButton getButton(String key){
JButton jb=new JButton(key);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String content=jb.getText();
action(content);
}
});
return jb;
}
/**
* 此方法逻辑处理
*/
private void action(String content){
switch(content){
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
content=text.getText()+content;
text.setText(content);
break;
}
switch(content){
case "+":
operator="+";//记录一下运算符
content=text.getText()+content;
text.setText(content);
break;
case "-":
operator="-";//记录一下运算符
content=text.getText()+content;
text.setText(content);
break;
case "*":
operator="*";//记录一下运算符
content=text.getText()+content;
text.setText(content);
break;
case "/":
operator="/";//记录一下运算符
content=text.getText()+content;
text.setText(content);
break;
case "=":
content=text.getText();
String[] str;
switch(operator){
case "+":
//由于'+'号不能直接拆分 转义处理
str=content.split("\\+");
value=Double.valueOf(str[0])+Double.valueOf(str[1]);
text.setText(String.valueOf(value));
break;
case "-":
//由于'+'号不能直接拆分 转义处理
str=content.split("\\-");
value=Double.valueOf(str[0])-Double.valueOf(str[1]);
text.setText(String.valueOf(value));
break;
case "*":
//由于'+'号不能直接拆分 转义处理
str=content.split("\\*");
value=Double.valueOf(str[0])* Double.valueOf(str[1]);
text.setText(String.valueOf(value));
break;
case "/":
//由于'+'号不能直接拆分 转义处理
str=content.split("\\/");
value=Double.valueOf(str[0])/Double.valueOf(str[1]);
text.setText(String.valueOf(value));
break;
}
break;
}
}
public static void main(String[] args) {
new Calculator();
}
}