Java实现一个简单计算器
2016-11-21 21:06
585 查看
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;
public class Caculator extends JFrame implements ActionListener {
static public String[] keys = {"1", "2", "3", "+",
"4", "5", "6", "-",
"7", "8", "9", "*",
".", "0", "log", "/",
"=", "+/-", "Del"};
static public JButton[] jb = new JButton[keys.length];
static public JTextField jt = new JTextField();
static public JPanel jp1 = new JPanel(new GridLayout(1, 1, 2, 10));
static public JPanel jp2 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp3 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp4 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp5 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp6 = new JPanel(new GridLayout(1, 2, 2, 10));
static public JPanel jp7 = new JPanel(new GridLayout(1, 1, 2, 10));
static public JPanel jp8 = new JPanel(new GridLayout(1, 2, 2, 10));
public Caculator() {
this.setLayout(new GridLayout(6, 1, 2, 10));
this.setVisible(true);
this.setBounds(300, 300, 300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i=0; i<jb.length; i++) {
jb[i] = new JButton(keys[i]);
}
jp1.add(jt);
for(int i=0; i<4; i++) {
jp2.add(jb[i]);
}
for(int i=4; i<8; i++) {
jp3.add(jb[i]);
}
for(int i=8; i<12; i++) {
jp4.add(jb[i]);
}
for(int i=12; i<16; i++) {
jp5.add(jb[i]);
}
jp6.add(jp7);jp6.add(jp8);
jp7.add(jb[16]); jp8.add(jb[17]);jp8.add(jb[18]);
this.add(jp1); this.add(jp2); this.add(jp3);
this.add(jp4); this.add(jp5); this.add(jp6);
for(int i=0; i<jb.length; i++) {
jb[i].addActionListener(this);
}
}
public static void main(String[] args) {
Caculator c = new Caculator();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(jb[0])||
e.getSource().equals(jb[1])||
e.getSource().equals(jb[2])||
e.getSource().equals(jb[3])||
e.getSource().equals(jb[4])||
e.getSource().equals(jb[5])||
e.getSource().equals(jb[6])||
e.getSource().equals(jb[7])||
e.getSource().equals(jb[8])||
e.getSource().equals(jb[9])||
e.getSource().equals(jb[11])||
e.getSource().equals(jb[15])||
e.getSource().equals(jb[13])||
e.getSource().equals(jb[10])||
e.getSource().equals(jb[12])||
e.getSource().equals(jb[14])) {
myNumButtonPerformed(e);
}
if(e.getSource().equals(jb[18])) {
myDelPerformed();
}
if(e.getSource().equals(jb[16])) {
jt.setText(String.valueOf(myValuePerformed()));
}
if(e.getSource().equals(jb[17])) {
jt.setText("-");
}
}
public void myNumButtonPerformed(ActionEvent e) {
StringBuffer s = null;
double d;
s = new StringBuffer(jt.getText());
if(s.equals(null)) {
jt.setText((String)e.getActionCommand());
} else {
s = s.append(e.getActionCommand());
jt.setText(s.substring(0));;
}
}
public double myValuePerformed() {
String s = jt.getText();
char sop = 0;
double value = 0;
double d1 = 0;
double d2 = 0;
for(int i=1; i<s.length(); i++) {
if((s.charAt(i) == '+')||
(s.charAt(i) == '-')||
(s.charAt(i) == '*')||
(s.charAt(i) == '/')) {
sop = s.charAt(i);
}
}
if(sop == '+') {
String[] str = s.split("\\+");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1+d2;
}
if(sop == '-') {
String[] str = s.substring(1).split("-");
StringBuffer sb = new StringBuffer(s.substring(0, 1));
sb = sb.append(str[0]);
d1 = Double.parseDouble(sb.substring(0, sb.length()));
d2 = Double.parseDouble(str[1]);
value = d1-d2;
}
if(sop == '*') {
String[] str = s.split("\\*");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1*d2;
}
if(sop == '/') {
String[] str = s.split("/");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1/d2;
}
if(s.substring(0, 3).equalsIgnoreCase("log")) {
String str = s.substring(3);
double d = Double.parseDouble(str);
value = Math.log(d);
}
return value;
}
public void myDelPerformed() {
String s = null;
s = jt.getText();
try {
jt.setText(s.substring(0, s.length()-1));
} catch(StringIndexOutOfBoundsException e) {
jt.setText("0");
}
}
}
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;
public class Caculator extends JFrame implements ActionListener {
static public String[] keys = {"1", "2", "3", "+",
"4", "5", "6", "-",
"7", "8", "9", "*",
".", "0", "log", "/",
"=", "+/-", "Del"};
static public JButton[] jb = new JButton[keys.length];
static public JTextField jt = new JTextField();
static public JPanel jp1 = new JPanel(new GridLayout(1, 1, 2, 10));
static public JPanel jp2 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp3 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp4 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp5 = new JPanel(new GridLayout(1, 4, 2, 10));
static public JPanel jp6 = new JPanel(new GridLayout(1, 2, 2, 10));
static public JPanel jp7 = new JPanel(new GridLayout(1, 1, 2, 10));
static public JPanel jp8 = new JPanel(new GridLayout(1, 2, 2, 10));
public Caculator() {
this.setLayout(new GridLayout(6, 1, 2, 10));
this.setVisible(true);
this.setBounds(300, 300, 300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i=0; i<jb.length; i++) {
jb[i] = new JButton(keys[i]);
}
jp1.add(jt);
for(int i=0; i<4; i++) {
jp2.add(jb[i]);
}
for(int i=4; i<8; i++) {
jp3.add(jb[i]);
}
for(int i=8; i<12; i++) {
jp4.add(jb[i]);
}
for(int i=12; i<16; i++) {
jp5.add(jb[i]);
}
jp6.add(jp7);jp6.add(jp8);
jp7.add(jb[16]); jp8.add(jb[17]);jp8.add(jb[18]);
this.add(jp1); this.add(jp2); this.add(jp3);
this.add(jp4); this.add(jp5); this.add(jp6);
for(int i=0; i<jb.length; i++) {
jb[i].addActionListener(this);
}
}
public static void main(String[] args) {
Caculator c = new Caculator();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(jb[0])||
e.getSource().equals(jb[1])||
e.getSource().equals(jb[2])||
e.getSource().equals(jb[3])||
e.getSource().equals(jb[4])||
e.getSource().equals(jb[5])||
e.getSource().equals(jb[6])||
e.getSource().equals(jb[7])||
e.getSource().equals(jb[8])||
e.getSource().equals(jb[9])||
e.getSource().equals(jb[11])||
e.getSource().equals(jb[15])||
e.getSource().equals(jb[13])||
e.getSource().equals(jb[10])||
e.getSource().equals(jb[12])||
e.getSource().equals(jb[14])) {
myNumButtonPerformed(e);
}
if(e.getSource().equals(jb[18])) {
myDelPerformed();
}
if(e.getSource().equals(jb[16])) {
jt.setText(String.valueOf(myValuePerformed()));
}
if(e.getSource().equals(jb[17])) {
jt.setText("-");
}
}
public void myNumButtonPerformed(ActionEvent e) {
StringBuffer s = null;
double d;
s = new StringBuffer(jt.getText());
if(s.equals(null)) {
jt.setText((String)e.getActionCommand());
} else {
s = s.append(e.getActionCommand());
jt.setText(s.substring(0));;
}
}
public double myValuePerformed() {
String s = jt.getText();
char sop = 0;
double value = 0;
double d1 = 0;
double d2 = 0;
for(int i=1; i<s.length(); i++) {
if((s.charAt(i) == '+')||
(s.charAt(i) == '-')||
(s.charAt(i) == '*')||
(s.charAt(i) == '/')) {
sop = s.charAt(i);
}
}
if(sop == '+') {
String[] str = s.split("\\+");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1+d2;
}
if(sop == '-') {
String[] str = s.substring(1).split("-");
StringBuffer sb = new StringBuffer(s.substring(0, 1));
sb = sb.append(str[0]);
d1 = Double.parseDouble(sb.substring(0, sb.length()));
d2 = Double.parseDouble(str[1]);
value = d1-d2;
}
if(sop == '*') {
String[] str = s.split("\\*");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1*d2;
}
if(sop == '/') {
String[] str = s.split("/");
d1 = Double.parseDouble(str[0]);
d2 = Double.parseDouble(str[1]);
value = d1/d2;
}
if(s.substring(0, 3).equalsIgnoreCase("log")) {
String str = s.substring(3);
double d = Double.parseDouble(str);
value = Math.log(d);
}
return value;
}
public void myDelPerformed() {
String s = null;
s = jt.getText();
try {
jt.setText(s.substring(0, s.length()-1));
} catch(StringIndexOutOfBoundsException e) {
jt.setText("0");
}
}
}
相关文章推荐
- 基于Java实现的一个简单计算器
- 一个实现排列和组合的JavaBean
- 用JAVA实现一个分页类
- 一个用Dijkstra算法实现的路由算法的java程序——6 STDijkstraAdv类
- 推荐一个关于AOP的AOP@WORK的专栏作者的AOP专题系列文章,主要关于Java下的AOP实现,毕竟Java下的AOP实现确实要比.Net下成熟得多,共12篇
- 推荐一个关于AOP的AOP@WORK的专栏作者的AOP专题系列文章,主要关于Java下的AOP实现,毕竟Java下的AOP实现确实要比.Net下成熟得多,共12篇
- 今天的问题:一个简单的例子,请帮我解开“接口实现Java‘隐藏实现细目’”的迷惑。
- JSP+JavaBean+Servlet实现一个留言板
- 一个用Dijkstra算法实现的路由算法的java程序——4 MapCanvas类
- 一个实现MD5的简洁的java类
- Java实现压缩一个目录下所有图片文件
- Java.NET --一个基于Java的Microsoft.NET框架的实现
- 一个简单的用JAVA实现的屏幕抓图(源代码)
- java和jdom实现的一个地址簿源码
- 一个用Dijkstra算法实现的路由算法的java程序——5 GraphShow类
- 一个用Dijkstra算法实现的路由算法的java程序——1 GraphAdjList类
- 一个实现MD5的简洁的java类 Jagie 原创
- 一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)
- 一个实现MD5的简洁的java类
- 一个用Dijkstra算法实现的路由算法的java程序——9 图信息文件示例