| | |
working with Java GUI
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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 Nov 5th, 2009
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 |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component csv database desktop draw ebook eclipse equation error event exception fractal game givemetehcodez graphics gui html ide image input integer intersect iphone j2me java java.xls javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions reporting rotatetext scanner screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






