用Java编写一个简单的计算器界面,并且可以实现加减乘除计算,很简单一个就可以了,还需要一个归零按

2024-11-29 03:55:57
推荐回答(1个)
回答(1):

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();
}

}