帮忙编一个java小程序!

2024-12-20 04:53:41
推荐回答(2个)
回答1:

import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;

class Calculator {

private Frame frame;

private Panel panel[] = new Panel[6];

private Button botton[] = new Button[25];

private TextField textField;

private final String string[] = { "1", "2", "3", "clear", "exit", "4", "5",
"6", "+", "-", "7", "8", "9", "*", "/", "0", ".", "sqr", "%",
"+/-", "sin", "cos", "tan", "PI", "=" };

private String str1, str2, optype;

private final Label label = new Label(
"Contact me--QQ:120355282 , mail:fsolsh@gmail.com");

private final String STR1 = "Please enter your data!";

private final String STR2 = "Arithmetic data can't be '0'!";

private double result = 0;

private void go() {

frame = new Frame("Welcome ! Author:Fsol-sh JSface");
frame.setResizable(false);
frame.setLayout(new GridLayout(7, 1, 5, 5));

for (int i = 0; i < botton.length; i++) {
botton[i] = new Button(string[i]);
botton[i].setForeground(Color.BLUE);
botton[i].setFont(new Font("", Font.BOLD, 12));
}

textField = new TextField(40);
textField.setFont(new Font("", Font.BOLD, 12));
textField.setEditable(false);

label.setAlignment(1);
label.setFont(new Font("", Font.BOLD, 12));
label.setForeground(Color.BLUE);

for (int i = 0; i < panel.length; i++) {
panel[i] = new Panel();
frame.add(panel[i]);
}

frame.add(label);

panel[0].add(textField);

for (int i = 1; i < 6; i++) {
panel[i].setLayout(new GridLayout(1, 5, 5, 5));
for (int j = (i - 1) * 5; j < i * 5; j++) {
panel[i].add(botton[j]);
}
}

for (int i = 0; i < 3; i++) {
for (int j = (i) * 5; j < i * 5 + 3; j++) {
buttonSet(botton[j]);
}
}

botton[3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});

botton[4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

operateTypeSet(botton[8]);
operateTypeSet(botton[9]);
operateTypeSet(botton[13]);
operateTypeSet(botton[14]);
operateTypeSet(botton[18]);

botton[15].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (check(textField.getText())
|| textField.getText().equals("0")) {
textField.setText("0");
} else
textField.setText(textField.getText() + "0");
}
});

botton[16].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (check(textField.getText())) {
textField.setText(STR1);
} else if (textField.getText().indexOf(".") == -1) {
textField.setText(textField.getText() + ".");
} else
textField.setText(textField.getText());
}
});

botton[17].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (check(textField.getText())) {
textField.setText(STR1);
} else
textField.setText(""
+ Math
.sqrt(Double.parseDouble(textField
.getText())));
}
});

botton[19].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = textField.getText();
if (check(str)) {
textField.setText(STR1);
} else if (isZero(str)) {
textField.setText(str);
} else if (str.indexOf("-") != -1) {
StringBuffer sb = new StringBuffer(str);
sb = sb.deleteCharAt(sb.indexOf("-"));
str = new String(sb);
textField.setText(str);
} else
textField.setText("-" + str);
}
});

botton[20].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (check(textField.getText())) {
textField.setText(STR1);
} else
textField
.setText(""
+ Math.sin(Double.parseDouble(textField
.getText())));
}
});

botton[21].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (check(textField.getText())) {
textField.setText(STR1);
} else
textField
.setText(""
+ Math.cos(Double.parseDouble(textField
.getText())));
}
});

botton[22].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (check(textField.getText())) {
textField.setText(STR1);
} else
textField
.setText(""
+ Math.tan(Double.parseDouble(textField
.getText())));
}
});

botton[23].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("" + Math.PI);
}
});

botton[24].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str2 = textField.getText();
if (check(str2)) {
textField.setText(STR1);
} else if (isZero(str2) && "/".equals(optype)) {
textField.setText(STR2);
} else {
operate(str1, str2);
textField.setText("" + result);
str1 = "";
str2 = "";
optype = "=";
}
}
});

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

frame.pack();
frame.setVisible(true);
}

private void operateTypeSet(final Button opbutton) {
opbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str1 = textField.getText();
if (check(str1)) {
str1 = "";
optype = null;
textField.setText(STR1);
} else {
textField.setText("");
optype = opbutton.getLabel();
}
}
});
}

private void buttonSet(final Button b) {
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (check(textField.getText())) {
textField.setText(b.getLabel());
} else {
textField.setText(textField.getText() + b.getLabel());
}
}
});
}

private boolean check(String param) {
return isNullOrEmpty(param) || STR1.equals(param) || STR2.equals(param);
}

private boolean isZero(String param) {
if (!isNullOrEmpty(param)) {
BigDecimal bigDecimal = new BigDecimal(param);
return bigDecimal.compareTo(new BigDecimal(0)) == 0;
} else {
return false;
}
}

private boolean isNullOrEmpty(String param) {
return param == null || param.trim().length() == 0;
}

private double operate(String param1, String param2) {
if (optype == "+") {
result = Double.parseDouble(param1) + Double.parseDouble(param2);
} else if (optype == "-") {
result = Double.parseDouble(param1) - Double.parseDouble(param2);
} else if (optype == "*") {
result = Double.parseDouble(param1) * Double.parseDouble(param2);
} else if (optype == "/") {
result = Double.parseDouble(param1) / Double.parseDouble(param2);
} else if (optype == "%") {
result = Double.parseDouble(param1) % Double.parseDouble(param2);
} else {
result = Double.parseDouble(param2);
}
return this.result;
}

public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.go();
}
}

回答2:

强人!!!!!!