Java小应用程序

Java小应用程序:

package applet;

 

import java.applet.AudioClip;

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

 

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

public class AppletCalculator extends JApplet implements ItemListener,ActionListener{

    

    //author:王晓勃

    //学号:100102060122

    

    //定义程序变量和对象

    JLabel name,id,result;

    JTextField text1,text2;

    JTextField number1,number2,number3;

    double nb1,nb2,nb3;

    JComboBox jcstyle; //列表框对象

    String[] style = new String[]{"+","-","*","/","%","1/x","sqrt"};

    AudioClip au; //音乐文件对象

    Image img;

    JButton start = new JButton("播放");

    JButton stop = new JButton("停止");

    JButton jbloop = new JButton("循环");

    JButton count = new JButton("计算");

    JButton mc = new JButton("清空");

    

    //初始化init()

    public void init(){

        //初始化对象

        img = getImage(getCodeBase(),"1.jpg");

        JPanel jp1 = new JPanel(); //定义面板jp1

        name = new JLabel("姓名:");        

        id = new JLabel("学号:");

        text1 = new JTextField("王晓勃",10);

        text1.setHorizontalAlignment(JTextField.CENTER); //设置文本对齐方式

        text1.setEnabled(false); //设置文本框不可编辑

        text2 = new JTextField("100102060122",10);

        text2.setHorizontalAlignment(JTextField.CENTER);

        text2.setEnabled(false);

        //把组件加入面板

        jp1.add(name);

        jp1.add(text1);

        jp1.add(id);        

        jp1.add(text2);

          

        //定义面板jp2

        JPanel jp2 = new JPanel();

        number1 = new JTextField("",5);

        number2 = new JTextField("",5);

        number3 = new JTextField("",5);

        number1.setHorizontalAlignment(JTextField.RIGHT);

        number2.setHorizontalAlignment(JTextField.RIGHT);

        number3.setHorizontalAlignment(JTextField.RIGHT);

        result = new JLabel("=");        

        jcstyle = new JComboBox(style); //为列表框添加选项数组

        //添加事件监听器

        count.addActionListener(this);

        mc.addActionListener(this);

        jcstyle.addItemListener(this);

        jp2.add(number1);

        jp2.add(jcstyle);

        jp2.add(number2);

        jp2.add(result);

        jp2.add(number3);        

        jp2.add(count);

        jp2.add(mc);

          

        //定义面板jp3

        JPanel jp3 = new JPanel();

        au = getAudioClip(getCodeBase(),"nan.wav");

        //添加事件监听器

        start.addActionListener(this);

        stop.addActionListener(this);

        jbloop.addActionListener(this);

        jp3.add(start);

        jp3.add(jbloop);

        jp3.add(stop);

          

        //设置布局管理器

        setLayout(new BorderLayout());

        add(BorderLayout.NORTH,jp1);

        add(BorderLayout.CENTER,jp2);

        add(BorderLayout.SOUTH,jp3);

        setSize(500,200);

          

        

    }

    

    //JComboBox事件处理函数

    public void itemStateChanged(ItemEvent e){

        if(e.getItem()=="1/x"){

            number2.setText("");

            number2.setEditable(false);

        }

        if(e.getItem()=="sqrt"){

            number2.setText("");

            number2.setEditable(false);

        }

        if(e.getItem()=="+"){

            number2.setEditable(true);

        }

        if(e.getItem()=="-"){

            number2.setEditable(true);

        }

        if(e.getItem()=="*"){

            number2.setEditable(true);

        }

        if(e.getItem()=="/"){

            number2.setEditable(true);

        }

        if(e.getItem()=="%"){

            number2.setEditable(true);

        }

    }

    

    //其他事件处理函数

    public void actionPerformed(ActionEvent e){

        JButton b = (JButton)e.getSource();

          

        if(b.equals(start))

        {

            au.play();

        }

        if(b.equals(jbloop)){

            au.loop();

        }

        if(b.equals(stop)){

              

            au.stop();

        }

        if(b.equals(mc)){

            number1.setText("");

            number2.setText("");

            number3.setText("");

        }

        if(b.equals(count)){

            try{

            if(jcstyle.getSelectedItem()=="+"){

                  

                  

                nb1 = Double.parseDouble(number1.getText());

                nb2 = Double.parseDouble(number2.getText());

                nb3 = nb1 + nb2;

                number3.setText(""+nb3);

                  

                  

            }

            if(jcstyle.getSelectedItem()=="-"){

                  

                nb1 = Double.parseDouble(number1.getText());

                nb2 = Double.parseDouble(number2.getText());

                nb3 = nb1 - nb2;

                number3.setText(""+nb3);

            }

            if(jcstyle.getSelectedItem()=="*"){

                  

                nb1 = Double.parseDouble(number1.getText());

                nb2 = Double.parseDouble(number2.getText());

                nb3 = nb1 * nb2;

                number3.setText(""+nb3);

            }

            if(jcstyle.getSelectedItem()=="/"){

                  

                nb1 = Double.parseDouble(number1.getText());

                nb2 = Double.parseDouble(number2.getText());

                nb3 = nb1 / nb2;

                number3.setText(""+nb3);

            }

            if(jcstyle.getSelectedItem()=="%"){

                  

                nb1 = Double.parseDouble(number1.getText());

                nb2 = Double.parseDouble(number2.getText());

                nb3 = nb1 % nb2;

                number3.setText(""+nb3);

            }

            if(jcstyle.getSelectedItem()=="1/x"){

                  

                nb1 = Double.parseDouble(number1.getText());

                  

                  

                number3.setText(""+1/nb1);

            }

            if(jcstyle.getSelectedItem()=="sqrt"){

              

                nb1 = Double.parseDouble(number1.getText());

                  

                  

                number3.setText(""+Math.sqrt(nb1));

            }

            }

            catch(Exception ee){

                JOptionPane.showMessageDialog(null, "输入格式有误,请检查后重新输入!");

            }

              

        }

    }

    public void stop(){

        au.stop();

    }

      

      

    

 

}

posted @ 2013-06-28 20:33  虢国技酱  阅读(409)  评论(0编辑  收藏  举报