javaGUI写一个有语音播报的数青蛙小程序

一个好玩的小程序,源码资源放下下面了,直接下载即可

加入了语音包实现自动阅读文章(举一反三阅读小说)(>^<)

需要直接看代码的往下翻

资源下载

目录结构

  • src
    • config
      • Config
      • DLLPath
    • main
      • Main
    • Thread
      • SapThread

主函数main

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.ccqq.Thread.SapThread;
import com.ccqq.config.DLLPath;

import static com.ccqq.config.Config.*;

public class Main extends Frame{
	
	/**
	 * @author CQ
	 */
	private static final long serialVersionUID = 1L;
	
	private JLabel jl_input = null,jl_volume = null,jl_speed = null;
	private JTextField jtf_input = null;
	private JButton jb_input = null,jb_out = null;
	private JTextArea jta_out = null;
	private JScrollPane jsp_out = null;
	private JPanel jp_out = null;
	private Integer[] volume = new Integer[] {0,10,20,30,40,50,60,70,80,90,100};
	private Integer[] speed = new Integer[] {-10,-8,-6,-4,-2,0,2,4,6,8,10};
	private JComboBox<Integer> jcb_volume = null,jcb_speed = null;
	
	Main(){
		this.setTitle("数青蛙");
		this.setLayout(null);
		this.setBounds(0, 0, 500, 700);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setIconImage(logo);
		
		jl_input = new JLabel("请输入青蛙数(1~99):");
		jl_input.setBounds(10, 50, 260, 40);
		jl_input.setFont(f_20);
		this.add(jl_input);
		
		jtf_input = new JTextField();
		jtf_input.setBounds( 270, 50, 120, 40);
		jtf_input.setFont(f_20);
		this.add(jtf_input);
		
		jb_input = new JButton("确定");
		jb_input.setBounds(400, 50, 90, 40);
		jb_input.setFont(f_20);
		this.add(jb_input);
		
		jb_out = new JButton("语音播报");
		jb_out.setBounds(360, 100, 130, 40);
		jb_out.setFont(f_20);
		jb_out.setEnabled(false);
		this.add(jb_out);

		jl_volume = new JLabel("音量:");
		jl_volume.setBounds(10, 100, 60, 40);
		jl_volume.setFont(f_20);
		this.add(jl_volume);
		
		jcb_volume = new JComboBox<Integer>(volume);
		jcb_volume.setBounds(70, 100, 110, 40);
		jcb_volume.setFont(f_20);
		jcb_volume.setSelectedIndex(10);
		jcb_volume.setEnabled(false);
		this.add(jcb_volume);
		
		jl_speed = new JLabel("语速:");
		jl_speed.setBounds(180, 100, 60, 40);
		jl_speed.setFont(f_20);
		this.add(jl_speed);
		
		jcb_speed = new JComboBox<Integer>(speed);
		jcb_speed.setBounds(240, 100, 110, 40);
		jcb_speed.setFont(f_20);
		jcb_speed.setSelectedIndex(6);
		jcb_speed.setEnabled(false);
		this.add(jcb_speed);
		
		jp_out = new JPanel();
		jp_out.setLayout(new FlowLayout());
		jsp_out = new JScrollPane(jp_out);
		jsp_out.setBounds(10, 150, 480, 540);
		jp_out.setPreferredSize(new Dimension(jsp_out.getWidth()-20,(COUNT*30)));
		this.add(jsp_out);
		
		jta_out = new JTextArea();
		jta_out.setSize(460, 390);
		jta_out.setFont(f_25);
		jta_out.setLineWrap(true);
		jta_out.setEditable(false);
		jp_out.add(jta_out);
		
		jb_input.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String str = jtf_input.getText();
				if(str.length() <= 0) {
					JOptionPane.showMessageDialog(null, "内容不能为空!", "提示",JOptionPane.INFORMATION_MESSAGE);
				}else {
					int index = Integer.parseInt(str);
					if(index == 0) {
						JOptionPane.showMessageDialog(null, "内容不能是0!", "提示",JOptionPane.INFORMATION_MESSAGE);
					}else {
						showJTA(index);
					}
				}
			}
		});
		
		jb_out.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String str = jta_out.getText();
				int v = (int) jcb_volume.getSelectedItem();
				int s = (int) jcb_speed.getSelectedItem();
				System.out.println(str+"\n"+" v = "+v+"  s = "+s);
				if(str.length() > 0) {
					//内容,音量,语速
					new SapThread(str,v,s,jb_out,jcb_volume,jcb_speed).start();
				}
			}
		});
		
		//限制数字和大小 2位
		jtf_input.addKeyListener(new KeyAdapter(){
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();				
				if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
					String s = jtf_input.getText();
					if(s.length() >= 2) e.consume();
				}else{
					e.consume(); //关键,屏蔽掉非法输入
				}
			}
		});
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
		});
		this.setVisible(true);
	}
	
	protected void showJTA(int index) {
		if(jta_out != null) {
			jta_out.setText("");	//清空原本数据
			
			for(int i=1;i<=index;i++) {
				jta_out.append(i+s1+"\n");
				jta_out.append(i+s2+"\n");
				jta_out.append((2*i)+s3+"\n");
				jta_out.append((4*i)+s4+"\n");
				int p=1;
				for(int j=1;j<=i;j++) {
					jta_out.append(s5+"、");
					if(p>=5) {
						jta_out.append("\n");
						p=0;
					}
					p++;
				}
				jta_out.setText(jta_out.getText().substring(0, jta_out.getText().length()-1));
				jta_out.append(",跳下水,"+"\n");
				int q = 1;
				for(int m=1;m<=i;m++) {
					jta_out.append(s6+"、");
					if(q>=8) {
						jta_out.append("\n");
						q=0;
					}
					q++;
				}
				jta_out.setText(jta_out.getText().substring(0, jta_out.getText().length()-1));
				jta_out.append("。"+"\n");
			}
			
			//更新UI
			COUNT = jta_out.getLineCount();
			jp_out.setPreferredSize(new Dimension(jsp_out.getWidth()-20,(COUNT*32+32)));
			jb_out.setEnabled(true);
			jcb_volume.setEnabled(true);
			jcb_speed.setEnabled(true);
		}
	}

	public static void main(String[] args) {
		new DLLPath();
		new Main();
	}

}

Config.java 静态配置文件

import java.awt.Font;
import java.awt.Image;

import javax.swing.ImageIcon;

public class Config {
	public static final Image logo = new ImageIcon("src\\images\\logo.png").getImage();
	public static final Font f_20 = new Font("楷体", Font.BOLD, 20);
	public static final Font f_25 = new Font("楷体", Font.BOLD, 25);
	public static int COUNT = 1;
	
	public static final String s1 = "只青蛙,";
	public static final String s2 = "张嘴,";
	public static final String s3 = "只眼睛,";
	public static final String s4 = "条腿。";
	public static final String s5 = "扑通";
	public static final String s6 = "呱";
	
	//系统路径
	public static final String WinPath = "C:\\Windows\\System32\\";
	//名称
	public static final String JACOB1 = "jacob-1.17-M2-x64.dll";
	public static final String JACOB2 = "jacob-1.17-M2-x86.dll";
}

DLLPath 检测和移动语音包

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static com.ccqq.config.Config.*;

/*
 * 	检测和移动语音包
 * */

public class DLLPath {
	public DLLPath(){
		File f1 = new File(WinPath+JACOB1);
		File f2 = new File(WinPath+JACOB2);
		if(!f1.exists() || !f2.exists()) {
			callCmd("mount.bat");
		}
	}
	
	 private static void  callCmd(String locationCmd){
	        StringBuilder sb = new StringBuilder();
	        try {
	            Process child = Runtime.getRuntime().exec(locationCmd);
	            InputStream in = child.getInputStream();
	            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
	            String line;
	            while((line=bufferedReader.readLine())!=null)
	            {
	                sb.append(line + "\n");
	            }
	            in.close();
	            try {
	                child.waitFor();
	            } catch (InterruptedException e) {
	                System.out.println(e);
	            }
	        } catch (IOException e) {
	        	e.printStackTrace();
	            System.out.println(e);
	        }
	    }
}

SapThread 语音线程

import javax.swing.JButton;
import javax.swing.JComboBox;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class SapThread extends Thread{
	
	private String str;
	private int volume;
	private int speed;
	private JButton out;
	private JComboBox<Integer> v,s;
	
	public SapThread(String str,int volume,int speed,JButton out,JComboBox<Integer> v,JComboBox<Integer> s) {
		this.str = str;
		this.volume = volume;
		this.speed = speed;
		this.out = out;
		this.v = v;
		this.s = s;
	}
	
	@Override
	public void run() {
		super.run();
		out.setEnabled(false);
		v.setEnabled(false);
		s.setEnabled(false);
			//ִ实例化语音
			ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
			//音量1-100
			sap.setProperty("Volume", new Variant(volume));
			//语音朗读速度 -10 到 +10
			sap.setProperty("Rate", new Variant(speed));
			//运行时输出语音内容
			Dispatch sapo = sap.getObject();
			//执行朗读
			Dispatch.call(sapo, "Speak",new Variant(str));
			//关闭
			sapo.safeRelease();

			out.setEnabled(true);
			v.setEnabled(true);
			s.setEnabled(true);
	}
}

没用的知识又增长了,哈哈哈哈哈哈!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荒·原

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值