[code="java"]import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Main extends JFrame {
Panel btnPanel = new Panel();
JTextField textField = new JTextField("");
// 添加标志
String s;
BigDecimal num = new BigDecimal(0 + "");
Boolean zflag = false;
operator op = operator.nul;
public enum operator {
add, sub, mul, div, nul
}
public Main() {
super("Calculator");
Init();
}
public void Init() {
this.setSize(325, 280);
this.setLayout(new GridLayout());
textField.setBounds(50, 40, 200, 30);
textField.setEditable(false);
// 添加按钮
JButton[] btn = new JButton[17];
btn[1] = new JButton("0");
btn[2] = new JButton("1");
btn[3] = new JButton("2");
btn[4] = new JButton("3");
btn[5] = new JButton("4");
btn[6] = new JButton("5");
btn[7] = new JButton("6");
btn[8] = new JButton("7");
btn[9] = new JButton("8");
btn[10] = new JButton("9");
btn[11] = new JButton("+");
btn[12] = new JButton("-");
btn[13] = new JButton("*");
btn[14] = new JButton("/");
btn[15] = new JButton("=");
btn[16] = new JButton("CR");
// 设置位置
for (int i = 1; i < btn.length; i++) {
if (i > 12)
btn[i].setBounds(50 * (i - 12), 200, 50, 30);
else if (i > 8)
btn[i].setBounds(50 * (i - 8), 160, 50, 30);
else if (i > 4)
btn[i].setBounds(50 * (i - 4), 120, 50, 30);
else
btn[i].setBounds(50 * i, 80, 50, 30);
btn[i].addActionListener(new btnAction());
btnPanel.add(btn[i]);
}
btnPanel.setLayout(null);
btnPanel.setBounds(80, 50, 10, 25);
btnPanel.add(textField);
add(btnPanel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 检查操作
public BigDecimal check() {
//是否有字的标志
zflag = true;
BigDecimal bd = new BigDecimal(textField.getText());
if (op == operator.nul) {
return num = bd;
} else if (op == operator.add) {
return num = num.add(bd);
} else if (op == operator.sub) {
return num = num.subtract(bd);
} else if (op == operator.mul) {
return num = num.multiply(bd);
} else if (op == operator.div) {
try {
return num = num.divide(bd);
} catch (ArithmeticException e) {
if (bd == new BigDecimal(0))
JOptionPane.showMessageDialog(null, "除数不能为空!", "消息!", 1);
else if (bd == new BigDecimal(""))
JOptionPane.showMessageDialog(null, "除数不能为0!", "消息!", 1);
else
return num = num.divide(bd, 3, BigDecimal.ROUND_HALF_UP);
}
}
return bd;
}
private class btnAction implements ActionListener {
// 验证有有文字,有符号则清空Textbox
public void validation(ActionEvent event) {
if (zflag) {
textField.setText("");
zflag = false;
}
textField.setText(textField.getText() + event.getActionCommand<script type="text/javascript" src="http://www.iteye.com/javascripts/tinymce/themes/advanced/langs/zh.js"></script><script type="text/javascript" src="http://www.iteye.com/javascripts/tinymce/plugins/javaeye/langs/zh.js"></script>());
}
public void actionPerformed(ActionEvent event) {
s = event.getActionCommand();
if (s == "0")
if (!textField.getText().equals(""))
textField.setText(textField.getText() + s);
if (s == "1")
validation(event);
if (s == "2")
validation(event);
if (s == "3")
validation(event);
if (s == "4")
validation(event);
if (s == "5")
validation(event);
if (s == "6")
validation(event);
if (s == "7")
validation(event);
if (s == "8")
validation(event);
if (s == "9")
validation(event);
if (s == "CR") {
textField.setText("");
op = operator.nul;
num = new BigDecimal(0);
}
if (s == "+") {
check();
op = operator.add;
}
if (s == "-") {
check();
op = operator.sub;
}
if (s == "*") {
check();
op = operator.mul;
}
if (event.getActionCommand() == "/") {
check();
op = operator.div;
}
if (event.getActionCommand() == "=") {
check();
op = operator.nul;
zflag = true;
textField.setText("" + num);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
}
[/code]
求帮忙看一下。3Q
[b]问题补充:[/b]
[code="java"]import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Main extends JFrame {
Panel btnPanel = new Panel();
JTextField textField = new JTextField("");
// 添加标志
String s;
BigDecimal num = new BigDecimal(0 + "");
Boolean zflag = false;
operator op = operator.nul;
operator opResult = operator.nul;
public enum operator {
add, sub, mul, div, nul
}
public Main() {
super("Calculator");
Init();
}
public void Init() {
this.setSize(325, 310);
// this.setLayout(new GridLayout());
this.setLayout(new BorderLayout());
textField.setBounds(50, 40, 200, 30);
textField.setEditable(false);
// 添加按钮
JButton[] btn = new JButton[17];
btn[1] = new JButton("0");
btn[2] = new JButton("1");
btn[3] = new JButton("2");
btn[4] = new JButton("3");
btn[5] = new JButton("4");
btn[6] = new JButton("5");
btn[7] = new JButton("6");
btn[8] = new JButton("7");
btn[9] = new JButton("8");
btn[10] = new JButton("9");
btn[11] = new JButton("+");
btn[12] = new JButton("-");
btn[13] = new JButton("*");
btn[14] = new JButton("/");
btn[15] = new JButton("=");
btn[16] = new JButton("CR");
// 设置位置
for (int i = 1; i < btn.length; i++) {
if (i > 12)
btn[i].setBounds(50 * (i - 12), 200, 50, 30);
else if (i > 8)
btn[i].setBounds(50 * (i - 8), 160, 50, 30);
else if (i > 4)
btn[i].setBounds(50 * (i - 4), 120, 50, 30);
else
btn[i].setBounds(50 * i, 80, 50, 30);
btn[i].addActionListener(new btnAction());
btnPanel.add(btn[i]);
}
btnPanel.setLayout(null);
btnPanel.setBounds(80, 50, 10, 25);
btnPanel.add(textField);
add(btnPanel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 检查操作
public BigDecimal check() {
//是否有字的标志
zflag = true;
BigDecimal bd = new BigDecimal(textField.getText());
if(opResult == operator.nul) {
opResult = op;
num = bd;
}
return bd;
}
private BigDecimal result() {
// TODO Auto-generated method stub
BigDecimal bd = new BigDecimal(textField.getText());
if (opResult == operator.nul) {
return num = bd;
} else if (opResult == operator.add) {
return num = num.add(bd);
} else if (opResult == operator.sub) {
return num = num.subtract(bd);
} else if (opResult == operator.mul) {
return num = num.multiply(bd);
} else if (opResult == operator.div) {
try {
return num = num.divide(bd);
} catch (ArithmeticException e) {
if (bd == new BigDecimal(0))
JOptionPane.showMessageDialog(null, "除数不能为空!", "消息!", 1);
else if (bd == new BigDecimal(""))
JOptionPane.showMessageDialog(null, "除数不能为0!", "消息!", 1);
else
return num = num.divide(bd, 3, BigDecimal.ROUND_HALF_UP);
}
}
return bd;
}
private class btnAction implements ActionListener {
// 验证有有文字,有符号则清空Textbox
public void validation(ActionEvent event) {
if (zflag) {
textField.setText("");
zflag = false;
}
textField.setText(textField.getText() + event.getActionCommand());
}
public void actionPerformed(ActionEvent event) {
s = event.getActionCommand();
if (s == "0")
if (!textField.getText().equals(""))
textField.setText(textField.getText() + s);
if (s == "1")
validation(event);
if (s == "2")
validation(event);
if (s == "3")
validation(event);
if (s == "4")
validation(event);
if (s == "5")
validation(event);
if (s == "6")
validation(event);
if (s == "7")
validation(event);
if (s == "8")
validation(event);
if (s == "9")
validation(event);
if (s == "CR") {
textField.setText("");
op = operator.nul;
num = new BigDecimal(0);
}
if (s == "+") {
op = operator.add;
check();
}
if (s == "-") {
op = operator.sub;
check();
}
if (s == "*") {
op = operator.mul;
check();
}
if (event.getActionCommand() == "/") {
op = operator.div;
check();
}
if (event.getActionCommand() == "=") {
result();
opResult = op = operator.nul;
zflag = true;
textField.setText("" + num);
num = new BigDecimal(0);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//new Main();
}
}
[/code]
上面的代码不知道啥原因没帖好。