java吧 关注:1,262,744贴子:12,763,279
  • 7回复贴,共1

求大神,期末课程设计,用Java编写简单计算器

只看楼主收藏回复

求能用的代码????


来自Android客户端1楼2014-06-05 09:21回复


    2楼2014-06-05 09:22
    收起回复
      2025-06-05 13:39:51
      广告
      自己顶贴


      来自Android客户端3楼2014-06-05 09:36
      回复
        去我的CSDN上下载,


        IP属地:四川4楼2014-06-05 12:00
        收起回复
          package com;
          import java.awt.BorderLayout;
          import java.awt.Button;
          import java.awt.Container;
          import java.awt.GridLayout;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.awt.event.KeyEvent;
          import java.awt.event.KeyListener;
          import java.awt.event.WindowAdapter;
          import java.awt.event.WindowEvent;
          import javax.swing.JFrame;
          import javax.swing.JOptionPane;
          import javax.swing.JPanel;
          import javax.swing.JTextField;
          import javax.swing.WindowConstants;
          public class Caculator extends JFrame implements ActionListener,KeyListener{
          /**
          *
          */
          private static final long serialVersionUID = 5204982079673572494L;
          private JTextField tf=new JTextField();
          private float x=0;
          private float y=0;
          private int code=0;
          private boolean enable;
          private boolean first;
          private String str="";
          public Caculator(){
          Container ct=this.getContentPane();
          this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
          tf.setHorizontalAlignment(JTextField.RIGHT);
          //tf.setText("0");
          enable=true;
          first=true;
          ct.add(tf,BorderLayout.NORTH);
          JPanel panel=new JPanel();
          panel.setLayout(new GridLayout(4,4));
          this.addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent e){
          if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(Caculator.this,"确定要关闭程序吗?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){
          e.getWindow().setVisible(false);
          e.getWindow().dispose();
          System.exit(0);
          }
          }
          });
          Button btn=null;
          btn=new Button("1");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("2");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("3");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("+");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("4");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("5");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("6");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("-");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("7");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("8");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("9");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("*");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("0");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button(".");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("/");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          btn=new Button("=");
          panel.add(btn);
          btn.addActionListener(this);
          btn.addKeyListener(this);
          this.add(panel,BorderLayout.CENTER);
          }
          /**
          * @param args
          */
          public static void main(String[] args) {
          // TODO Auto-generated method stub
          Caculator mainframe=new Caculator();
          mainframe.setTitle("testing Caculator");
          mainframe.setSize(400,400);
          mainframe.setVisible(true);
          }
          public void actionPerformed(ActionEvent e) {
          // TODO Auto-generated method stub
          if(e.getActionCommand()=="+"){
          x= Float.parseFloat(tf.getText());
          code=0;
          this.tf.setText("");
          }
          if(e.getActionCommand()=="-"){
          x= Float.parseFloat(tf.getText());
          code=1;
          this.tf.setText("");
          }
          if(e.getActionCommand()=="*"){
          x= Float.parseFloat(tf.getText());
          code=2;
          this.tf.setText("");
          }
          if(e.getActionCommand()=="/"){
          x= Float.parseFloat(tf.getText());
          code=3;
          this.tf.setText("");
          }
          if(e.getActionCommand()!="+"&&e.getActionCommand()!="-"&&e.getActionCommand()!="*"&&e.getActionCommand()!="/"&&e.getActionCommand()!="="){
          if(enable){
          if(first){
          System.out.println("haha");
          tf.setText(e.getActionCommand());
          first=false;
          }
          else {
          tf.setText(tf.getText()+e.getActionCommand());
          }
          }
          else {
          tf.setText(e.getActionCommand());
          enable=true;
          }
          }
          if(e.getActionCommand()=="="){
          switch(code){
          case 0:
          y=x+Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          case 1:
          y=x-Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          case 2:
          y=x*Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          case 3:
          y=x/Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          }
          }
          }
          public void keyPressed(KeyEvent e) {
          // TODO Auto-generated method stub
          if(e.getKeyChar()=='+'){
          x= Float.parseFloat(tf.getText());
          code=0;
          this.tf.setText("");
          }
          if(e.getKeyChar()=='-'){
          x= Float.parseFloat(tf.getText());
          code=1;
          this.tf.setText("");
          }
          if(e.getKeyChar()=='*'){
          x= Float.parseFloat(tf.getText());
          code=2;
          this.tf.setText("");
          }
          if(e.getKeyChar()=='/'){
          x= Float.parseFloat(tf.getText());
          code=3;
          this.tf.setText("");
          }
          if(e.getKeyChar()=='1'||e.getKeyChar()=='2'||e.getKeyChar()=='3'||e.getKeyChar()=='0'
          ||e.getKeyChar()=='4'||e.getKeyChar()=='5'||e.getKeyChar()=='6'||e.getKeyChar()=='.'
          ||e.getKeyChar()=='7'||e.getKeyChar()=='8'||e.getKeyChar()=='9'){
          System.out.println("hai");
          if(enable){
          if(first){
          System.out.println("hehe");
          str=Character.toString(e.getKeyChar());
          tf.setText(str);
          first=false;
          }
          else {
          str=Character.toString(e.getKeyChar());
          tf.setText(tf.getText()+str);
          }
          }
          else {
          str=Character.toString(e.getKeyChar());
          tf.setText(str);
          enable=true;
          }
          }
          if(e.getKeyCode()==KeyEvent.VK_ENTER){
          switch(code){
          case 0:
          y=x+Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          case 1:
          y=x-Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          case 2:
          y=x*Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          case 3:
          y=x/Float.parseFloat(this.tf.getText());
          tf.setText(Float.toString(y));
          enable=false;
          break;
          }
          }
          }
          public void keyReleased(KeyEvent arg0) {
          // TODO Auto-generated method stub
          }
          public void keyTyped(KeyEvent arg0) {
          // TODO Auto-generated method stub
          }
          }


          IP属地:北京5楼2014-06-05 20:20
          回复
            这个好简单的样子。。。。


            IP属地:重庆6楼2014-06-05 20:21
            回复