This project is similar to, but not exactly like, my phonetic project. I am having to construct a GUI and have done so. I have been able to get everything except one part.
What I need it to do is:
1) User inputs a string (this part works)
2) When user clicks button "Code It" it needs to convert the string into Morse code. (this part is what isn't working)
I don't know how to set it up to do it. I'm trying to use an array to do it.
What it does:
1) Takes user's input
2) When user clicks button "Code It" it just displays the string they entered.
Any ideas and help will be appreciated.
package morsecode;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MorseCode
{
public static class morseCode implements ActionListener
{
JFrame frame;
JPanel contentPane;
JTextField words;
JButton CodeIt;
JLabel enterString, coded;
public morseCode()
{
// Create and set up the frame
frame = new JFrame("Morse Code");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a content pane with a BoxLayout
// and empty borders
contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 100));
contentPane.setLayout(new GridLayout(4,2,5,10));
// Create a text field and a descriptive label
enterString = new JLabel("Enter string: ");
contentPane.add(enterString);
words = new JTextField(50);
contentPane.add(words);
// Create a display morseCode button
CodeIt = new JButton("Code It");
CodeIt.addActionListener(this);
contentPane.add(CodeIt);
// Create a label that will display text in morse code
coded = new JLabel(" ");
contentPane.add(coded);
// Add content pane to frame
frame.setContentPane(contentPane);
// Size and then diplay the frame
frame.pack();
frame.setVisible(true);
}
//////////// I think this is the section where I need to put my array
//////////// set up since it retrieves the user input and I believe
//////////// displays it
public void actionPerformed(ActionEvent event)
{
String text = words.getText();
// char[] stringArray = text.toCharArray();
// String[] morse = {".- ","-... ","-.-. ","-.. ",". ","..-. ","--. ",".... ",".. ",".--- ","-.- ",".-.. ","-- ","-. ","--- ",".--. ","--.- ",".-. ","... ","- ","..- ","...- ",".-- ","-..- ","-.-- ","--.. "};
coded.setText(text);
}
}
private static void runGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
morseCode greeting = new morseCode();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
runGUI();
}
});
}
}