您好,欢迎光临本网站![请登录][注册会员]  
文件名称: java课程设计
  所属分类: Java
  开发工具:
  文件大小: 828kb
  下载次数: 0
  上传时间: 2012-06-09
  提 供 者: njust_c*******
 详细说明: java课程设计 简易有声计算器 package zhang.java; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.math.BigDecimal; import java.io.File; import java.applet.*; import java.net.MalformedURLException; class SoundThread implements Runnable{ AudioClip chosenClip; static boolean isplay=true; static boolean flag=true; public void stopSoundThread(){flag=false;} public SoundThread(File file){ super(); try{ chosenClip=Applet.newAudioClip(file.toURL()); }//try catch (MalformedURLException e){ e.printStackTrace(); }//catch }//SoundThread(File file1) public void run(){ while(flag){ synchronized (this){ if(isplay) chosenClip.play(); else { try{this.wait();} catch(InterruptedException e){e.printStackTrace();} }//else try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } this.stopSoundThread(); }//synchronized (this) }//while }//run() }//class SoundThread class Sound_Play_File{ SoundThread sthread; Thread thread; public void init(File file){ sthread=new SoundThread(file); thread=new Thread(sthread); thread.start(); }//init public void play(){ synchronized(this){ sthread.isplay=true; sthread.notify(); }//synchronized }//play() public void stop(){ sthread.isplay=false; }//stop public void doit(File file){ this.init(file); if(sthread.isplay){ this.play(); synchronized(this){ try{this.wait();} catch(InterruptedException e){e.printStackTrace();} }//synchronized } else{ this.stop(); synchronized(this){ try{this.wait();} catch(InterruptedException e){e.printStackTrace();} }//synchronized } }//doit }//class Sound_Play_File class MyFrame extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; private JButton nButton;//数字按钮 private JButton pButton;//运算符按钮 private JTextField stext;//显示文本框 private JTextField otext;//运算文本框 private double arg=0.0; private String op="="; private boolean start=true; private final int WIDTH=305;//窗口宽 private final int HEITH=280;////窗口高 static Sound_Play_File sound=new Sound_Play_File(); /*-------------------构造函数----------------------*/ public MyFrame(){ super(); this.setLayout(null); this.setSize(WIDTH,HEITH);//设定框架大小 stext=new JTextField(); stext.setEditable(false);//不可编辑 stext.setBounds(0,0,298,20); this.add(stext); otext=new JTextField("0."); otext.setHorizontalAlignment(JTextField.RIGHT);//靠右显示 otext.setBounds(0,20,298,20); otext.setEditable(false); this.add(otext); /*--------------数字按钮构建----------*/ nButton=new JButton("7"); nButton.addActionListener(this); nButton.setBounds(0,40,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("8"); nButton.addActionListener(this); nButton.setBounds(60,40,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("9"); nButton.addActionListener(this); nButton.setBounds(120,40,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("4"); nButton.addActionListener(this); nButton.setBounds(0,80,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("5"); nButton.addActionListener(this); nButton.setBounds(60,80,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("6"); nButton.addActionListener(this); nButton.setBounds(120,80,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("1"); nButton.addActionListener(this); nButton.setBounds(0,120,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("2"); nButton.addActionListener(this); nButton.setBounds(60,120,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("3"); nButton.addActionListener(this); nButton.setBounds(120,120,60,40); nButton.setForeground(Color.blue); this.add(nButton); nButton=new JButton("0"); nButton.addActionListener(this); nButton.setBounds(0,160,60,40); nButton.setForeground(Color.blue); this.add(nButton); /*-------------运算符按钮构建------------*/ pButton=new JButton("."); pButton.addActionListener(this); pButton.setBounds(60,160,60,40); pButton.setForeground(Color.BLACK); add(pButton); pButton=new JButton("sqrt"); pButton.addActionListener(this); pButton.setBounds(120,160,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("+"); pButton.addActionListener(this); pButton.setBounds(180,40,60,40); pButton.setForeground(Color.BLACK); add(pButton); pButton=new JButton("-"); pButton.addActionListener(this); pButton.setBounds(180,80,60,40); pButton.setForeground(Color.BLACK); add(pButton); pButton=new JButton("*"); pButton.addActionListener(this); pButton.setBounds(180,120,60,40); pButton.setForeground(Color.BLACK); add(pButton); pButton=new JButton("/"); pButton.addActionListener(this); pButton.setBounds(180,160,60,40); pButton.setForeground(Color.BLACK); add(pButton); pButton=new JButton("退格"); pButton.addActionListener(this); pButton.setBounds(240,40,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("sin"); pButton.addActionListener(this); pButton.setBounds(240,80,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("cos"); pButton.addActionListener(this); pButton.setBounds(240,120,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("tan"); pButton.addActionListener(this); pButton.setBounds(240,160,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("归零"); pButton.addActionListener(this); pButton.setBounds(0,200,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("静音"); pButton.addActionListener(this); pButton.setBounds(60,200,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("有声"); pButton.addActionListener(this); pButton.setBounds(120,200,60,40); pButton.setForeground(Color.RED); add(pButton); pButton=new JButton("="); pButton.addActionListener(this); pButton.setBounds(180,200,120,40); pButton.setForeground(Color.RED); add(pButton); //this.setVisible(true); }//MyFrame() public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String s=e.getActionCommand(); if(s.equals("静音")){ sound.sthread.flag=false; } else if(s.equals("有声")){ sound.sthread.flag=true; } else if(('0'<=s.charAt(0)&&s.charAt(0)<='9')||s.charAt(0)=='.'){ if(start)otext.setText(s); else otext.setText(otext.getText()+s); start=false; stext.setText(stext.getText()+s); if(s.charAt(0)=='0'){ File file=new File("shengyin\\0.wav"); sound.doit(file); } else if(s.charAt(0)=='1'){ File file=new File("shengyin\\1.wav"); sound.doit(file); } else if(s.charAt(0)=='2'){ File file=new File("shengyin\\2.wav"); sound.doit(file); } else if(s.charAt(0)=='3'){ File file=new File("shengyin\\3.wav"); sound.doit(file); } else if(s.charAt(0)=='4'){ File file=new File("shengyin\\4.wav"); sound.doit(file); } else if(s.charAt(0)=='5'){ File file=new File("shengyin\\5.wav"); sound.doit(file); } else if(s.charAt(0)=='6'){ File file=new File("shengyin\\6.wav"); sound.doit(file); } else if(s.charAt(0)=='7'){ File file=new File("shengyin\\7.wav"); sound.doit(file); } else if(s.charAt(0)=='8'){ File file=new File("shengyin\\8.wav"); sound.doit(file); } else if(s.charAt(0)=='9'){ File file=new File("shengyin\\9.wav"); sound.doit(file); } }//第一个if else if(s.equals("归零")){ stext.setText(""); otext.setText("0."); start=true; op="="; File file=new File("shengyin\\guiling.wav"); sound.doit(file); }//if(s.equals("归零")) else if(s.equals("退格")){ if(stext.getText()==null){} else{ String s1=otext.getText().substring(0, otext.getText().length()-1); otext.setText(s1); String s2=stext.getText().substring(0, stext.getText().length()-1); stext.setText(s2); }//else File file=new File(""); sound.doit(file); }//if(s.equals("退格")) else{ stext.setText(stext.getText()+s); if(start){ if(s.equals("-")){//输入负数 otext.setText(s); start=false; }//if(s.equals("-")) else op=s; }//第二个if else{ double temp=Double.parseDouble(otext.getText()); caculator(temp); start=true; op=s; if(s.equals("sin")){temp=Double.parseDouble(otext.getText()); caculator(temp);op=s;} else if(s.equals("cos")){} else if(s.equals("tan")){} else if(s.equals("sqrt")){} else if(s.equals("=")){ stext.setText(stext.getText()+arg+";"); otext.setText(""+arg); }//if(s.equals("=") start=true; }//第二个else }//第一个else }//actionPerformed(ActionEvent e) public double Number(double pDouble){ //保留小数点后四位 BigDecimal bd=new BigDecimal(pDouble); BigDecimal bd1=bd.setScale(5,bd.ROUND_HALF_UP); pDouble=bd1.doubleValue(); return pDouble; }//Number(double pDouble) public void caculator(double temp) {//计算程序 // TODO Auto-generated method stub if(op.equals("+")) {arg+=temp;arg=Number(arg);} else if(op.equals("-")){arg-=temp;arg=Number(arg);} else if(op.equals("*")){arg*=temp;arg=Number(arg);} else if(op.equals("/")){ if(temp==0){ stext.setText(stext.getText()+"除数不能为0!ERROR!!!"); otext.setText("ERROR!!!"); }//if(temp==0) else {arg/=temp;arg=Number(arg);} } else if(op.equals("=")){arg=temp;} else if(op.equals("sin")){ arg=Math.sin(temp*(Math.PI)/180.0);arg=Number(arg); } else if(op.equals("cos")){ arg=Math.cos(temp*(Math.PI)/180.0);arg=Number(arg); } else if(op.equals("tan")){ if(temp ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: java 有声计算器
 输入关键字,在本站1000多万海量源码库中尽情搜索: