| | |
working with Java GUI
![]() |
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.
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.
Java Syntax (Toggle Plain Text)
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(); } }); } }
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
0
#2 21 Days Ago
What java version are you using?
If you are using 1.5 for example then you can map the letters with the morse code using a hashmap:
Then you can loop through the array and use the char[i] as the key to get the String value from the map.
If you are using 1.5 for example then you can map the letters with the morse code using a hashmap:
Java Syntax (Toggle Plain Text)
HashMap<Character,String> map = HashMap<Character,String>(26); map.put('a', ".-"); map.put('b', "-..."); ... String text = words.getText().toLowerCase(); char[] stringArray = text.toCharArray();
Then you can loop through the array and use the char[i] as the key to get the String value from the map.
Java Syntax (Toggle Plain Text)
String result=""; ... result += map.get(stringArray[i]); ...
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Help - Basic Java GUI problem (Java)
- Java GUI Creation (Java)
- Java GUI Run Error (Java)
- Hello! Team working on a java project for university (Java)
- Help with placing images in Java GUI (Java)
- Delete button not working on my Java GUI Inventory (Java)
- Java IDE(s) (Java)
- Java / GUI Developer Needed (UK) (Software Development Job Offers)
- java gui libraries (Java)
- two Java GUI problems - please help (Java)
Other Threads in the Java Forum
- Previous Thread: HELP!!!!!
- Next Thread: Help with hw...
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






