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

//设计一个电报机

//要导入的包

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();
  }

}

 

这样就完成了,试试效果如何设计一个莫尔斯电码电报机_第1张图片

你可能感兴趣的:(swing,java)