Kód:
import java.awt.*; // uziv rozhrani, a graf okna
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Morse extends JFrame {
//JButton exitButton;
JLabel label;
JTextField input;
JTextArea output;
String text = "";
//String morse = "";
char index;
//finalni pole s ekvivalentnimi znaky pro a-z
public static final String[] morseAlpha = {".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...",
"-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
//finalni pole pro cislice od 0-9
public static final String[] morseNumeric = {"-----", ".----", "..---", "...--", "....-",
".....", "-....", "--...", "---..", "---."};
public Morse(){
//zavola JFrame s titulkem
super( "Konvertor Morseovi abecedy - Jiří Horálek" );
Container container = getContentPane();
container.setLayout( new FlowLayout( FlowLayout.LEFT, 10, 14 ) );
label = new JLabel( "Vložte text k převedeni a stiskněte ENTER:");
container.add( label );
input = new JTextField( 29 );
input.setFont( new Font( "Monospace", Font.PLAIN, 14 ) );
input.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ){
output.setText( "" );
String text = input.getText().toUpperCase();
for (int i = 0; i < text.length(); i++){
index = text.charAt( i );
if ( Character.isDigit( index ) ){
//vraci index - 48 ( 0=48, 9=57 )
output.append( morseNumeric[ index - 48 ] + " " );
}
else if ( Character.isLetter( index ) ){
//vraci index - 65 ( A=65 , Z=90 )
output.append( morseAlpha[ index - 65 ] + " " );
}
else if ( index == ' '){ //pokud na vstupu mezera tak vrat 3
output.append( " " ); //vrati tri mezery
}
}
output.append( "\n" );
}
}
);
container.add( input );
output = new JTextArea( 10, 48 );
output.setFont( new Font( "Monospace", Font.BOLD, 14 ) );
output.setEditable( false );
container.add( new JScrollPane( output ) );
setSize( 420, 300 );
setVisible( true );
output.setText( "" );
}
public static void main( String args[] ) {
Morse application = new Morse();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
HashTable jsem nikdy nepouzival