用JAVA写一个计算器 要求有图形界面.简单的加减乘除,不要小数点运算.
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/05 14:46:18
用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}
}
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}
}
用JAVA写一个计算器 要求有图形界面.简单的加减乘除,不要小数点运算.
java的类 谢谢写一个计算器类Calculator,完成对两个数的加减乘除运算。
求简单java写计算器代码加减乘除
用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器
制作一个简单的计算器程序,实现加减乘除运算.
用labviEW做一个计算器,至少有加减乘除和小数点,
老师要求张浩使用面向对象的思想编写一个计算器类(Calculator),可以实现两个整数的加减乘除的运算.java
用C语言编程,编一个简单的计算器,需要用什么函数?这个计算器的功能有加减乘除,还有乘方,要求小...
c语言加减乘除设计;大神改下要求写一个简单的计算器,输入一个数学表达式 程序输出结果.
用C语言设计一个简单计算器程序,能根据键盘输入的运算指令和数据,做简单的加减乘除运算.
用java编写计算器 能够实现简单的加、减、乘、除、乘方、开方运算等;
c语言计算器程序设计包含加减乘除简单的函数运算