I need to write a GUI application that reads in a character and displays the character's ASCII code. Guys,I need help on this.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
Here's your GUI, just add water.
Mark Phylus
Greenhorn
Joined: Mar 28, 2004
Posts: 18
posted
0
public void actionPerformed(ActionEvent ae) { //I need code for this part btn gets the character input and then converts it to ASCII code and display the ASCII code to lbl.
}
Thanks
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
read it convert it display it What problem are you having?
Mark Phylus
Greenhorn
Joined: Mar 28, 2004
Posts: 18
posted
0
can't read it can't convert it and can't display it help will be appreciated. Thanks
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
I need to write a GUI application that reads in a character and displays the character's ASCII code
If you are being truthful here, YOU need to write it. If not, then just say you want a total handout, and the thread will end.
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
posted
0
Mark, we don't do peoples homework�s around here, but if you can provide the code you have written so far, we will be more than happy to help. I see that Michael has done more than enough by posting the GUI code, at least you can work on the conversion code, by following Michael's steps, that is read-convert-display. Good Luck
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Mark Phylus
Greenhorn
Joined: Mar 28, 2004
Posts: 18
posted
0
Ok, here what I've done so far, and I have 3 errors. Struggling to fix them Will appreciate some help. Thanks.
/* My Sample Program: Displays a frame with two buttons, one textfield, and one text area.User enters a character the display btn reads it and display the character's ASCII code in the text area
*/ import javax.swing.*; import java.awt.*; import java.awt.event.*; class NiceGUI extends JFrame implements ActionListener { /** * Default frame width */ private static final int FRAME_WIDTH = 300; /** * Default frame height */ private static final int FRAME_HEIGHT = 270; /** * X coordinate of the frame default origin point */ private static final int FRAME_X_ORIGIN = 150; /** * Y coordinate of the frame default origin point */ private static final int FRAME_Y_ORIGIN = 250; /** * Default width for buttons */ private static final int BUTTON_WIDTH = 80; /** * Default height for buttons */ private static final int BUTTON_HEIGHT = 30; /** * Constant for empty string */ private static final String EMPTY_STRING = ""; /** * Constant for platform specific newline */ private static final String NEWLINE = System.getProperty("line.separator"); /** * The Swing button for Cancel */ private JButton clearButton; /** * The Swing button for OK */ private JButton addButton; /** * The JTextField for the user to enter a text */ private JTextField inputLine; /** * The JTextArea for displaying the entered text */ private JTextArea textArea; //---------------------------------- // Main method //---------------------------------- public static void main(String[] args) { NiceGUI frame = new NiceGUI(); frame.setVisible(true); }
//---------------------------------- // Constructors //---------------------------------- /** * Default constructor */ public NiceGUI(){ Container contentPane; //set the frame properties setSize (FRAME_WIDTH, FRAME_HEIGHT); setResizable (false); setTitle ("Welcome to the Program"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane( ); contentPane.setLayout( null ); contentPane.setBackground( Color.white ); //create and place two buttons on the frame addButton = new JButton("Display"); addButton.setBounds(70, 190, BUTTON_WIDTH, BUTTON_HEIGHT); contentPane.add(addButton); clearButton = new JButton("CLEAR"); clearButton.setBounds(160, 190, BUTTON_WIDTH, BUTTON_HEIGHT); contentPane.add(clearButton); //register this frame as an action listener of the two buttons clearButton.addActionListener(this); addButton.addActionListener(this); inputLine = new JTextField(); inputLine.setBounds(90, 155, 130, 25); contentPane.add(inputLine); inputLine.addActionListener(this); textArea = new JTextArea(); textArea.setBounds(50, 5, 200, 130); textArea.setBorder(BorderFactory.createLineBorder(Color.red)); //--------------------------------------- // Different types of borders; some borders effective if background color is changed // // textArea.setBorder(BorderFactory.createEtchedBorder(Color.blue, Color.gray)); // textArea.setBorder(BorderFactory.createLoweredBevelBorder()); // textArea.setBorder(BorderFactory.createRaisedBevelBorder()); // textArea.setBorder(BorderFactory.createTitledBorder("JTextArea")); // //------------------------------------- textArea.setEditable(false); contentPane.add(textArea); //---------------------------------------------------- // To add scroll bars to the text area /* textArea = new JTextArea(); JScrollPane scrollText= new JScrollPane(textArea); scrollText.setBounds(50, 5, 200, 135); scrollText.setBorder(BorderFactory.createLineBorder(Color.red)); contentPane.add(scrollText); */ //------------------------------------------------------ //register 'Exit upon closing' as a default close operation setDefaultCloseOperation( EXIT_ON_CLOSE ); } //------------------------------------------------- // Public Methods: // // void actionPerformed ( ActionEvent ) // //------------------------------------------------ /** * Standard method to respond the action event. * */ // Error message final String INVALID_INPUT_MESSAGE = "Re-enter character Please!"; //************************************************************************* public void actionPerformed(ActionEvent event) { Char c; if (event.getSource() instanceof JButton){ JButton clickedButton = (JButton) event.getSource(); if (clickedButton == addButton) {
Char c = inputLine.getText(charAt(0)); addText(inputLine.getText(c));