Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Looking for guidance and instructions

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looking for guidance and instructions

    In the book we are reading for my Java 2 class there is a section on GUI that gave me some code, but I don't know how to use it in a text editor (textpad) to make the code so it will work.
    There are 2 code sections they gave us in the book and I don' t know whether or not I am supposed to put them both into one java file or how I am supposed to do it.

    **( Oh Yeah I also have Netbeans available but am not sure how to use it either)**

    Here is the code:
    (1st code)
    import javax.swing.JFrame;
    /**
    This program displays the growth of an investment.
    */
    public class InvestmentViewer3
    {
    public static void main(String[] args)
    {
     JFrame frame = new InvestmentFrame();
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
     }
     }

    Here is the second code:
    import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.JButton;
     import javax.swing.JFrame;
     import javax.swing.JLabel;
     import javax.swing.JPanel;
     import javax.swing.JTextField;
    /**
     A frame that shows the growth of an investment with variable interest.
     */
     public class InvestmentFrame extends JFrame
     {
     private static final int FRAME_WIDTH = 450;
     private static final int FRAME_HEIGHT = 100;
     
     private static final double DEFAULT_RATE = 5;
     private static final double INITIAL_BALANCE = 1000;
     
     private JLabel rateLabel;
     private JTextField rateField;
     private JButton button;
     private JLabel resultLabel;
     private JPanel panel;
     private
    public InvestmentFrame()
     {
     account = new BankAccount(INITIAL_BALANCE);
     
     // Use instance variables for components
     resultLabel = new JLabel("balance: " + account.getBalance());
     
     // Use helper methods
     createTextField();
     createButton();
     createPanel();
     
     setSize(FRAME_WIDTH, FRAME_HEIGHT);
     }
     
     private void createTextField()
     {
     rateLabel = new JLabel("Interest Rate: ");
     
     final int FIELD_WIDTH = 10;
     rateField = new JTextField(FIELD_WIDTH);
     rateField.setText("" + DEFAULT_RATE);
     }
     
     private void createButton()
     {
     button = new JButton("Add Interest");
     
     class AddInterestListener implements ActionListener
     {
     public void actionPerformed(ActionEvent event)
     {
     double rate = Double.parseDouble(rateField.getText());
     double interest = account.getBalance() * rate / 100;
     account.deposit(interest);
     resultLabel.setText("balance: " + account.getBalance());
     }
     }
     
     ActionListener listener = new AddInterestListener();
     button.addActionListener(listener);
     }
     
     private void createPanel()
     {
     panel = new JPanel();
     panel.add(rateLabel);
     panel.add(rateField);
     panel.add(button);
     panel.add(resultLabel);
     add(panel);
     }
     }

    Thanks in davance

    COY
    Last edited by coyboss; February 10th, 2011 at 09:39 PM. Reason: added info about netbeans


  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Looking for guidance and instructions

    Is there a problem with your code or do you no know how to run it. If you don't know how to run it, have a quick search on Google, there'll be sites there with tutorials on running from command line and guides for Netbeans. Check the tutorials section on this forum as well.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,668
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Looking for guidance and instructions

    .java files need to be compiled into .class files before they become runnable by the JRE (Java Runtime Environment).

    This can be done with the command line, but it is usually best to get an IDE (Integrated Development Environment). If you have Netbeans, that can be a great tool, but if you are just making small applications and self-coding your GUIs (Graphical User Interface), I would recommend a light-weight IDE instead of Netbeans. Notepad is about as light-weight as it gets, but I would go a step above that. I hear Netbeans is great, but I think using it to make small learning programs is overkill.

    Personally, I have had tremendous success with JCreator (JCreator — Java IDE). If you download the LE version, it is free. JCreator will allow you to create, compile, and run java code without having to make projects for them (but you can still make projects if you want). Being a light-weight IDE, the LE version lacks some of the luxuries of things like Debuggers and auto-imports, but println statements can be used to debug code yourself and it is probably best to learn how to debug code without a debugger. As for the imports, it is also probably best to learn how to find a library to import it. If you need help finding libraries, I'll provide you with a simple Google search you can do.

    If you would like to use the command prompt for the time being, here is a website that might help: Java and the Windows Command Prompt
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looking for guidance and instructions

    OK let me clarify something
    I have tried to compile both files in TextPad but they both give errors.
    here are the errors for the 1st code:
    .\InvestmentFrame.java:25: illegal combination of modifiers: public and private
    public InvestmentFrame()
    ^
    .\InvestmentFrame.java:27: cannot find symbol
    symbol : variable account
    location: class InvestmentFrame
    account = new BankAccount(INITIAL_BALANCE);
    ^
    .\InvestmentFrame.java:27: cannot find symbol
    symbol : class BankAccount
    location: class InvestmentFrame
    account = new BankAccount(INITIAL_BALANCE);
    ^
    .\InvestmentFrame.java:30: cannot find symbol
    symbol : variable account
    location: class InvestmentFrame
    resultLabel = new JLabel("balance: " + account.getBalance());
    ^
    .\InvestmentFrame.java:58: cannot find symbol
    symbol : variable account
    location: class AddInterestListener
    double interest = account.getBalance() * rate / 100;
    ^
    .\InvestmentFrame.java:59: cannot find symbol
    symbol : variable account
    location: class AddInterestListener
    account.deposit(interest);
    ^
    .\InvestmentFrame.java:60: cannot find symbol
    symbol : variable account
    location: class AddInterestListener
    resultLabel.setText("balance: " + account.getBalance());

    here are the errors for the second code:
    C:\Users\welshd.psrc\Documents\gui Java\InvestmentFrame.java:25: illegal combination of modifiers: public and private
    public InvestmentFrame()
    ^
    C:\Users\welshd.psrc\Documents\gui Java\InvestmentFrame.java:27: cannot find symbol
    symbol : variable account
    location: class InvestmentFrame
    account = new BankAccount(INITIAL_BALANCE);
    ^
    C:\Users\welshd.psrc\Documents\gui Java\InvestmentFrame.java:27: cannot find symbol
    symbol : class BankAccount
    location: class InvestmentFrame
    account = new BankAccount(INITIAL_BALANCE);
    ^
    C:\Users\welshd.psrc\Documents\gui Java\InvestmentFrame.java:30: cannot find symbol
    symbol : variable account
    location: class InvestmentFrame
    resultLabel = new JLabel("balance: " + account.getBalance());
    ^
    C:\Users\welshd.psrc\Documents\gui Java\InvestmentFrame.java:58: cannot find symbol
    symbol : variable account
    location: class AddInterestListener
    double interest = account.getBalance() * rate / 100;
    ^
    C:\Users\welshd.psrc\Documents\gui Java\InvestmentFrame.java:59: cannot find symbol
    symbol : variable account
    location: class AddInterestListener
    account.deposit(interest);
    ^
    C:\Users\welshd.psrc\Documents\gui Java\InvestmentFrame.java:60: cannot find symbol
    symbol : variable account
    location: class AddInterestListener
    resultLabel.setText("balance: " + account.getBalance());

    if they won't compile how can I make them work?
    I didnt' alter any of the code from the book just C & P'd it in to TextPad.

    COY

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looking for guidance and instructions

    OK I am just going to close this thread,
    I have gotten people confused.

Similar Threads

  1. [SOLVED] Medication of ArrayDemo.java to include iterative menu
    By xyldon27 in forum Loops & Control Statements
    Replies: 8
    Last Post: June 30th, 2009, 02:10 AM