首页编程java编程java可以做什么小程序?用java做一个可视化小程序,可以录音并予以保存。

java可以做什么小程序?用java做一个可视化小程序,可以录音并予以保存。

编程之家2023-10-12103次浏览

大家好,今天来为大家分享java可以做什么小程序的一些知识点,和用java做一个可视化小程序,可以录音并予以保存。的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!

java可以做什么小程序?用java做一个可视化小程序,可以录音并予以保存。

Java的应用领域有哪些Java语言能做什么

只要把它的应用领域有很多,比如说网页开发呀,还有程序的开发啊,像之前基本上都是用Java开发的,因为他是一个边解释边运行的一个程序语言,而不像星c语言那样,编译完成以后再运行,所以说他可能虽然说执行效率高,但是可能他占用了一些内存会比较大,但是这个不同的语言都有不同的优势和弱点,这个要知道。

java:编写一个计算器小程序,要求可以做加减乘除运算

import javax.swing.*;

import java.awt.*;

java可以做什么小程序?用java做一个可视化小程序,可以录音并予以保存。

import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener{

private static final long serialVersionUID= 8199443193151152362L;

java可以做什么小程序?用java做一个可视化小程序,可以录音并予以保存。

private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),

bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),

bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),

bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");

JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,

bto_0,bto_dian,bto_deng,bto_jia};

private JTextField text_double;//= new JTextField("0");

private String operator="=";//当前运算的运算符

private boolean firstDigit= true;//标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字

private double resultNum= 0.0;//计算的中间结果

private boolean operateValidFlag= true;//判断操作是否合法

public Calculator()

{

super("Calculator");

this.setBounds(300, 300, 300, 300);

this.setResizable(false);

this.setBackground(Color.orange);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.getContentPane().setLayout(new BorderLayout());//设置布局

text_double=new JTextField("0",20);//设置文本区

text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐

this.getContentPane().add(text_double,BorderLayout.NORTH);//将文本区添加到Content北部

JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局

this.getContentPane().add(panel);//添加panel面板

for(int i=0;i<button.length;i++)//在面板上添加按钮

panel.add(button[i]);

for(int i=0;i<button.length;i++)

button[i].addActionListener(this);//为按钮注册

text_double.setEditable(false);//文本框不可编辑

text_double.addActionListener(this);//

this.setVisible(true);

}

public void actionPerformed(ActionEvent e)//

{

String c= e.getActionCommand();//返回与此动作相关的命令字符串。

System.out.println("##########command is"+c);

if(c.equals("C")){

handleC();//用户按了“C”键

}

else if(c.equals("CE"))//用户按了"CE"键

{

text_double.setText("0");

}

else if("0123456789.".indexOf(c)>= 0)//用户按了数字键或者小数点键

{

handleNumber(c);// handlezero(zero);

} else//用户按了运算符键

{

handleOperator(c);

}

}

private void handleC()//初始化计算器的各种值

{

text_double.setText("0");

firstDigit= true;

operator="=";

}

private void handleNumber(String button){

if(firstDigit)//输入的第一个数字

{

text_double.setText(button);

} else if((button.equals("."))&&(text_double.getText().indexOf(".")< 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面

//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回-1

{

text_double.setText(text_double.getText()+".");

} else if(!button.equals("."))//如果输入的不是小数点,则将数字附在结果文本框的后面

{

text_double.setText(text_double.getText()+ button);

}

//以后输入的肯定不是第一个数字了

firstDigit= false;

}

private void handleOperator(String button){

if(operator.equals("/")){

//除法运算

//如果当前结果文本框中的值等于0

if(getNumberFromText()== 0.0){

//操作不合法

operateValidFlag= false;

text_double.setText("除数不能为零");

} else{

resultNum/= getNumberFromText();

}

} else if(operator.equals("+")){

//加法运算

resultNum+= getNumberFromText();

} else if(operator.equals("-")){

//减法运算

resultNum-= getNumberFromText();

} else if(operator.equals("*")){

//乘法运算

resultNum*= getNumberFromText();

} else if(operator.equals("sqrt")){

//平方根运算

if(getNumberFromText()<0){

operateValidFlag= false;

text_double.setText("被开方数不能为负数");}

else

resultNum= Math.sqrt(resultNum);

}

else if(operator.equals("+/-")){

//正数负数运算

resultNum= resultNum*(-1);

} else if(operator.equals("=")){

//赋值运算

resultNum= getNumberFromText();

}

if(operateValidFlag){

//双精度浮点数的运算

long t1;

double t2;

t1=(long) resultNum;

t2= resultNum- t1;

if(t2== 0){

text_double.setText(String.valueOf(t1));

} else{

text_double.setText(String.valueOf(resultNum));

}

}

operator= button;//运算符等于用户按的按钮

firstDigit= true;

operateValidFlag= true;

}

private double getNumberFromText()//从结果的文本框获取数字

{

double result= 0;

try{

result= Double.valueOf(text_double.getText()).doubleValue();// ValueOf()返回表示指定的 double值的 Double实例

} catch(NumberFormatException e){

}

return result;

}

public static void main(final String[] args){

new Calculator();

}

}

用java做一个可视化小程序,可以录音并予以保存。

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.InputStream;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.events.*;

import javax.sound.sampled.AudioFileFormat;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.SourceDataLine;

import javax.sound.sampled.TargetDataLine;

public class RecordPlay{

boolean stopCapture= false;//控制录音标志

AudioFormat audioFormat;//录音格式

//读取数据:从TargetDataLine写入ByteArrayOutputStream录音

ByteArrayOutputStream byteArrayOutputStream;

int totaldatasize= 0;

TargetDataLine targetDataLine;

//播放数据:从AudioInputStream写入SourceDataLine播放

AudioInputStream audioInputStream;

SourceDataLine sourceDataLine;

private Button captureBtn;

private Button stopBtn;

private Button playBtn;

private Button saveBtn;

private Label myLabel;

private Shell shell;

private Display display;

public RecordPlay(){

super();

display= new Display();

shell= new Shell(display);

shell.setSize(350, 150);

shell.setText("录音机程序");

//

myLabel= new Label(shell, SWT.NONE);

myLabel.setBounds(38, 21, 100, 18);

myLabel.setText("录音机");

//创建按钮

captureBtn= new Button(shell, SWT.NONE);

captureBtn.setBounds(30, 61, 60, 18);

captureBtn.setText("录音");

captureBtn.setEnabled(true);

stopBtn= new Button(shell, SWT.NONE);

stopBtn.setBounds(100, 61, 60, 18);

stopBtn.setText("停止");

stopBtn.setEnabled(false);

playBtn= new Button(shell, SWT.NONE);

playBtn.setBounds(170, 61, 60, 18);

playBtn.setText("播放");

playBtn.setEnabled(false);

saveBtn= new Button(shell, SWT.NONE);

saveBtn.setBounds(240, 61, 60, 18);

saveBtn.setText("保存");

saveBtn.setEnabled(false);

//注册录音事件

captureBtn.addSelectionListener(new SelectionListener(){

public void widgetSelected(SelectionEvent event){

captureBtn.setEnabled(false);

stopBtn.setEnabled(true);

playBtn.setEnabled(false);

saveBtn.setEnabled(false);

//开始录音

capture();

}

public void widgetDefaultSelected(SelectionEvent event){

// text.setText("No worries!");

}

});

//注册停止事件

stopBtn.addSelectionListener(new SelectionListener(){

public void widgetSelected(SelectionEvent event){

captureBtn.setEnabled(true);

stopBtn.setEnabled(false);

playBtn.setEnabled(true);

saveBtn.setEnabled(true);

//停止录音

stop();

}

public void widgetDefaultSelected(SelectionEvent event){

// text.setText("No worries!");

}

});

//注册播放事件

playBtn.addSelectionListener(new SelectionListener(){

public void widgetSelected(SelectionEvent event){

//播放录音

play();

}

public void widgetDefaultSelected(SelectionEvent event){

// text.setText("No worries!");

}

});

//注册保存事件

saveBtn.addSelectionListener(new SelectionListener(){

public void widgetSelected(SelectionEvent event){

//保存录音

save();

}

public void widgetDefaultSelected(SelectionEvent event){

// text.setText("No worries!");

}

});

}

public void start(){

shell.open();

while(!shell.isDisposed()){

if(!display.readAndDispatch()){

display.sleep();

}

}

}

public static void main(String[] args){

RecordPlay label= new RecordPlay();

label.start();

}

//(1)录音事件,保存到ByteArrayOutputStream中

private void capture(){

try{

//打开录音

audioFormat= getAudioFormat();

DataLine.Info dataLineInfo= new DataLine.Info(

TargetDataLine.class, audioFormat);

targetDataLine=(TargetDataLine) AudioSystem.getLine(dataLineInfo);

targetDataLine.open(audioFormat);

targetDataLine.start();

//创建独立线程进行录音

Thread captureThread= new Thread(new CaptureThread());

captureThread.start();

} catch(Exception e){

e.printStackTrace();

System.exit(0);

}

}

//(2)播放ByteArrayOutputStream中的数据

private void play(){

try{

//取得录音数据

byte audioData[]= byteArrayOutputStream.toByteArray();

//转换成输入流

InputStream byteArrayInputStream= new ByteArrayInputStream(

audioData);

AudioFormat audioFormat= getAudioFormat();

audioInputStream= new AudioInputStream(byteArrayInputStream,

audioFormat, audioData.length/ audioFormat.getFrameSize());

DataLine.Info dataLineInfo= new DataLine.Info(

SourceDataLine.class, audioFormat);

sourceDataLine=(SourceDataLine) AudioSystem.getLine(dataLineInfo);

sourceDataLine.open(audioFormat);

sourceDataLine.start();

//创建独立线程进行播放

Thread playThread= new Thread(new PlayThread());

playThread.start();

} catch(Exception e){

e.printStackTrace();

System.exit(0);

}

}

//(3)停止录音

public void stop(){

stopCapture= true;

}

//(4)保存文件

public void save(){

//取得录音输入流

AudioFormat audioFormat= getAudioFormat();

byte audioData[]= byteArrayOutputStream.toByteArray();

InputStream byteArrayInputStream= new ByteArrayInputStream(audioData);

audioInputStream= new AudioInputStream(byteArrayInputStream,

audioFormat, audioData.length/ audioFormat.getFrameSize());

//写入文件

try{

File file= new File("d:/myjava/test.wav");

AudioSystem

.write(audioInputStream, AudioFileFormat.Type.WAVE, file);

} catch(Exception e){

e.printStackTrace();

}

}

//取得AudioFormat

private AudioFormat getAudioFormat(){

float sampleRate= 16000.0F;

// 8000,11025,16000,22050,44100

int sampleSizeInBits= 16;

// 8,16

int channels= 1;

// 1,2

boolean signed= true;

// true,false

boolean bigEndian= false;

// true,false

return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,

bigEndian);

}

class PlayThread extends Thread{

byte tempBuffer[]= new byte[10000];

public void run(){

try{

int cnt;

//读取数据到缓存数据

while((cnt= audioInputStream.read(tempBuffer, 0,

tempBuffer.length))!=-1){

if(cnt> 0){

//写入缓存数据

sourceDataLine.write(tempBuffer, 0, cnt);

}

}

// Block等待临时数据被输出为空

sourceDataLine.drain();

sourceDataLine.close();

} catch(Exception e){

e.printStackTrace();

System.exit(0);

}

}

}

class CaptureThread extends Thread{

//临时数组

byte tempBuffer[]= new byte[10000];

public void run(){

byteArrayOutputStream= new ByteArrayOutputStream();

totaldatasize= 0;

stopCapture= false;

try{//循环执行,直到按下停止录音按钮

while(!stopCapture){

//读取10000个数据

int cnt= targetDataLine.read(tempBuffer, 0,

tempBuffer.length);

if(cnt> 0){

//保存该数据

byteArrayOutputStream.write(tempBuffer, 0, cnt);

totaldatasize+= cnt;

}

}

byteArrayOutputStream.close();

} catch(Exception e){

e.printStackTrace();

System.exit(0);

}

}

}

}

关于本次java可以做什么小程序和用java做一个可视化小程序,可以录音并予以保存。的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。

道家代表人物 道家代表人物及代表作java中什么事键值对(关于java web的键值对)