Solved

compiles but doesn't run?

Posted on 2006-11-03
11
194 Views
Last Modified: 2013-11-23
Everything ran great until I added a text area and a scroll pane, It compiles ok but doesn't run I feel I am close any hints as to how off these new components are(area,scroll)




import java.awt.*;  
import javax.swing.*;  
import java.awt.event.*;  
import java.text.*;  
 
public class Hw_prog1 extends JFrame {

Hw_prog1Event TRON = new Hw_prog1Event(this);  
JButton Calculate;
JButton Clear;  
JButton Exit;
JTextArea area;///////////////////////////////////New component
JScrollPane scrollPane;///////////////////////////////////New component





Panel File1  = new Panel();  
Label PrincipalLabel = new Label("$Enter Loan Amount:",Label.CENTER);   //create first row (File)      
TextField LonAmt = new TextField(10);  

Panel File2 = new Panel();  
Label RateLabel = new Label("0.0Enter Interest Rate:",Label.CENTER);   //create second row (File)      
TextField Rat = new TextField(10);  

Panel File3 = new Panel();  
Label TermLabel = new Label("Enter Term-Years:",Label.CENTER);   //create third row (File)      
TextField Trm = new TextField(10);  

Panel File4 = new Panel();  
Label PaymentLabel = new Label("Monthly Payments:",Label.CENTER);   //create forth row (File)      
TextField MthlyPymnt = new TextField(10);  

Panel File5 = new Panel();   //create fifth row (Buttons)

Panel File6 = new Panel(); ///////////////////////////////////New component  


 

   
 
//create  borders(Grid layout)  
public Hw_prog1() {  //Reference the main method
super("McBride.Financial.MortCal");  //Calling setSize  
setSize(875,130);   //set size

//name Buttons
Calculate = new JButton("Calculate");  
Clear = new JButton("New Amount");  
Exit = new JButton("Exit");  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //inform Frame to close
   
GridLayout layout =new GridLayout(1, 5, 10, 10); //calls the TextFeild & sets paramerters  
Container pane = getContentPane();  //holds the feilds in place & generates a ‚ÄòContainer‚Äô object that represents the pane
pane.setLayout(layout); // checks known parameters







PrincipalLabel.setForeground(Color.magenta);
RateLabel.setForeground(Color.blue);
TermLabel.setForeground(Color.magenta);
PaymentLabel.setForeground(Color.red);

Calculate.setForeground(Color.magenta);
Clear.setForeground(Color.blue);
Exit.setForeground(Color.red);


pane.setForeground(Color.blue);


//TRON listens for Button fuctions
Calculate.addActionListener(TRON);  
Clear.addActionListener(TRON);  
Exit.addActionListener(TRON);

//‚ÄòFlowLayout‚Äô components fall into place from left to right  
FlowLayout mgr = new FlowLayout(FlowLayout.CENTER,10, 10); //sets parameters  
File1 .setLayout(mgr); //Directs the user were to place data  
File1 .add(PrincipalLabel); //add Component  
File1 .add(LonAmt); //add Component    
pane.add(File1 );  

FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER,10, 10);  
File2.setLayout(layout2);  
File2.add(RateLabel);  
File2.add(Rat);  
pane.add(File2);  

FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER,10, 10);  
File3.setLayout(layout3);  
File3.add(TermLabel);  
File3.add(Trm);  
pane.add(File3);  

FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER,10, 10);  
MthlyPymnt.setEditable(false);  
File4.setLayout(layout4);  
File4.add(PaymentLabel);  
File4.add(MthlyPymnt);  
pane.add(File4);  

FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER,5, 5);  
File5.setLayout(layout5);  
File5.add(Calculate);  
File5.add(Clear);  
File5.add(Exit);  
pane.add(File5);

FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER);  //////////////////////////////////New component
File6.setLayout(layout6);///////////////////////////////////New component
File6.add(scrollPane);///////////////////////////////////New component
pane.add(File6);///////////////////////////////////New component

area = new JTextArea(5, 10);  // 5 rows and 10 columns///////////////////////////////////New component
scrollPane = new JScrollPane(area);  // add the text area to the scroll pane///////////////////New component



setVisible(true);  
}  

public static void main(String[] arguments) {

Hw_prog1 frame = new Hw_prog1();  


}
class Hw_prog1Event implements ActionListener {  
Hw_prog1 virtual;  

public Hw_prog1Event(Hw_prog1 in) {  
virtual=in;  
}  
public void actionPerformed(ActionEvent event) {  
String command = event.getActionCommand();  
if (event.getSource() == Calculate)  
 LaunchComputation();  
if (event.getSource() == Clear)  
reset();  
if (event.getSource() == Exit)  
System.exit(0);  
}  
void LaunchComputation() {  // sub-program to do math
 try   //when called
 {  
double LonAmt = Double.valueOf(virtual.LonAmt.getText()).doubleValue();   // Object & string, double value when Tron calls virtual
double Rat = Double.valueOf(virtual.Rat.getText()).doubleValue();  
int Trm = Integer.parseInt(virtual.Trm.getText());  
int totalmonths = (Trm*12);  
double Payment = 0;  
String monthlyPayment = new String();  
NumberFormat currency = NumberFormat.getCurrencyInstance();  
double I = (Rat/100.0/12.0);  

Payment = (LonAmt*I)/(1-Math.pow(1/(1+I),totalmonths));  
virtual.MthlyPymnt.setText(currency.format(Payment));  
} catch (NumberFormatException ex){  
}  
}  
protected void reset(){  
virtual.LonAmt.setText(null);  
virtual.Rat.setText(null);  
virtual.Trm.setText(null);  
virtual.MthlyPymnt.setText(null);  
}  
}  

 
}  
   
   

0
Comment
Question by:charliebaby
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 17870218
You shouldn't mix AWT and Swing
0
 

Author Comment

by:charliebaby
ID: 17870332
for example?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17870366
Panel, Label are AWT. JScrollPane etc, Swing
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:charliebaby
ID: 17870479
Is this better, still compiles but won't run, What do I have to do to see the area JTextArea area; and
JScrollPane scrollPane;, will close this question on next correspondence, and post new one


import java.awt.*;  
import javax.swing.*;  
import java.awt.event.*;  
import java.text.*;  
 
public class Hw_prog1 extends JFrame {

Hw_prog1Event TRON = new Hw_prog1Event(this);  
JButton Calculate;
JButton Clear;  
JButton Exit;
JTextArea area;
JScrollPane scrollPane;





Panel File1  = new Panel();  
Label PrincipalLabel = new Label("$Enter Loan Amount:",Label.CENTER);   //create first row (File)      
TextField LonAmt = new TextField(10);  

Panel File2 = new Panel();  
Label RateLabel = new Label("0.0Enter Interest Rate:",Label.CENTER);   //create second row (File)      
TextField Rat = new TextField(10);  

Panel File3 = new Panel();  
Label TermLabel = new Label("Enter Term-Years:",Label.CENTER);   //create third row (File)      
TextField Trm = new TextField(10);  

Panel File4 = new Panel();  
Label PaymentLabel = new Label("Monthly Payments:",Label.CENTER);   //create forth row (File)      
TextField MthlyPymnt = new TextField(10);  

Panel File5 = new Panel();   //create fifth row (Buttons)

Panel File6 = new Panel();  


 

   
 
//create  borders(Grid layout)  
public Hw_prog1() {  //Reference the main method
super("McBride.Financial.MortCal");  //Calling setSize  
setSize(875,130);   //set size

//name Buttons
Calculate = new JButton("Calculate");  
Clear = new JButton("New Amount");  
Exit = new JButton("Exit");  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //inform Frame to close
   
GridLayout layout =new GridLayout(1, 5, 10, 10); //calls the TextFeild & sets paramerters  
Container pane = getContentPane();  //holds the feilds in place & generates a ‚ÄòContainer‚Äô object that represents the pane
pane.setLayout(layout); // checks known parameters







PrincipalLabel.setForeground(Color.magenta);
RateLabel.setForeground(Color.blue);
TermLabel.setForeground(Color.magenta);
PaymentLabel.setForeground(Color.red);

Calculate.setForeground(Color.magenta);
Clear.setForeground(Color.blue);
Exit.setForeground(Color.red);


pane.setForeground(Color.blue);


//TRON listens for Button fuctions
Calculate.addActionListener(TRON);  
Clear.addActionListener(TRON);  
Exit.addActionListener(TRON);

//‚ÄòFlowLayout‚Äô components fall into place from left to right  
FlowLayout mgr = new FlowLayout(FlowLayout.CENTER,10, 10); //sets parameters  
File1 .setLayout(mgr); //Directs the user were to place data  
File1 .add(PrincipalLabel); //add Component  
File1 .add(LonAmt); //add Component    
pane.add(File1 );  

FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER,10, 10);  
File2.setLayout(layout2);  
File2.add(RateLabel);  
File2.add(Rat);  
pane.add(File2);  

FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER,10, 10);  
File3.setLayout(layout3);  
File3.add(TermLabel);  
File3.add(Trm);  
pane.add(File3);  

FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER,10, 10);  
MthlyPymnt.setEditable(false);  
File4.setLayout(layout4);  
File4.add(PaymentLabel);  
File4.add(MthlyPymnt);  
pane.add(File4);  

FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER,5, 5);  
File5.setLayout(layout5);  
File5.add(Calculate);  
File5.add(Clear);  
File5.add(Exit);  
pane.add(File5);

FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER);  
File6.setLayout(layout6);
File6.add(scrollPane);
pane.add(File6);

JTextArea area = new JTextArea (5, 10);  // 5 rows and 10 columns
JScrollPane scrollPane = new JScrollPane(area);  // add the text area to the scroll pane



setVisible(true);  
}  

public static void main(String[] arguments) {

Hw_prog1 frame = new Hw_prog1();  


}
class Hw_prog1Event implements ActionListener {  
Hw_prog1 virtual;  

public Hw_prog1Event(Hw_prog1 in) {  
virtual=in;  
}  
public void actionPerformed(ActionEvent event) {  
String command = event.getActionCommand();  
if (event.getSource() == Calculate)  
 LaunchComputation();  
if (event.getSource() == Clear)  
reset();  
if (event.getSource() == Exit)  
System.exit(0);  
}  
void LaunchComputation() {  // sub-program to do math
 try   //when called
 {  
double LonAmt = Double.valueOf(virtual.LonAmt.getText()).doubleValue();   // Object & string, double value when Tron calls virtual
double Rat = Double.valueOf(virtual.Rat.getText()).doubleValue();  
int Trm = Integer.parseInt(virtual.Trm.getText());  
int totalmonths = (Trm*12);  
double Payment = 0;  
String monthlyPayment = new String();  
NumberFormat currency = NumberFormat.getCurrencyInstance();  
double I = (Rat/100.0/12.0);  

Payment = (LonAmt*I)/(1-Math.pow(1/(1+I),totalmonths));  
virtual.MthlyPymnt.setText(currency.format(Payment));  
} catch (NumberFormatException ex){  
}  
}  
protected void reset(){  
virtual.LonAmt.setText(null);  
virtual.Rat.setText(null);  
virtual.Trm.setText(null);  
virtual.MthlyPymnt.setText(null);  
}  
}  

 
}  
   
   

0
 
LVL 3

Accepted Solution

by:
ForceRs earned 250 total points
ID: 17870615
File6.add(scrollPane); // scrollpane was null when it was added.  Must move:
JTextArea area = new JTextArea(5, 10); // 5 rows and 10 columns
            JScrollPane scrollPane = new JScrollPane(area); // add the text area to
                                                                                    // the scroll pane

To above
File6.add(scrollPane);

This works:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;

public class Hw_prog1      extends
                                    JFrame {

      Hw_prog1Event TRON = new Hw_prog1Event(this);

      JButton Calculate;

      JButton Clear;

      JButton Exit;

      JTextArea area;

      JScrollPane scrollPane;

      Panel File1 = new Panel();

      Label PrincipalLabel = new Label("$Enter Loan Amount:", Label.CENTER); // create
                                                                                                                  // first
                                                                                                                  // row
                                                                                                                  // (File)

      TextField LonAmt = new TextField(10);

      Panel File2 = new Panel();

      Label RateLabel = new Label("0.0Enter Interest Rate:", Label.CENTER); // create
                                                                                                                  // second
                                                                                                                  // row
                                                                                                                  // (File)

      TextField Rat = new TextField(10);

      Panel File3 = new Panel();

      Label TermLabel = new Label("Enter Term-Years:", Label.CENTER); // create
                                                                                                      // third row
                                                                                                      // (File)

      TextField Trm = new TextField(10);

      Panel File4 = new Panel();

      Label PaymentLabel = new Label("Monthly Payments:", Label.CENTER); // create
                                                                                                            // forth
                                                                                                            // row
                                                                                                            // (File)

      TextField MthlyPymnt = new TextField(10);

      Panel File5 = new Panel(); // create fifth row (Buttons)

      Panel File6 = new Panel();

      // create borders(Grid layout)
      public Hw_prog1() { // Reference the main method
            super("McBride.Financial.MortCal"); // Calling setSize
            setSize(875, 130); // set size

            // name Buttons
            Calculate = new JButton("Calculate");
            Clear = new JButton("New Amount");
            Exit = new JButton("Exit");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // inform Frame to close

            GridLayout layout = new GridLayout(1, 5, 10, 10); // calls the
                                                                                          // TextFeild & sets
                                                                                          // paramerters
            Container pane = getContentPane(); // holds the feilds in place &
                                                                  // generates a ‚ÄòContainer‚Äô
                                                                  // object that represents the pane
            pane.setLayout(layout); // checks known parameters

            PrincipalLabel.setForeground(Color.magenta);
            RateLabel.setForeground(Color.blue);
            TermLabel.setForeground(Color.magenta);
            PaymentLabel.setForeground(Color.red);

            Calculate.setForeground(Color.magenta);
            Clear.setForeground(Color.blue);
            Exit.setForeground(Color.red);

            pane.setForeground(Color.blue);

            // TRON listens for Button fuctions
            Calculate.addActionListener(TRON);
            Clear.addActionListener(TRON);
            Exit.addActionListener(TRON);

            // ‚ÄòFlowLayout‚Äô components fall into place from left to right
            FlowLayout mgr = new FlowLayout(FlowLayout.CENTER, 10, 10); // sets
                                                                                                      // parameters
            File1.setLayout(mgr); // Directs the user were to place data
            File1.add(PrincipalLabel); // add Component
            File1.add(LonAmt); // add Component
            pane.add(File1);

            FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
            File2.setLayout(layout2);
            File2.add(RateLabel);
            File2.add(Rat);
            pane.add(File2);

            FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 10, 10);
            File3.setLayout(layout3);
            File3.add(TermLabel);
            File3.add(Trm);
            pane.add(File3);

            FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER, 10, 10);
            MthlyPymnt.setEditable(false);
            File4.setLayout(layout4);
            File4.add(PaymentLabel);
            File4.add(MthlyPymnt);
            pane.add(File4);

            FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER, 5, 5);
            File5.setLayout(layout5);
            File5.add(Calculate);
            File5.add(Clear);
            File5.add(Exit);
            pane.add(File5);

            FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER);
            File6.setLayout(layout6);
            JTextArea area = new JTextArea(5, 10); // 5 rows and 10 columns
            JScrollPane scrollPane = new JScrollPane(area); // add the text area to
                                                                                    // the scroll pane

            File6.add(scrollPane);
            pane.add(File6);

            setVisible(true);
      }

      public static void main(String[] arguments) {

            Hw_prog1 frame = new Hw_prog1();

      }

      class Hw_prog1Event implements ActionListener {
            Hw_prog1 virtual;

            public Hw_prog1Event(Hw_prog1 in) {
                  virtual = in;
            }

            public void actionPerformed(ActionEvent event) {
                  String command = event.getActionCommand();
                  if(event.getSource() == Calculate)
                        LaunchComputation();
                  if(event.getSource() == Clear)
                        reset();
                  if(event.getSource() == Exit)
                        System.exit(0);
            }

            void LaunchComputation() { // sub-program to do math
                  try // when called
                  {
                        double LonAmt = Double.valueOf(virtual.LonAmt.getText()).doubleValue(); // Object
                                                                                                                                    // &
                                                                                                                                    // string,
                                                                                                                                    // double
                                                                                                                                    // value
                                                                                                                                    // when
                                                                                                                                    // Tron
                                                                                                                                    // calls
                                                                                                                                    // virtual
                        double Rat = Double.valueOf(virtual.Rat.getText()).doubleValue();
                        int Trm = Integer.parseInt(virtual.Trm.getText());
                        int totalmonths = (Trm * 12);
                        double Payment = 0;
                        String monthlyPayment = new String();
                        NumberFormat currency = NumberFormat.getCurrencyInstance();
                        double I = (Rat / 100.0 / 12.0);

                        Payment = (LonAmt * I) / (1 - Math.pow(1 / (1 + I), totalmonths));
                        virtual.MthlyPymnt.setText(currency.format(Payment));
                  } catch (NumberFormatException ex) {
                  }
            }

            protected void reset() {
                  virtual.LonAmt.setText(null);
                  virtual.Rat.setText(null);
                  virtual.Trm.setText(null);
                  virtual.MthlyPymnt.setText(null);
            }
      }

}
0
 
LVL 92

Expert Comment

by:objects
ID: 17870703
> You shouldn't mix AWT and Swing
> Panel, Label are AWT. JScrollPane etc, Swing

that won't stop it running
why did u accept that comment?
0
 

Author Comment

by:charliebaby
ID: 17870706
sorry wanted to split the points is there anything I can do now, to split them between  ForceRs  and CEHJ
0
 
LVL 3

Expert Comment

by:ForceRs
ID: 17870734
Don't worry about.  No problem.  Glad to help.
0
 

Author Comment

by:charliebaby
ID: 17870806
you are all so cool, Thank you so much I take it that the scroll panel will take affect as soon as there is text to scroll to?
0
 
LVL 3

Expert Comment

by:ForceRs
ID: 17870862
Yes -- works for me.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
advertisement module in core php 4 162
JavaScript/Java - Changing an image background color 4 66
Chrome and Firefox Java 5 31
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question