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)
Code java:
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:
Code java:
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
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.
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
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
Re: Looking for guidance and instructions
OK I am just going to close this thread,
I have gotten people confused.