小码虫虫
2022-01-17 17:15
采纳率: 100%
浏览 51
已结题

我有一个java弹窗,里边的按钮如何触发另一个java运行,并将结果返回java弹窗里的txt区里呢

我是自学java,以前没有代码经验,哪位帮看一下,仔细说明一下最好,感激不尽!
在btn_1 加上想触发的luyin.java

弹窗代码:

package ui;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.luyin.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

public class zi extends JFrame {
/**
     * 
     */
    private static final long serialVersionUID = -7121712615598595661L;
private JPanel contentPane;



public zi() {
//设置窗口居中    
    int windowWidth = this.getWidth(); //获得窗口宽
    int windowHeight = this.getHeight();//获得窗口高
    Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包
    Dimension screenSize = kit.getScreenSize(); //获取屏幕的尺寸
    int screenWidth = screenSize.width; //获取屏幕的宽
    int screenHeight = screenSize.height; //获取屏幕的高

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setBounds(screenWidth/4-windowWidth/4, screenHeight/4-windowHeight/4, 800, 600);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);//设置布局为空布局

//设置窗口底部按钮
JButton btn_0 = new JButton("语音输入");//创建按钮组件 //$NON-NLS-1$

btn_0.setFont(new Font("System", Font.PLAIN, 24)); //$NON-NLS-1$

btn_0.setBounds(1, 520, 782, 40);//设置按钮组件的位置(1,520)和大小(782,40)

contentPane.add(btn_0);

//添加底部按钮事件
btn_0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {

    JOptionPane.showMessageDialog(btn_0,"按钮1 被点击");//按钮被点击弹出JOptionPane并显示该信息 //$NON-NLS-1$
}
});



//设置窗口左侧上部按钮
JButton btn_1 = new JButton("识别");//创建按钮组件 //$NON-NLS-1$

btn_1.setFont(new Font("System", Font.PLAIN, 14)); //$NON-NLS-1$

btn_1.setBounds(1, 10, 100, 250);//设置按钮组件的位置(1,10)和大小(100,250)

contentPane.add(btn_1);

//添加左上部按钮事件


//设置窗口左侧下部按钮
JButton btn_2 = new JButton("数据");//创建按钮组件 //$NON-NLS-1$

btn_2.setFont(new Font("System", Font.PLAIN, 14)); //$NON-NLS-1$

btn_2.setBounds(1, 270, 100, 250);//设置按钮组件的位置(1,270)和大小(100,250)

contentPane.add(btn_2);

//设置上部文本区域
JTextArea ja_1 =new JTextArea();//创建文本区
ja_1.setBounds(101, 10, 670, 250);//设置文本区的位置(101,10)和大小(670,250)
ja_1.setFont(new Font("System", Font.PLAIN, 38)); //$NON-NLS-1$
ja_1.setVisible(true);
contentPane.add(ja_1);

//设置下部文本区域
JTextArea ja_2 =new JTextArea();//创建文本区
ja_2.setBounds(101, 270, 670, 250);//设置文本区的位置(101,10)和大小(670,250)
ja_2.setFont(new Font("System", Font.PLAIN, 38)); //$NON-NLS-1$
ja_2.setVisible(true);
contentPane.add(ja_2);
}
//main
public static void main(String[] args) {
zi frame = new zi();
frame.setVisible(true);
frame.setResizable(false);
}
}

想触发的录音代码

import java.io.File;
import java.io.FileOutputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.TargetDataLine;
 
public class luyin{
    private static FileOutputStream os;
    //采样率
    private static float RATE = 8000f;
    //编码格式PCM
    private static AudioFormat.Encoding ENCODING = AudioFormat.Encoding.PCM_SIGNED;
    //帧大小 16 
    private static int SAMPLE_SIZE = 16;
    //是否大端
    private static boolean BIG_ENDIAN = false;
    //通道数
    private static int CHANNELS = 1;
 
    public static void main(String[] args) throws Exception{
        if(args.length<1) {
            save("D:\\Users\\Administrator\\eclipse-workspace\\luyin\\123.pcm");
        }else {
            save(args[0]);
        }
    }
 
    
    public static void save(String path) throws Exception {
        File file = new File(path);
        
        if(file.isDirectory()) {
            if(!file.exists()) {
                file.mkdirs();
            }
            file.createNewFile();
        }
        
        AudioFormat audioFormat = new AudioFormat(ENCODING,RATE, SAMPLE_SIZE, CHANNELS, (SAMPLE_SIZE / 8) * CHANNELS,
                RATE, BIG_ENDIAN);
        TargetDataLine targetDataLine = AudioSystem.getTargetDataLine(audioFormat);
        targetDataLine.open();
        targetDataLine.start();
        byte[] b = new byte[256];
        int flag = 0;
        os = new FileOutputStream(file);
        while((flag = targetDataLine.read(b, 0, b.length))>0) {//从声卡中采集数据
            os.write(b);
            System.out.println(flag);
        }
    }
}


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题