求Java编的简单计算器源代码
发布网友
发布时间:2024-01-18 04:48
我来回答
共3个回答
热心网友
时间:2024-07-25 17:27
http://hi.baidu.com/zpsailor/blog/item/6918f41696e1d9084b90a737.html
到这里去下哈
热心网友
时间:2024-07-25 17:23
package edu.hpu.yyf;
import java.awt.*;
import java.awt.event.*;
public class MyCalculator {
private static double d1 = 0.0;
private static double d2 = 0.0;
private static String s1 = new String("0");
private static String s2 = new String("0");
private static char c1 ='0';
private static void judgec1(TextField text){
switch(c1){
case '+':s2 = String.valueOf(d1+d2);d1=d1+d2;d2=0;text.setText(s2);s2="0";break;
case '-':s2 = String.valueOf(d1-d2);d1=d1-d2;d2=0;text.setText(s2);s2="0";break;
case '*':s2 = String.valueOf(d1*d2);d1=d1*d2;d2=0;text.setText(s2);s2="0";break;
case '/':if(d2==0) {text.setText("0");d1=0;break;}s2 = String.valueOf(d1/d2);d1=d1/d2;d2=0;text.setText(s2);s2="0";break;}
}
public static void main(String [] args){
Frame cclt = new Frame("我的计算器");
cclt.setBounds(300,150,300,265);
cclt.setLayout(null);
final TextField text = new TextField();
text.setBounds(10, 30, 280, 35);
text.setText("0");
text.setEditable(false);
Font font = new Font("", 5, 25);
text.setFont(font);
cclt.add(text);
Panel panel = new Panel();
Font font_1 = new Font("", 5, 20);
panel.setFont(font_1);
panel.setBounds(5, 67, 290, 193);
panel.setBackground(Color.GREEN);
panel.setLayout(new GridLayout(5,4,5,5));
cclt.add(panel);
Button space = new Button();
panel.add(space);
Button Backspace = new Button("Backspace");
Font font_2 = new Font("", 0, 14);
Backspace.setFont(font_2);
panel.add(Backspace);
Button CE = new Button("CE");
panel.add(CE);
Button C = new Button("C");
panel.add(C);
Button seven = new Button("7");
panel.add(seven);
Button eight = new Button("8");
panel.add(eight);
Button nine = new Button("9");
panel.add(nine);
Button but = new Button("/");
panel.add(but);
Button four = new Button("4");
panel.add(four);
Button five = new Button("5");
panel.add(five);
Button six = new Button("6");
panel.add(six);
Button ride = new Button("*");
panel.add(ride);
Button one = new Button("1");
panel.add(one);
Button two = new Button("2");
panel.add(two);
Button three = new Button("3");
panel.add(three);
Button substract = new Button("-");
panel.add(substract);
Button zero = new Button("0");
panel.add(zero);
Button space_1 = new Button(" ");
panel.add(space_1);
Button equal = new Button("=");
panel.add(equal);
Button add = new Button("+");
panel.add(add);
CE.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
d1=0;
d2=0;
s1="0";
s2="0";
c1 ='0';
text.setText("0");
}
});
seven.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="7";
else {text.setText(s1+"7");
s1+="7";}
text.setText(s1);
}
});
eight.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="8";
else {text.setText(s1+"8");
s1+="8";}
text.setText(s1);
}
});
nine.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="9";
else {text.setText(s1+"9");
s1+="9";}
text.setText(s1);
}
});
four.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="4";
else {text.setText(s1+"4");
s1+="4";}
text.setText(s1);
}
});
five.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="5";
else {text.setText(s1+"5");
s1+="5";}
text.setText(s1);
}
});
six.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="6";
else {text.setText(s1+"6");
s1+="6";}
text.setText(s1);
}
});
one.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="1";
else {text.setText(s1+"1");
s1+="1";}
text.setText(s1);
}
});
two.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="2";
else {text.setText(s1+"2");
s1+="2";}
text.setText(s1);
}
});
three.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="3";
else {text.setText(s1+"3");
s1+="3";}
text.setText(s1);
}
});
zero.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(!s1.equals("0")) s1+="0";
text.setText(s1);
}
});
but.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '/';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '/';}
s1="0";
}
});
substract.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '-';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '-';}
s1="0";
}
});
ride.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '*';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '*';}
s1="0";
}
});
add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') {d1=Double.parseDouble(s1);if(d1!=0){c1 = '+';}}
else {d2=Double.parseDouble(s1);
judgec1(text);
c1 = '+';}
s1="0";
}
});
/* Backspace.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.length()!=0){
if(c1=='0') {text.setText(s1.substring(0, s1.length()-1));s1=s1.substring(0, s1.length()-1);}
else if (c1!='0'&&!s2.equals("0")) {text.setText(s2.substring(0, s1.length()-1));s2=s2.substring(0, s2.length()-1);}
}}
});*/
equal.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(c1=='0') d1=Double.parseDouble(s1);
else {d2=Double.parseDouble(s1);
judgec1(text);}
s1="0";
c1='0';
}
});
cclt.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
((Frame) e.getComponent()).dispose();
}
});
cclt.setResizable(false);
cclt.setVisible(true);
}
}
我们老师写的哦
热心网友
时间:2024-07-25 17:22
package util;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame {
private String num = "0";
private String operator = "+";
private String result = "0";
private final JTextField textField;
/*
* public static void main(String[] args) { // TODO Auto-generated method
* stub Calculator frame=new Calculator(); }
*/
public Calculator() {
super();
setTitle("计算器");
setResizable(false);
setBounds(100, 100, 208, 242);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
final JPanel viewPanel = new JPanel();
getContentPane().add(viewPanel, BorderLayout.NORTH);
textField = new JTextField();
textField.setText(num);
textField.setColumns(18);
textField.setEditable(false);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBackground(Color.white);
textField.setFont(new Font("Courier New", Font.BOLD, 18));
viewPanel.add(textField);
getContentPane().add(viewPanel, BorderLayout.NORTH);
final JPanel clearBottonPanel = new JPanel();
getContentPane().add(clearBottonPanel, BorderLayout.CENTER);
String[] clearButtonNames = { "<-", "CE", "C" };
for (int i = 0; i < clearButtonNames.length; i++) {
final JButton button = new JButton(clearButtonNames[i]);
button.setFont(new Font("Courier New", Font.PLAIN, 14));
button.setForeground(Color.red);
button.addActionListener(new ClearButtonActionListener());
clearBottonPanel.add(button);
}
final JPanel inpuptButtonPanel = new JPanel();
final GridLayout girdLayout = new GridLayout(4, 0);
girdLayout.setVgap(10);
girdLayout.setHgap(10);
inpuptButtonPanel.setLayout(girdLayout);
getContentPane().add(inpuptButtonPanel, BorderLayout.SOUTH);
String[][] inputButtonNames = { { "1", "2", "3", "+", },
{ "4", "5", "6", "-", }, { "7", "8", "9", "*", },
{ ".", "0", "=", "/", } };
for (int row = 0; row < inputButtonNames.length; row++) {
for (int col = 0; col < inputButtonNames.length; col++) {
final JButton button = new JButton(inputButtonNames[row][col]);
button.setName(row + "" + col);
button.setFont(new Font("Courier New", Font.BOLD, 15));
if (col == 3) {
button.setFont(new Font("Courier New", Font.PLAIN, 15));
button.setForeground(Color.red);
}
button.addActionListener(new InputButtonActionListener());
inpuptButtonPanel.add(button);
}
}
// setVisible(true);
}
class InputButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String name = button.getName();
Integer row = Integer.valueOf(name.substring(0, 1));
Integer col = Integer.valueOf(name.substring(1, 2));
if (col == 3) {
count();
textField.setText(result);
operator = button.getText();
} else if (row == 3) {
if (col == 0) {
if (num.indexOf(".") < 0) {
num = num + button.getText();
textField.setText(num);
}
} else if (col == 1) {
if (num.indexOf(".") > 0) {
num = num + button.getText();
textField.setText(num);
} else {
if (!num.substring(0, 1).equals("0")) {
num = num + button.getText();
textField.setText(num);
}
}
} else {
count();
textField.setText(result);
operator = "+";
}
} else {
if (num.equals("0"))
num = button.getText();
else
num = num + button.getText();
textField.setText(num);
}
}
private void count() {
float n = Float.valueOf(num);
float r = Float.valueOf(result);
if (r == 0) {
result = num;
num = "0";
} else {
if (operator.equals("+")) {
r = r + n;
} else if (operator.equals("-")) {
r = r - n;
} else if (operator.equals("*")) {
r = r * n;
} else {
r = r / n;
}
num = "0";
result = r + "";
}
}
}
public class ClearButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String text = button.getText().trim();
if (text.equals("<-")) {
int length = num.length();
if (length == 1)
num = "0";
else
num = num.substring(0, length - 1);
} else if (text.equals("CE")) {
num = "0";
} else {
num = "0";
operator = "+";
result = "0";
}
textField.setText(num);
}
}
}
谢谢采纳!!