
求JAVA高手帮贴一个JAVA编写的计算器代码!要有简单的加减乘除功能?
2个回答
2013-11-21
展开全部
全部复制后 运行。完成了简单的功能。
package calc.qq;
import java.awt.BorderLayout;
import java.awt.Color;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
@SuppressWarnings("serial")
public class Jisuan extends JFrame implements ActionListener {
private JTextField reasult;
private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0,
btnAC, btnAdd, btnSub, btnReasult, btnD, btnAbout, btnCancel;
@SuppressWarnings("unused")
private boolean add, sub, end, s, c;
private String str;
private double num1, num2;
public Jisuan() {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
TitledBorder tb = new TitledBorder("输出");
tb.setTitleColor(Color.BLUE);
btnAbout = new JButton(" 关于 ");
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
System.exit(0);
}
});
btnAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
JOptionPane.showMessageDialog(null, "Lapore Meta QQ 80615677", "消息",
JOptionPane.INFORMATION_MESSAGE);
}
});
p3.add(btnAbout);
p3.add(btnCancel);
// JPanel p4=new JPanel();
// JPanel p5=new JPanel();
// reasult.setBorder(tb);
reasult = new JTextField("0", 20);
reasult.setEditable(false);
reasult.setHorizontalAlignment(JTextField.RIGHT);
reasult.setForeground(Color.BLUE);
p1.setBorder(tb);
p1.add(reasult);
btn0 = new JButton("0");
btn0.addActionListener(this);
btn1 = new JButton("1");
btn1.addActionListener(this);
btn2 = new JButton("2");
btn2.addActionListener(this);
btn3 = new JButton("3");
btn3.addActionListener(this);
btn4 = new JButton("4");
btn4.addActionListener(this);
btn5 = new JButton("5");
btn5.addActionListener(this);
btn6 = new JButton("6");
btn6.addActionListener(this);
btn7 = new JButton("7");
btn7.addActionListener(this);
btn8 = new JButton("8");
btn8.addActionListener(this);
btn9 = new JButton("9");
btn9.addActionListener(this);
btnD = new JButton(".");
btnD.addActionListener(this);
btnD.setForeground(Color.RED);
btnAC = new JButton("AC");
btnAC.addActionListener(this);
btnAC.setBackground(Color.PINK);
btnAdd = new JButton("+");
btnAdd.addActionListener(this);
btnAdd.setForeground(Color.BLUE);
btnSub = new JButton("—");
btnSub.addActionListener(this);
btnSub.setForeground(Color.BLUE);
btnReasult = new JButton("=");
btnReasult.addActionListener(this);
btnReasult.setForeground(Color.RED);
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
p2.add(btn7);
p2.add(btn8);
p2.add(btn9);
p2.add(btn0);
p2.add(btnD);
p2.add(btnAC);
p2.add(btnAdd);
p2.add(btnSub);
p2.add(btnReasult);
p2.setLayout(new GridLayout(5, 3));
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);
}
public void num(int i) {
String s = null;
s = String.valueOf(i);
if (end) {
// 如果数字输入结束,则将文本框置零,重新输入
reasult.setText("0");
end = false;
}
if ((reasult.getText()).equals("0")) {
// 如果文本框的内容为零,则覆盖文本框的内容
reasult.setText(s);
}
else {
// 如果文本框的内容不为零,则在内容后面添加数字
str = reasult.getText() + s;
reasult.setText(str);
}
}/*
* String s=null; s=String.valueOf(i); str=reasult.getText()+s;
* reasult.setText(str); }
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1)
num(1);
else if (e.getSource() == btn2)
num(2);
else if (e.getSource() == btn3)
num(3);
else if (e.getSource() == btn4)
num(4);
else if (e.getSource() == btn5)
num(5);
else if (e.getSource() == btn6)
num(6);
else if (e.getSource() == btn7)
num(7);
else if (e.getSource() == btn8)
num(8);
else if (e.getSource() == btn9)
num(9);
else if (e.getSource() == btn0)
num(0);
else if (e.getSource() == btnAdd) {
sign(1);
btnD.setEnabled(true);
} else if (e.getSource() == btnSub) {
sign(2);
btnD.setEnabled(true);
} else if (e.getSource() == btnAC) {
btnD.setEnabled(true);
reasult.setText("0");
}
else if (e.getSource() == btnD) {
str = reasult.getText();
str += ".";
reasult.setText(str);
btnD.setEnabled(false);
} else if (e.getSource() == btnReasult) {
btnD.setEnabled(true);
num2 = Double.parseDouble(reasult.getText());
if (add) {
num1 = num1 + num2;
} else if (sub) {
num1 = num1 - num2;
}
reasult.setText(String.valueOf(num1));
end = true;
}
}
public void sign(int s) {
if (s == 1) {
add = true;
sub = false;
} else if (s == 2) {
add = false;
sub = true;
}
num1 = Double.parseDouble(reasult.getText());
end = true;
}
public static void main(String[] args) {
Jisuan j = new Jisuan();
j.setTitle("+/-简易计算器");
j.setLocation(500, 280);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setResizable(false);
j.pack();
j.setVisible(true);
}
}
package calc.qq;
import java.awt.BorderLayout;
import java.awt.Color;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
@SuppressWarnings("serial")
public class Jisuan extends JFrame implements ActionListener {
private JTextField reasult;
private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0,
btnAC, btnAdd, btnSub, btnReasult, btnD, btnAbout, btnCancel;
@SuppressWarnings("unused")
private boolean add, sub, end, s, c;
private String str;
private double num1, num2;
public Jisuan() {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
TitledBorder tb = new TitledBorder("输出");
tb.setTitleColor(Color.BLUE);
btnAbout = new JButton(" 关于 ");
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
System.exit(0);
}
});
btnAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ee) {
JOptionPane.showMessageDialog(null, "Lapore Meta QQ 80615677", "消息",
JOptionPane.INFORMATION_MESSAGE);
}
});
p3.add(btnAbout);
p3.add(btnCancel);
// JPanel p4=new JPanel();
// JPanel p5=new JPanel();
// reasult.setBorder(tb);
reasult = new JTextField("0", 20);
reasult.setEditable(false);
reasult.setHorizontalAlignment(JTextField.RIGHT);
reasult.setForeground(Color.BLUE);
p1.setBorder(tb);
p1.add(reasult);
btn0 = new JButton("0");
btn0.addActionListener(this);
btn1 = new JButton("1");
btn1.addActionListener(this);
btn2 = new JButton("2");
btn2.addActionListener(this);
btn3 = new JButton("3");
btn3.addActionListener(this);
btn4 = new JButton("4");
btn4.addActionListener(this);
btn5 = new JButton("5");
btn5.addActionListener(this);
btn6 = new JButton("6");
btn6.addActionListener(this);
btn7 = new JButton("7");
btn7.addActionListener(this);
btn8 = new JButton("8");
btn8.addActionListener(this);
btn9 = new JButton("9");
btn9.addActionListener(this);
btnD = new JButton(".");
btnD.addActionListener(this);
btnD.setForeground(Color.RED);
btnAC = new JButton("AC");
btnAC.addActionListener(this);
btnAC.setBackground(Color.PINK);
btnAdd = new JButton("+");
btnAdd.addActionListener(this);
btnAdd.setForeground(Color.BLUE);
btnSub = new JButton("—");
btnSub.addActionListener(this);
btnSub.setForeground(Color.BLUE);
btnReasult = new JButton("=");
btnReasult.addActionListener(this);
btnReasult.setForeground(Color.RED);
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
p2.add(btn7);
p2.add(btn8);
p2.add(btn9);
p2.add(btn0);
p2.add(btnD);
p2.add(btnAC);
p2.add(btnAdd);
p2.add(btnSub);
p2.add(btnReasult);
p2.setLayout(new GridLayout(5, 3));
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);
}
public void num(int i) {
String s = null;
s = String.valueOf(i);
if (end) {
// 如果数字输入结束,则将文本框置零,重新输入
reasult.setText("0");
end = false;
}
if ((reasult.getText()).equals("0")) {
// 如果文本框的内容为零,则覆盖文本框的内容
reasult.setText(s);
}
else {
// 如果文本框的内容不为零,则在内容后面添加数字
str = reasult.getText() + s;
reasult.setText(str);
}
}/*
* String s=null; s=String.valueOf(i); str=reasult.getText()+s;
* reasult.setText(str); }
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1)
num(1);
else if (e.getSource() == btn2)
num(2);
else if (e.getSource() == btn3)
num(3);
else if (e.getSource() == btn4)
num(4);
else if (e.getSource() == btn5)
num(5);
else if (e.getSource() == btn6)
num(6);
else if (e.getSource() == btn7)
num(7);
else if (e.getSource() == btn8)
num(8);
else if (e.getSource() == btn9)
num(9);
else if (e.getSource() == btn0)
num(0);
else if (e.getSource() == btnAdd) {
sign(1);
btnD.setEnabled(true);
} else if (e.getSource() == btnSub) {
sign(2);
btnD.setEnabled(true);
} else if (e.getSource() == btnAC) {
btnD.setEnabled(true);
reasult.setText("0");
}
else if (e.getSource() == btnD) {
str = reasult.getText();
str += ".";
reasult.setText(str);
btnD.setEnabled(false);
} else if (e.getSource() == btnReasult) {
btnD.setEnabled(true);
num2 = Double.parseDouble(reasult.getText());
if (add) {
num1 = num1 + num2;
} else if (sub) {
num1 = num1 - num2;
}
reasult.setText(String.valueOf(num1));
end = true;
}
}
public void sign(int s) {
if (s == 1) {
add = true;
sub = false;
} else if (s == 2) {
add = false;
sub = true;
}
num1 = Double.parseDouble(reasult.getText());
end = true;
}
public static void main(String[] args) {
Jisuan j = new Jisuan();
j.setTitle("+/-简易计算器");
j.setLocation(500, 280);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setResizable(false);
j.pack();
j.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-21
展开全部
思考比学习更重要。我写过一个算式解释器,程序界只有一个文本框(大道至简),先找到最外层括号,把其中字符串递归解释,直到没有括号为止,然后查找优先级高的运算符…你可以去尝试。也可以去写解析式解释器,画出函数图像。祝你好运
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询