Solved

compiles but doesn't run?

Posted on 2006-11-03
11
192 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This video teaches viewers about errors in exception handling.
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.

758 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

21 Experts available now in Live!

Get 1:1 Help Now