日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

设计一个莫尔斯电码电报机

發(fā)布時(shí)間:2024/3/12 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计一个莫尔斯电码电报机 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

//設(shè)計(jì)一個(gè)電報(bào)機(jī)

//要導(dǎo)入的包

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JFrame;

//界面設(shè)置,包括按鈕,面板

public class LXCON extends JFrame implements ActionListener{
?
? /**
?? *
?? */
? private static final long serialVersionUID = 1L;
? JFrame f;
? JPanel p1,p2,p3;
? JTextField t1,t2;
? Label l1= new Label("INPUT");
? Label l2=new Label("OUTPUT");
? JButton b1[]=new JButton[3];
? String b[]= {"CLEAR","SWITCH","BEEP"};
? public LXCON(){
????? f=new JFrame("摩爾斯電碼轉(zhuǎn)換器V1.1");
????? t1=new JTextField(35);
????? t2=new JTextField(35);
????? p1=new JPanel();
????? p2=new JPanel();
????? p3=new JPanel();
????? f.setBounds(400, 200, 500,300);
??? ?
??? ?

????? f.add(p1,BorderLayout.NORTH);
????? f.add(p2,BorderLayout.SOUTH);
????? f.add(p3,BorderLayout.CENTER);
????? p3.setLayout(new GridLayout(1,3));
???? ?
????? p1.add(t1);
????? p1.add(l1);
????? p2.add(t2);
????? p2.add(l2);
????? for(int i=0;i<3;i++) {
????????? b1[i]=new JButton(b[i]);
????????? p3.add(b1[i]);
????????? b1[i].addActionListener(this);
????? }
??? ?
?
//??? ?
????? f.setVisible(true);
? }

?

?

public void actionPerformed(ActionEvent e) {?????? ?
?

????? if(e.getSource()==b1[0]) {
??????? t2.setText("");
??????? t1.setText("");
??????? }
????? else if (e.getSource()==b1[1]) {
????????? t2.setText(mo(t1.getText()));
????? }
????? else if (e.getSource()==b1[2]) {
????????? try {
????????????? beee(t2.getText());
????????? } catch (InterruptedException e1) {
????????????? // TODO Auto-generated catch block
????????????? e1.printStackTrace();
????????? }
????? }
???? }

?

public static String mosc (String toEncode)

{

String morse = toEncode;

if (toEncode.equalsIgnoreCase("a"))

morse = ".-";

if (toEncode.equalsIgnoreCase("b"))

morse = "-...";

if (toEncode.equalsIgnoreCase("c"))

morse = "-.-.";

if (toEncode.equalsIgnoreCase("d"))

morse = "-..";

if (toEncode.equalsIgnoreCase("e"))

morse = ".";

if (toEncode.equalsIgnoreCase("f"))

morse = "..-.";

if (toEncode.equalsIgnoreCase("g"))

morse = "--.";

if (toEncode.equalsIgnoreCase("h"))

morse = "....";

if (toEncode.equalsIgnoreCase("i"))

morse = "..";

if (toEncode.equalsIgnoreCase("j"))

morse = ".---";

if (toEncode.equalsIgnoreCase("k"))

morse = "-.-";

if (toEncode.equalsIgnoreCase("l"))

morse = ".-..";

if (toEncode.equalsIgnoreCase("m"))

morse = "--";

if (toEncode.equalsIgnoreCase("n"))

morse = "-.";

if (toEncode.equalsIgnoreCase("o"))

morse = "---";

if (toEncode.equalsIgnoreCase("p"))

morse = ".--.";

if (toEncode.equalsIgnoreCase("q"))

morse = "--.-";

if (toEncode.equalsIgnoreCase("r"))

morse = ".-.";

if (toEncode.equalsIgnoreCase("s"))

morse = "...";

if (toEncode.equalsIgnoreCase("t"))

morse = "-";

if (toEncode.equalsIgnoreCase("u"))

morse = "..-";

if (toEncode.equalsIgnoreCase("v"))

morse = "...-";

if (toEncode.equalsIgnoreCase("w"))

morse = ".--";

if (toEncode.equalsIgnoreCase("x"))

morse = "-..-";

if (toEncode.equalsIgnoreCase("y"))

morse = "-.--";

if (toEncode.equalsIgnoreCase("z"))

morse = "--..";

if (toEncode.equalsIgnoreCase("0"))

morse = "-----";

if (toEncode.equalsIgnoreCase("1"))

morse = ".----";

if (toEncode.equalsIgnoreCase("2"))

morse = "..---";

if (toEncode.equalsIgnoreCase("3"))

morse = "...--";

if (toEncode.equalsIgnoreCase("4"))

morse = "....-";

if (toEncode.equalsIgnoreCase("5"))

morse = ".....";

if (toEncode.equalsIgnoreCase("6"))

morse = "-....";

if (toEncode.equalsIgnoreCase("7"))

morse = "--...";

if (toEncode.equalsIgnoreCase("8"))

morse = "---..";

if (toEncode.equalsIgnoreCase("9"))

morse = "----.";

if (toEncode.equalsIgnoreCase("."))

morse = ".-.-";

if (toEncode.equalsIgnoreCase(","))

morse = "--..--";

if (toEncode.equalsIgnoreCase("?"))

morse = "..--..";

return morse;

}

?

//我們輸入的是字符串,所以需要利用上面的函數(shù)將字符串整體進(jìn)行轉(zhuǎn)換

public static String mo(String moo) {
????? String? mo1="";
????? for (int i = 0; i < moo.length(); i++) {
????????? mo1+=mosc(Character.toString(moo.charAt(i)));
????? }
???? ?
????? return mo1;
???? ?
? }

//再加一個(gè)打嗶嗶聲的功能,就是把莫爾斯電碼打出來

@SuppressWarnings("deprecation")
public static class Player extends JFrame{
? /**
?? *
?? */
?
? public static void bf(){
????? try {
????????? URL cb;
????????? File f = new File("C:/Users/86180/Desktop/beep_1.wav"); //引號(hào)里面的是音樂文件所在的絕對(duì)路徑
????????? cb = f.toURL();
????????? AudioClip aau;
????????? aau = Applet.newAudioClip(cb);//加載音頻
????????? aau.play(); //播放音頻
????????? } catch (MalformedURLException e) {
????????????????? e.printStackTrace();
????????? }
? }
}

//將莫斯碼單個(gè)字符分析,調(diào)用上面的函數(shù)發(fā)聲

public static void beee(String cod) throws InterruptedException {
? for (int i = 0; i < cod.length(); i++) {
????? if(cod.charAt(i)=='.') {
???????? ?
????????? System.out.println("beep");
????????? Player.bf();
????????? Thread.currentThread().sleep(100);
????????? }
????????? else if (cod.charAt(i)=='-') {
????????????? Thread.currentThread().sleep(300);
????????? }
?????????? ?
???? ?
? }
}

//主函數(shù)
? public static void main(String[] args) {
????? new? LXCON();
? }

}

?

這樣就完成了,試試效果如何

總結(jié)

以上是生活随笔為你收集整理的设计一个莫尔斯电码电报机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。