java 计算机程序_用Java写一个计算机的程序怎么写啊拜托了各位 谢谢

展开全部

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 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'||32313133353236313431303231363533e58685e5aeb931333335323464e.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 } }

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以计算加减乘除的计算器小程序package zj lec1; import java awt BorderLayout; import java awt FlowLayout; import java awt GridLayout; import java awt event ActionEvent; import java awt event ActionListener; import java math BigInteger; import javax swing JButton; import javax swing JFrame; import javax swing JLabel; import javax swing JPanel; import javax swing JTextField; public class 简易计算机 extends JFrame implements ActionListener { JTextField a b c d; JButton ok exit; public 简易计算机 { this setLayout new FlowLayout ; this add a new JTextField 10 ; a setText "0" ; a setHorizontalAlignment JTextField RIGHT ; this add b new JTextField 1 ; this add c new JTextField 10 ; c setText "0" ; c setHorizontalAlignment JTextField RIGHT ; this add ok new JButton " " ; this add d new JTextField 10 ; this add exit new JButton "Exit" ; a addActionListener this ; b addActionListener this ; c addActionListener this ; exit addActionListener this ; ok addActionListener this ; this setSize 540 70 ; this setTitle "简易计算机" ; this setVisible true ; this setResizable false ; } public static void main String[] args { new 简易计算机 ; } @Override public void actionPerformed ActionEvent e { if e getSource ok || e getSource c || e getSource a { BigInteger ia new BigInteger a getText ; BigInteger ic new BigInteger c getText ; String ib b getText ; 判断是用加法 还是减法 还是乘法 除法 if ib contains "+" { BigInteger id ia add ic ; d setText id toString ; } else if ib contains " " { BigInteger id ia subtract ic ; d setText id toString ; } else if ib contains " " { BigInteger id ia multiply ic ; d setText id toString ; } else if ib contains " " { BigInteger id ia divide ic ; d setText id toString ; } } else if e getSource exit { System exit 0 ; } } }">可以计算加减乘除的计算器小程序package zj lec1; import java awt BorderLayout; import java awt FlowLayout; import java awt GridLayout; import java awt event ActionEvent; import java awt event ActionListener; import java math BigInteger; import javax swing JButton; import javax swing J [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值