java小应用程序_Java小应用程序

这个博客介绍了一个用Java编写的JApplet小应用程序,它结合了简单的计算器功能和音频播放功能。用户可以选择加减乘除、求余数、倒数和平方根等运算,并播放音频文件。程序还包含了错误处理,当输入格式有误时会给出提示。
摘要由CSDN通过智能技术生成

Java小应用程序:

ba80d7b3703860e268c4c8f5893414ac.png

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值