Solved

compiles but doesn't run?

Posted on 2006-11-03
11
193 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 121
Java asynchronous logging 4 50
eclipse compiler vs Installed JREs option 3 77
Apps blocked by Java 9 63
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now