首页 > 代码库 > JAVA编写的一个简单的计算器
JAVA编写的一个简单的计算器
做的项目里面有一个是计算器的。做得比较简单,只是普通的加减乘除等于小数点负号删除退格等,其他功能如科学计算、进制转换,其实难度也不大,就是懒得去弄那些。整个程序可以说是自己独立完成的吧,因为没什么花哨,无非就是运算时条件多弄点,让程序不出BUG。
周末在家设置了一下字体和输入框,搞大了点,看着也舒服了点。当然,这界面很普通,没啥特色。整个代码也没啥特色,刚开始写了些,其他的就复制,回来才发现代码太多了,不过懒得改,所以整个代码也没用到方法。这完全是一大败笔,所以,没打算用这个当毕业答辩。嘿嘿,有一点还好,就是暂时没发现BUG。
不说了,贴上去代码:
package org.ckw;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
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 Test2 {
static void f(){
}
static String s="";
static String s1="";
static double d2=0;
static double sum=0;
static int a=0;
static int a1=0;
public static void main(String[] args) {
JFrame jf=new JFrame("计算器");
final JTextField jtf=new JTextField(30);//文本框
jtf.setHorizontalAlignment(jtf.RIGHT);//右对齐
jtf.setFont(new Font(null, Font.PLAIN, 30));
jtf.setText("0");
JPanel jp0=new JPanel();
jp0.setLayout(new BorderLayout());
jp0.add(jtf);
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(4,4,3,3));
JPanel jp1=new JPanel();
jp1.setLayout(new GridLayout(1,2,3,3));
JButton b1=new JButton("1");
b1.setFont(new Font(null, Font.PLAIN, 15));
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"1");
s=s+"1";
}
});
JButton b2=new JButton(s+"2");
b2.setFont(new Font(null, Font.PLAIN, 15));
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"2");
s=s+"2";
}
});
JButton b3=new JButton("3");
b3.setFont(new Font(null, Font.PLAIN, 15));
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"3");
s=s+"3";
}
});
JButton b4=new JButton("4");
b4.setFont(new Font(null, Font.PLAIN, 15));
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"4");
s=s+"4";
}
});
JButton b5=new JButton("5");
b5.setFont(new Font(null, Font.PLAIN, 15));
b5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"5");
s=s+"5";
}
});
JButton b6=new JButton("6");
b6.setFont(new Font(null, Font.PLAIN, 15));
b6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"6");
s=s+"6";
}
});
JButton b7=new JButton("7");
b7.setFont(new Font(null, Font.PLAIN, 15));
b7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"7");
s=s+"7";
}
});
JButton b8=new JButton("8");
b8.setFont(new Font(null, Font.PLAIN, 15));
b8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"8");
s=s+"8";
}
});
JButton b9=new JButton("9");
b9.setFont(new Font(null, Font.PLAIN, 15));
b9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"9");
s=s+"9";
}
});
JButton b0=new JButton("0");
b0.setFont(new Font(null, Font.PLAIN, 16));
b0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText(s+"0");
s=s+"0";
}
});
JButton bdeng=new JButton("=");
bdeng.setBackground(Color.ORANGE);
bdeng.setFont(new Font(null, Font.PLAIN, 24));
bdeng.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(s.length()>0^(s.length()==1&&s.indexOf(‘-‘) == 0)){
if(a==1){
sum+=Double.parseDouble(jtf.getText());;
}else if(a==2){
sum-=Double.parseDouble(jtf.getText());;
}else if(a==3){
sum*=Double.parseDouble(jtf.getText());;
}else if(a==4){
sum/=Double.parseDouble(jtf.getText());;
}else{
sum=Double.parseDouble(jtf.getText());
}
jtf.setText(""+sum);
s="";
a=0;
a1=1;
}
}
});
JButton bjia=new JButton("+");
bjia.setFont(new Font(null, Font.PLAIN, 16));
bjia.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(s.length()>0||a1==1){
if(a==1){
sum+=Double.parseDouble(jtf.getText());
}else if(a==2){
sum-=Double.parseDouble(jtf.getText());;
}else if(a==3){
sum*=Double.parseDouble(jtf.getText());;
}else if(a==4){
sum/=Double.parseDouble(jtf.getText());;
}else{
sum=Double.parseDouble(jtf.getText());
}
jtf.setText(""+sum);
s="";
a=1;
a1=0;
}
}
});
JButton bjian=new JButton("-");
bjian.setFont(new Font(null, Font.PLAIN, 16));
bjian.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(s.length()>1||(s.length()>0&&jtf.getText().indexOf("-")!=0||a1==1)){
if(a==1){
sum+=Double.parseDouble(jtf.getText());
}else if(a==2){
sum-=Double.parseDouble(jtf.getText());
}else if(a==3){
sum*=Double.parseDouble(jtf.getText());
}else if(a==4){
sum/=Double.parseDouble(jtf.getText());
}else{
sum=Double.parseDouble(jtf.getText());
}
jtf.setText(""+sum);
s="";
a=2;
a1=0;
}
if(jtf.getText().length()==1&&jtf.getText().indexOf("0")==0){
if(jtf.getText().indexOf("-")==0){
System.out.println(123);
}else{
jtf.setText("-");
s="-";
}
}
}
});
JButton bceng=new JButton("x");
bceng.setFont(new Font(null, Font.PLAIN, 16));
bceng.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(s.length()>0||a1==1){
if(a==1){
sum+=Double.parseDouble(jtf.getText());
}else if(a==2){
sum-=Double.parseDouble(jtf.getText());;
}else if(a==3){
sum*=Double.parseDouble(jtf.getText());;
}else if(a==4){
sum/=Double.parseDouble(jtf.getText());;
}else{
sum=Double.parseDouble(jtf.getText());
}
jtf.setText(""+sum);
s="";
a=3;
a1=0;
}
}
});
JButton bchu=new JButton("÷");
bchu.setFont(new Font(null, Font.PLAIN, 16));
bchu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(s.length()>0||a1==1){
if(a==1){
sum+=Double.parseDouble(jtf.getText());
}else if(a==2){
sum-=Double.parseDouble(jtf.getText());;
}else if(a==3){
sum*=Double.parseDouble(jtf.getText());;
}else if(a==4){
sum/=Double.parseDouble(jtf.getText());;
}else{
sum=Double.parseDouble(jtf.getText());
}
jtf.setText(""+sum);
s="";
a=4;
a1=0;
}
}
});
JButton bdian=new JButton(".");
bdian.setFont(new Font(null, Font.PLAIN, 16));
bdian.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(s.indexOf(‘.‘) < 0){
jtf.setText(s+".");
s=s+".";
}
}
});
JButton bsan=new JButton("C");
bsan.setForeground(Color.RED);
bsan.setFont(new Font(null, Font.PLAIN, 16));
bsan.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jtf.setText("0");
sum=0;
s="";
}
});
JButton btui=new JButton("DEL");
btui.setForeground(Color.ORANGE);
btui.setFont(new Font(null, Font.PLAIN, 14));
btui.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (jtf.getText().length()>0) {
jtf.setText(jtf.getText().substring(0, jtf.getText().length()-1));
s=jtf.getText();
}
if (jtf.getText().length()==0) {
jtf.setText("0");
}
}
});
jp.add(bsan);jp.add(btui);jp.add(bchu);jp.add(bceng);
jp.add(b7);jp.add(b8);jp.add(b9);jp.add(bjia);
jp.add(b4);jp.add(b5);jp.add(b6);jp.add(bjian);
jp.add(b1);jp.add(b2);jp.add(b3);jp.add(bdian);
jp1.add(b0);jp1.add(bdeng);
jf.add(BorderLayout.NORTH, jp0);
jf.add(BorderLayout.CENTER, jp);
jf.add(BorderLayout.SOUTH, jp1);
jf.setVisible(true);
jf.setBounds(200, 200, 400, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JAVA编写的一个简单的计算器