用JAVA写一个计算器 要求有图形界面.简单的加减乘除,不要小数点运算.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/23 17:15:32
用JAVA写一个计算器 要求有图形界面.简单的加减乘除,不要小数点运算.
用JAVA写一个计算器 要求有图形界面.简单的加减乘除,不要小数点运算.
用JAVA写一个计算器 要求有图形界面.简单的加减乘除,不要小数点运算.
寔杩炵画杩愮畻寰桙br/>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 {
\x05public static void main(String[] args) {
\x05\x05Frame f = new Frame("Calculator");
\x05\x05
\x05\x05final TextField tf = new TextField();
\x05\x05Panel p = new Panel();
\x05\x05
\x05\x05f.setLayout(new BorderLayout());
\x05\x05f.add(tf,BorderLayout.NORTH);
\x05\x05f.add(p,BorderLayout.CENTER);
\x05\x05
\x05\x05p.setLayout(new GridLayout(4,4));
\x05\x05
\x05\x05Button one = new Button("1");
\x05\x05Button two = new Button("2");
\x05\x05Button three = new Button("3");
\x05\x05Button four = new Button("4");
\x05\x05Button five = new Button("5");
\x05\x05Button six = new Button("6");
\x05\x05Button seven = new Button("7");
\x05\x05Button eight = new Button("8");
\x05\x05Button nine = new Button("9");
\x05\x05Button zero = new Button("0");
\x05\x05Button add = new Button("+");
\x05\x05Button sub = new Button("-");
\x05\x05Button mul = new Button("*");
\x05\x05Button div = new Button("/");
\x05\x05Button decimal = new Button(".");
\x05\x05final Button equal = new Button("=");
\x05\x05
\x05\x05p.add(one);
\x05\x05p.add(two);
\x05\x05p.add(three);
\x05\x05p.add(four);
\x05\x05p.add(five);
\x05\x05p.add(six);
\x05\x05p.add(seven);
\x05\x05p.add(eight);
\x05\x05p.add(nine);
\x05\x05p.add(zero);
\x05\x05p.add(add);
\x05\x05p.add(sub);
\x05\x05p.add(mul);
\x05\x05p.add(div);
\x05\x05p.add(decimal);
\x05\x05p.add(equal);
\x05\x05
\x05\x05final String[] ary = {"",null,""};
\x05\x05
\x05\x05class NumMouseListener implements MouseListener{
\x05\x05\x05public void mouseClicked(MouseEvent e) {
\x05\x05\x05\x05String num = ((Button)e.getSource()).getLabel().trim();
\x05\x05\x05\x05if(ary[1] == null){
\x05\x05\x05\x05\x05ary[0] = ary[0] + "" + num;
\x05\x05\x05\x05}else{
\x05\x05\x05\x05\x05ary[2] = ary[2] + "" + num;
\x05\x05\x05\x05}
\x05\x05\x05\x05
\x05\x05\x05\x05tf.setText(tf.getText() + "" + num);
\x05\x05\x05}
\x05\x05\x05public void mouseEntered(MouseEvent e) {}
\x05\x05\x05public void mouseExited(MouseEvent e) {}
\x05\x05\x05public void mousePressed(MouseEvent e) {}
\x05\x05\x05public void mouseReleased(MouseEvent e) {}
\x05\x05\x05
\x05\x05}
\x05\x05
\x05\x05one.addMouseListener(new NumMouseListener());
\x05\x05two.addMouseListener(new NumMouseListener());
\x05\x05three.addMouseListener(new NumMouseListener());
\x05\x05four.addMouseListener(new NumMouseListener());
\x05\x05five.addMouseListener(new NumMouseListener());
\x05\x05six.addMouseListener(new NumMouseListener());
\x05\x05seven.addMouseListener(new NumMouseListener());
\x05\x05eight.addMouseListener(new NumMouseListener());
\x05\x05nine.addMouseListener(new NumMouseListener());
\x05\x05zero.addMouseListener(new NumMouseListener());
\x05\x05
\x05\x05class OperatotrMouseListener implements MouseListener{
\x05\x05\x05public void mouseClicked(MouseEvent e) {
\x05\x05\x05\x05if(e.getSource() == equal){
\x05\x05\x05\x05\x05try {
\x05\x05\x05\x05\x05\x05long result = calculate(Integer.parseInt(ary[0]),Integer.parseInt(ary[2]),ary[1]);
\x05\x05\x05\x05\x05\x05tf.setText(String.valueOf(result));
\x05\x05\x05\x05\x05\x05
\x05\x05\x05\x05\x05\x05ary[0] = tf.getText();
\x05\x05\x05\x05\x05\x05ary[1] = null;
\x05\x05\x05\x05\x05\x05ary[2] = "";
\x05\x05\x05\x05\x05} catch (Exception e1) {
\x05\x05\x05\x05\x05\x05e1.printStackTrace();
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05}else{
\x05\x05\x05\x05\x05ary[1] = ((Button) e.getSource()).getLabel();
\x05\x05\x05\x05\x05
\x05\x05\x05\x05\x05if(ary[0].length() > 0){
\x05\x05\x05\x05\x05\x05ary[2] = "";
\x05\x05\x05\x05\x05}else{
\x05\x05\x05\x05\x05\x05ary[0] = String.valueOf(tf.getText());
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05\x05
\x05\x05\x05\x05\x05tf.setText("");
\x05\x05\x05\x05}
\x05\x05\x05}
\x05\x05\x05private long calculate(int i,int j,String operator) throws Exception {
\x05\x05\x05\x05if(operator.equals("+")){
\x05\x05\x05\x05\x05return i + j;
\x05\x05\x05\x05}else if(operator.equals("-")){
\x05\x05\x05\x05\x05return i - j;
\x05\x05\x05\x05}else if(operator.equals("*")){
\x05\x05\x05\x05\x05return i * j;
\x05\x05\x05\x05}else if(operator.equals("/")){
\x05\x05\x05\x05\x05return i / j;
\x05\x05\x05\x05}else{
\x05\x05\x05\x05\x05throw new Exception("Invalid operator.(Only +-/* accepted");
\x05\x05\x05\x05}
\x05\x05\x05}
\x05\x05\x05public void mouseEntered(MouseEvent e) {}
\x05\x05\x05public void mouseExited(MouseEvent e) {}
\x05\x05\x05public void mousePressed(MouseEvent e) {}
\x05\x05\x05public void mouseReleased(MouseEvent e) {}
\x05\x05}
\x05\x05
\x05\x05add.addMouseListener(new OperatotrMouseListener());
\x05\x05mul.addMouseListener(new OperatotrMouseListener());
\x05\x05div.addMouseListener(new OperatotrMouseListener());
\x05\x05sub.addMouseListener(new OperatotrMouseListener());
\x05\x05equal.addMouseListener(new OperatotrMouseListener());
\x05\x05
\x05\x05f.addWindowListener(new WindowListener(){
\x05\x05\x05public void windowClosing(WindowEvent e) {
\x05\x05\x05\x05System.exit(0);
\x05\x05\x05}
\x05\x05\x05
\x05\x05\x05public void windowActivated(WindowEvent e) {}
\x05\x05\x05public void windowClosed(WindowEvent e) {}
\x05\x05\x05public void windowDeactivated(WindowEvent e) {}
\x05\x05\x05public void windowDeiconified(WindowEvent e) {}
\x05\x05\x05public void windowIconified(WindowEvent e) {}
\x05\x05\x05public void windowOpened(WindowEvent e) {}
\x05\x05});
\x05\x05
\x05\x05f.setVisible(true);
\x05\x05f.setBounds(200,200,250,250);
\x05\x05f.validate();
\x05}
}