设计一个莫尔斯电码电报机
//設計一個電報機
//要導入的包
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;
//界面設置,包括按鈕,面板
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("摩爾斯電碼轉換器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;
}
?
//我們輸入的是字符串,所以需要利用上面的函數將字符串整體進行轉換
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;
???? ?
? }
//再加一個打嗶嗶聲的功能,就是把莫爾斯電碼打出來
@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"); //引號里面的是音樂文件所在的絕對路徑
????????? cb = f.toURL();
????????? AudioClip aau;
????????? aau = Applet.newAudioClip(cb);//加載音頻
????????? aau.play(); //播放音頻
????????? } catch (MalformedURLException e) {
????????????????? e.printStackTrace();
????????? }
? }
}
//將莫斯碼單個字符分析,調用上面的函數發聲
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);
????????? }
?????????? ?
???? ?
? }
}
//主函數
? public static void main(String[] args) {
????? new? LXCON();
? }
}
?
這樣就完成了,試試效果如何
總結
以上是生活随笔為你收集整理的设计一个莫尔斯电码电报机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正则表达式总结(待续)
- 下一篇: Servlet总结待续