用Java编写一个简单的计算器界面,并且可以实现加减乘除计算,很简单一个就可以了,还需要一个归零按
用Java编写一个简单的计算器界面,并且可以实现加减乘除计算,很简单一个就可以了,还需要一个归零按钮就可以了...
用Java编写一个简单的计算器界面,并且可以实现加减乘除计算,很简单一个就可以了,还需要一个归零按钮就可以了
展开
展开全部
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Calculator {
public static void main(String[] args) {
Frame f = new Frame("Calculator");
final TextField tf = new TextField();
Panel p = new Panel();
f.setLayout(new BorderLayout());
f.add(tf, BorderLayout.NORTH);
f.add(p, BorderLayout.CENTER);
p.setLayout(new GridLayout(4, 4));
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button add = new Button("+");
Button sub = new Button("-");
Button mul = new Button("*");
Button div = new Button("/");
Button decimal = new Button(".");
final Button equal = new Button("=");
p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
p.add(six);
p.add(seven);
p.add(eight);
p.add(nine);
p.add(zero);
p.add(add);
p.add(sub);
p.add(mul);
p.add(div);
p.add(decimal);
p.add(equal);
final String[] ary = {"", null, ""};
class NumMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
String num = ((Button)e.getSource()).getLabel().trim();
if(ary[1] == null){
ary[0] = ary[0] + "" + num;
}else{
ary[2] = ary[2] + "" + num;
}
tf.setText(tf.getText() + "" + num);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
one.addMouseListener(new NumMouseListener());
two.addMouseListener(new NumMouseListener());
three.addMouseListener(new NumMouseListener());
four.addMouseListener(new NumMouseListener());
five.addMouseListener(new NumMouseListener());
six.addMouseListener(new NumMouseListener());
seven.addMouseListener(new NumMouseListener());
eight.addMouseListener(new NumMouseListener());
nine.addMouseListener(new NumMouseListener());
zero.addMouseListener(new NumMouseListener());
class OperatotrMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == equal){
try {
long result = calculate(Integer.parseInt(ary[0]), Integer.parseInt(ary[2]), ary[1]);
tf.setText(String.valueOf(result));
ary[0] = tf.getText();
ary[1] = null;
ary[2] = "";
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
ary[1] = ((Button) e.getSource()).getLabel();
if(ary[0].length() > 0){
ary[2] = "";
}else{
ary[0] = String.valueOf(tf.getText());
}
tf.setText("");
}
}
private long calculate(int i, int j, String operator) throws Exception {
if(operator.equals("+")){
return i + j;
}else if(operator.equals("-")){
return i - j;
}else if(operator.equals("*")){
return i * j;
}else if(operator.equals("/")){
return i / j;
}else{
throw new Exception("Invalid operator.(Only +-/* accepted");
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
add.addMouseListener(new OperatotrMouseListener());
mul.addMouseListener(new OperatotrMouseListener());
div.addMouseListener(new OperatotrMouseListener());
sub.addMouseListener(new OperatotrMouseListener());
equal.addMouseListener(new OperatotrMouseListener());
f.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});
f.setVisible(true);
f.setBounds(200, 200, 250, 250);
f.validate();
}
}
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Calculator {
public static void main(String[] args) {
Frame f = new Frame("Calculator");
final TextField tf = new TextField();
Panel p = new Panel();
f.setLayout(new BorderLayout());
f.add(tf, BorderLayout.NORTH);
f.add(p, BorderLayout.CENTER);
p.setLayout(new GridLayout(4, 4));
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button add = new Button("+");
Button sub = new Button("-");
Button mul = new Button("*");
Button div = new Button("/");
Button decimal = new Button(".");
final Button equal = new Button("=");
p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
p.add(six);
p.add(seven);
p.add(eight);
p.add(nine);
p.add(zero);
p.add(add);
p.add(sub);
p.add(mul);
p.add(div);
p.add(decimal);
p.add(equal);
final String[] ary = {"", null, ""};
class NumMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
String num = ((Button)e.getSource()).getLabel().trim();
if(ary[1] == null){
ary[0] = ary[0] + "" + num;
}else{
ary[2] = ary[2] + "" + num;
}
tf.setText(tf.getText() + "" + num);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
one.addMouseListener(new NumMouseListener());
two.addMouseListener(new NumMouseListener());
three.addMouseListener(new NumMouseListener());
four.addMouseListener(new NumMouseListener());
five.addMouseListener(new NumMouseListener());
six.addMouseListener(new NumMouseListener());
seven.addMouseListener(new NumMouseListener());
eight.addMouseListener(new NumMouseListener());
nine.addMouseListener(new NumMouseListener());
zero.addMouseListener(new NumMouseListener());
class OperatotrMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == equal){
try {
long result = calculate(Integer.parseInt(ary[0]), Integer.parseInt(ary[2]), ary[1]);
tf.setText(String.valueOf(result));
ary[0] = tf.getText();
ary[1] = null;
ary[2] = "";
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
ary[1] = ((Button) e.getSource()).getLabel();
if(ary[0].length() > 0){
ary[2] = "";
}else{
ary[0] = String.valueOf(tf.getText());
}
tf.setText("");
}
}
private long calculate(int i, int j, String operator) throws Exception {
if(operator.equals("+")){
return i + j;
}else if(operator.equals("-")){
return i - j;
}else if(operator.equals("*")){
return i * j;
}else if(operator.equals("/")){
return i / j;
}else{
throw new Exception("Invalid operator.(Only +-/* accepted");
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
add.addMouseListener(new OperatotrMouseListener());
mul.addMouseListener(new OperatotrMouseListener());
div.addMouseListener(new OperatotrMouseListener());
sub.addMouseListener(new OperatotrMouseListener());
equal.addMouseListener(new OperatotrMouseListener());
f.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});
f.setVisible(true);
f.setBounds(200, 200, 250, 250);
f.validate();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询