473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ghost in the code

Here's a weird one. I compile and run this code, and get Bob Jones as the account holder, and a Withdrawel <sic> JTextField where there should not be one. Anybody help me on this one?



Expand|Select|Wrap|Line Numbers
  1.  
  2. // Exercise 4.16: AccountInformation.java
  3. // This application inputs and outputs account information.
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7. import javax.swing.border.*;
  8.  
  9. public class AccountInformation extends JFrame
  10. {
  11.    // JPanel to group deposit components
  12.    private JPanel enterJPanel;
  13.  
  14.    // JLabel and JTextField for deposits
  15.    private JLabel depositJLabel;
  16.    private JTextField depositJTextField;
  17.  
  18.    // JButton to initiate balance calculation
  19.    private JButton enterJButton;
  20.  
  21.    // JPanel to group account information components
  22.    private JPanel accountJPanel;
  23.  
  24.    // JLabel and JTextField for account holder's name
  25.    private JLabel nameJLabel;
  26.    private JTextField nameJTextField;
  27.  
  28.    // JLabel and JTextField for account number
  29.    private JLabel accountNumberJLabel;
  30.    private JTextField accountNumberJTextField;
  31.  
  32.    // JLabel and JTextField for balance
  33.    private JLabel balanceJLabel;
  34.    private JTextField balanceJTextField;
  35.  
  36.    // no-argument constructor
  37.    public AccountInformation() 
  38.    {
  39.       createUserInterface();
  40.    }
  41.  
  42.    // create and position GUI components; register event handlers
  43.    private void createUserInterface()
  44.    {
  45.       // get content pane and set layout to null
  46.       Container contentPane = getContentPane();
  47.       contentPane.setLayout( null );
  48.  
  49.       // set up enterJPanel
  50.       enterJPanel = new JPanel();
  51.       enterJPanel.setLayout( null );
  52.       enterJPanel.setBounds( 16, 16, 152, 126 );
  53.       enterJPanel.setBorder( 
  54.          new TitledBorder( "Enter information" ) );
  55.       contentPane.add( enterJPanel );
  56.  
  57.       // set up depositJLabel
  58.       depositJLabel = new JLabel();
  59.       depositJLabel.setText( "Deposit amount:" );
  60.       depositJLabel.setBounds( 16, 40, 140, 16 );
  61.       enterJPanel.add( depositJLabel );
  62.  
  63.       // set up depositJTextField
  64.       depositJTextField = new JTextField();
  65.       depositJTextField.setText( "0" );
  66.       depositJTextField.setBounds( 16, 56, 120, 21 );
  67.       depositJTextField.setHorizontalAlignment( JTextField.RIGHT );
  68.       enterJPanel.add( depositJTextField );
  69.  
  70.       // set up enterJButton
  71.       enterJButton = new JButton();
  72.       enterJButton.setText( "Enter" );
  73.       enterJButton.setBounds( 16, 150, 152, 34 );
  74.       contentPane.add( enterJButton );
  75.       enterJButton.addActionListener(
  76.  
  77.          new ActionListener() // anonymous inner class
  78.          {
  79.             // event handler called when enterJButton is pressed
  80.             public void actionPerformed( ActionEvent event )
  81.             {
  82.                enterJButtonActionPerformed( event );
  83.             }
  84.  
  85.          } // end anonymous inner class
  86.  
  87.       ); // end call to addActionListener
  88.  
  89.       // set up accountJPanel
  90.       accountJPanel = new JPanel();
  91.       accountJPanel.setLayout( null );
  92.       accountJPanel.setBounds( 180, 16, 136, 170 );
  93.       accountJPanel.setBorder( 
  94.          new TitledBorder( "Account information" ) );
  95.       contentPane.add( accountJPanel );
  96.  
  97.       // set up nameJLabel
  98.       nameJLabel = new JLabel();
  99.       nameJLabel.setText( "Name:" );
  100.       nameJLabel.setBounds( 16, 24, 100, 16 );
  101.       accountJPanel.add( nameJLabel );
  102.  
  103.       // set up nameJTextField
  104.       nameJTextField = new JTextField();
  105.       nameJTextField.setText( "Sue Purple" );
  106.       nameJTextField.setBounds( 16, 40, 104, 21 );
  107.       nameJTextField.setEditable( false );
  108.       accountJPanel.add( nameJTextField );
  109.  
  110.       // set up accountNumberJLabel
  111.       accountNumberJLabel = new JLabel();
  112.       accountNumberJLabel.setText( "Account Number:" );
  113.       accountNumberJLabel.setBounds( 16, 70, 140, 16 );
  114.       accountJPanel.add( accountNumberJLabel );
  115.  
  116.       // set up accountNumberJTextField
  117.       accountNumberJTextField = new JTextField();
  118.       accountNumberJTextField.setText( "12345" );
  119.       accountNumberJTextField.setBounds( 16, 86, 104, 21 );
  120.       accountNumberJTextField.setEditable( false );
  121.       accountNumberJTextField.setHorizontalAlignment( 
  122.          JTextField.RIGHT );
  123.       accountJPanel.add( accountNumberJTextField );
  124.  
  125.       // set up balanceJLabel
  126.       balanceJLabel = new JLabel();
  127.       balanceJLabel.setText( "Balance:" );
  128.       balanceJLabel.setBounds( 16, 116, 100, 16 );
  129.       accountJPanel.add( balanceJLabel );
  130.  
  131.       // set up balanceJTextField
  132.       balanceJTextField = new JTextField();
  133.       balanceJTextField.setText( "0" );
  134.       balanceJTextField.setBounds( 16, 132, 104, 21 );
  135.       balanceJTextField.setEditable( false );
  136.       balanceJTextField.setHorizontalAlignment( JTextField.RIGHT );
  137.       accountJPanel.add( balanceJTextField );
  138.  
  139.       // set properties of application's window
  140.       setTitle( "Account Information" ); // set title bar text
  141.       setSize( 325, 225 );               // set window size
  142.       setVisible( true );                // display window
  143.  
  144.    } // end method createUserInterface
  145.  
  146.    // method called when enterJButton is pressed
  147.    private void enterJButtonActionPerformed( ActionEvent event )
  148.    {
  149.       // display new balance
  150.       balanceJTextField.setText( String.valueOf( 
  151.          Integer.parseInt( depositJTextField.getText() ) - 
  152.          Integer.parseInt( withdrawelJTextField.getText() ) ) );
  153.  
  154.       // clear depositJTextField
  155.       depositJTextField.setText( "0" );    
  156.  
  157.    } // end method enterJButtonActionPerformed
  158.  
  159.    // main method
  160.    public static void main( String[] args ) 
  161.    {
  162.       AccountInformation application = new AccountInformation();
  163.       application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  164.  
  165.    } // end method main
  166.  
  167. } // end class AccountInformation
  168.  
  169.  


I just can't figure this out. I sent in the code to my teacher, and he says he can't figure it out, either. How can the text, "Bob Jones", appear, when it does not exist anywhere in the code?

Flummoxed.
Oct 18 '07 #1
4 1446
r035198x
13,262 8TB
What is suprising me now is that on 151 you have

Expand|Select|Wrap|Line Numbers
  1.  ...Integer.parseInt( withdrawelJTextField.getText() ) ) );
and yet you have not defined a variable called withdrawelJTextField anywhere in your code.
Are you sure this compiles?
Oct 18 '07 #2
I'm sorry. I guess it does not compile; it gives me that error. But it still runs the weird output. Could this be the reason? You can see on line 104 that the nameJTextField is set to "Sue Purple", and yet this text does not appear anywhere in the output. Instead, the text "Bob Jones" appears. Also, there is no code which sets up a withdrawelJLabel, yet this label appears in the output. I guess the first thing to do is to go ahead and code a withdrawelJLabel, as well as a withdrawelJTextField. But I'm still at a loss to figure out this screwy output. If I could figure out how to post an image in a reply, I would post the output. I guess I should have posted the outputted image in the first place.....
Oct 18 '07 #3
r035198x
13,262 8TB
I'm sorry. I guess it does not compile; it gives me that error. But it still runs the weird output. Could this be the reason? You can see on line 104 that the nameJTextField is set to "Sue Purple", and yet this text does not appear anywhere in the output. Instead, the text "Bob Jones" appears. Also, there is no code which sets up a withdrawelJLabel, yet this label appears in the output. I guess the first thing to do is to go ahead and code a withdrawelJLabel, as well as a withdrawelJTextField. But I'm still at a loss to figure out this screwy output. If I could figure out how to post an image in a reply, I would post the output. I guess I should have posted the outputted image in the first place.....
If the code does not compile, then a class file won't be created and you can't run the program. So which program is giving you the strange output?
Oct 18 '07 #4
JosAH
11,448 Expert 8TB
I guess the code that is running is not the code we're seeing here.

kind regards,

Jos
Oct 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Roger Shrubber | last post by:
I have a page with images that the user can drag from one frame to another. I need them to see a "ghost image" of the image they are dragging, while the original stays put. I use the onmousemove...
2
by: otisim | last post by:
I have a test environment that we rebuild servers on a regular basis. To streamline the process we use ghost. We will be installing SQL on the servers and want to build a ghost image with that...
2
by: Peter Flickinger | last post by:
I have been using a label with the TreeView control - to simulate the ghost image of a node during drag/drop (so that a ghost of the node appears to be floating next to the cursor on a drag/drop)....
2
by: Peteroid | last post by:
When the application I'm working on is run it creates a panel with a Paint event customized to draw primitives (circles, rectangles, etc.), places the panel on a form, and then launches the form....
1
by: Tim Marshall | last post by:
Does anyone else get "ghost" VB editor windows appear on their computers when developing in Access 2003? These are VB editor windows with no menu items and a grey empty background. I usually...
1
by: Howard Kaikow | last post by:
It was my expectation that a sub using the usual recursive FindFirstFile/FindNextFile would be able to walk the directory tree on any logical volume. I am finding that this may not be the case...
0
by: bbcrock | last post by:
I do not have a link to point you to at this time, sorry. It's an internal application. I have some JS code that hides the current div, sets the tabnumber passed in as the current tab number,...
1
by: bbcrock | last post by:
I do not have a link to point you to at this time, sorry. It's an internal application. I have some JS code that hides the current div (group1Tab1), sets the tabnumber passed in as the current...
2
by: Geolas | last post by:
Hi, new to this community.I am not really sure if this was the right place, because this community seem to be for developers really. Well my question is, as the topic states, how to create a...
2
by: buntybutt | last post by:
Hi!!! I have the windows Xp proffesional service pack 1 Symentec Ghost edition. I want the service pack 2 Ghost edition. Help me to get URL. Thank you very...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.