Solved

I have it almost working

Posted on 2006-11-07
9
246 Views
Last Modified: 2010-03-31
What do I need to do to fix the menu component? I have it almost working...thank you


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

public class Hw_prog2_     extends JFrame {
Hw_prog2_Event TRON = new Hw_prog2_Event(this);

     JButton Calculate;
       JButton Clear;
     JButton Exit;

     JTextArea area;
     JScrollPane scrollPane;
      
       JMenuBar menuBar;
       JMenuItem item;
      
             

     JPanel File1 = new JPanel();
       JLabel PrincipalLabel = new JLabel("$Enter Loan Amount:", JLabel.CENTER); // create
       JTextField LonAmt = new JTextField(8);
      
     JPanel File2 = new JPanel();
     JLabel RateLabel = new JLabel("0.0Enter Interest Rate:", JLabel.CENTER); // create
       JTextField Rat = new JTextField(8);

     JPanel File3 = new JPanel();
     JLabel TermLabel = new JLabel("Enter Term-Years:", JLabel.CENTER); // create
       JTextField Trm = new JTextField(8);

     JPanel File4 = new JPanel();
       JLabel PaymentLabel = new JLabel("Monthly Payments:", JLabel.CENTER); // create
       JTextField MthlyPymnt = new JTextField(8);

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

     JPanel File6 = new JPanel();
      
       JPanel File7 = new JPanel();
      
        JPanel File8 = new JPanel();

             // create borders(Grid layout)
     public Hw_prog2_() {                     // Reference the main method
         
     super("McBride.Financial.MortCal"); setSize(890, 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, 1, 1, 1); // calls the
                                                                       
       Container pane = getContentPane(); // holds the feilds in place &
                                                   
       pane.setLayout(layout); // checks known parameters
      
          PrincipalLabel.setForeground(Color.blue);
          RateLabel.setForeground(Color.blue);
          TermLabel.setForeground(Color.blue);
          PaymentLabel.setForeground(Color.blue);

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

          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

          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
              File6.add(scrollPane);
            pane.add(File6);
             
              FlowLayout layout7 = new FlowLayout(FlowLayout.CENTER);
          File7.setLayout(layout7);
          menuBar = new JMenuBar();
              setJMenuBar(menuBar);

              JMenu menu = new JMenu("Menu");
            JMenuItem item = new JMenuItem("5.35% at 7yrs");
              JMenuItem itema = new JMenuItem("5.50% at 15yrs");
              JMenuItem itemb = new JMenuItem("5.75% at 30yrs");
              File7.add(menu);
              menu.add(item);
              menu.add(itema);
              menu.add(itemb);
            File7.add(item);
              File7.add(itema);
              File7.add(itemb);
              pane.add(File7);
             
      // FlowLayout layout8 = new FlowLayout(FlowLayout.CENTER);
      // File8.setLayout(layout8);
      //File8.add(menu);
            
               //setVisible(true);

                  
                  
            item.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
       // this will get called
}});
setVisible(true);
}

        public static void main(String[] arguments) {
     Hw_prog2_ frame = new Hw_prog2_();

}

        class Hw_prog2_Event implements ActionListener {
       Hw_prog2_ virtual;

       public Hw_prog2_Event(Hw_prog2_ 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);
}

      public    void LaunchComputation() { // sub-program to do math
               try {
                        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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 3

Expert Comment

by:rixlabs
ID: 17894522
Hi charliebaby,

If you want to add a menu in the File7 panel try this ->

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

public class Hw_prog2_     extends JFrame {
Hw_prog2_Event TRON = new Hw_prog2_Event(this);

     JButton Calculate;
      JButton Clear;
     JButton Exit;

     JTextArea area;
     JScrollPane scrollPane;
     
      JMenuBar menuBar;
      JMenuItem item;
     
           

     JPanel File1 = new JPanel();
      JLabel PrincipalLabel = new JLabel("$Enter Loan Amount:", JLabel.CENTER); // create
      JTextField LonAmt = new JTextField(8);
     
     JPanel File2 = new JPanel();
     JLabel RateLabel = new JLabel("0.0Enter Interest Rate:", JLabel.CENTER); // create
      JTextField Rat = new JTextField(8);

     JPanel File3 = new JPanel();
     JLabel TermLabel = new JLabel("Enter Term-Years:", JLabel.CENTER); // create
      JTextField Trm = new JTextField(8);

     JPanel File4 = new JPanel();
      JLabel PaymentLabel = new JLabel("Monthly Payments:", JLabel.CENTER); // create
      JTextField MthlyPymnt = new JTextField(8);

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

     JPanel File6 = new JPanel();
     
      JPanel File7 = new JPanel();
     
       JPanel File8 = new JPanel();

             // create borders(Grid layout)
     public Hw_prog2_() {                     // Reference the main method
         
     super("McBride.Financial.MortCal"); setSize(890, 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, 1, 1, 1); // calls the
                                                                       
      Container pane = getContentPane(); // holds the feilds in place &
                                                   
      pane.setLayout(layout); // checks known parameters
     
          PrincipalLabel.setForeground(Color.blue);
          RateLabel.setForeground(Color.blue);
          TermLabel.setForeground(Color.blue);
          PaymentLabel.setForeground(Color.blue);

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

          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

          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
            File6.add(scrollPane);
           pane.add(File6);
           
            FlowLayout layout7 = new FlowLayout(FlowLayout.CENTER);
          File7.setLayout(layout7);
          menuBar = new JMenuBar();
            setJMenuBar(menuBar);
           
            JMenu menu = new JMenu("Menu");
            menuBar.add(menu);
            JMenuItem item = new JMenuItem("5.35% at 7yrs");
            item.setEnabled(true);
           
            JMenuItem itema = new JMenuItem("5.50% at 15yrs");
            itema.setEnabled(true);
            JMenuItem itemb = new JMenuItem("5.75% at 30yrs");
            itemb.setEnabled(true);
            menu.add(item);
            menu.add(itema);
            menu.add(itemb);
            File7.add(menuBar);
            pane.add(File7);
           
           
     // FlowLayout layout8 = new FlowLayout(FlowLayout.CENTER);
     // File8.setLayout(layout8);
     //File8.add(menu);
         
             //setVisible(true);

               
               
       /*   item.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
       // this will get called
}});*/
setVisible(true);
}

        public static void main(String[] arguments) {
     Hw_prog2_ frame = new Hw_prog2_();

}

        class Hw_prog2_Event implements ActionListener {
      Hw_prog2_ virtual;

      public Hw_prog2_Event(Hw_prog2_ 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);
}

      public    void LaunchComputation() { // sub-program to do math
               try {
                    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

Expert Comment

by:rixlabs
ID: 17894560
Sorry I commente the ActionListener...

You cant Uncomment it :P
0
 
LVL 3

Expert Comment

by:rixlabs
ID: 17894598
Sorry for my typing errors....

If you want you can Uncomment the Actionlistener without problems...

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:charliebaby
ID: 17894822
Made changes as I have posted, for some reason I see nothing in that area now? is this correct, I coppied your order of placement exactly. Thank you       


 FlowLayout layout7 = new FlowLayout(FlowLayout.CENTER);
          File7.setLayout(layout7);
          menuBar = new JMenuBar();
              setJMenuBar(menuBar);

              JMenu menu = new JMenu("Menu");
               menuBar.add(menu);
            JMenuItem item = new JMenuItem("5.35% at 7yrs");
               item.setEnabled(true);
              JMenuItem itema = new JMenuItem("5.50% at 15yrs");
                 itema.setEnabled(true);
              JMenuItem itemb = new JMenuItem("5.75% at 30yrs");
               itemb.setEnabled(true);
              //File7.add(menu);
              menu.add(item);
              menu.add(itema);
              menu.add(itemb);
                File7.add(menuBar);
            //File7.add(item);
             // File7.add(itema);
             // File7.add(itemb);
              pane.add(File7);
             
      // FlowLayout layout8 = new FlowLayout(FlowLayout.CENTER);
      // File8.setLayout(layout8);
      //File8.add(menu);
            
               //setVisible(true);

                  
                  
            //item.addActionListener(new ActionListener() {
     // public void actionPerformed(ActionEvent event) {
       // this will get called
//}});
setVisible(true);
}

0
 

Author Comment

by:charliebaby
ID: 17894941
That was the first thing I tried, But it didn't help, will try it again, maybe I sliped up some how, you now how easy that can happen, oh will let you know, Thanks again
0
 

Author Comment

by:charliebaby
ID: 17894972
No, that didn't do it, I am at a brick wall myself, I am not a programer only a student in a very fast paced school, maybe there is something I neclected to decleare in the original code I posted, could you go over it again and make sure that I didn't neglet implmenting something. Thank you so much
0
 

Author Comment

by:charliebaby
ID: 17895262
I'm sorry it occures to me that I did not make it clear as to the problem that I was having with my menu. What I am expecting it to do is, when you click on "menu" the three Items first appear,and drop down to be selected, How can I do this?
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 17895401
try changing this:

>             JMenu menu = new JMenu("Menu");
>            JMenuItem item = new JMenuItem("5.35% at 7yrs");
>             JMenuItem itema = new JMenuItem("5.50% at 15yrs");
>             JMenuItem itemb = new JMenuItem("5.75% at 30yrs");
>             File7.add(menu);
>             menu.add(item);
>             menu.add(itema);
>             menu.add(itemb);
>            File7.add(item);
>             File7.add(itema);
>             File7.add(itemb);
>             pane.add(File7);


to



        JMenuBar bar = new JMenuBar();
        File7.add(bar);
       
        JMenu menu = new JMenu("Menu");
        JMenuItem item = new JMenuItem("5.35% at 7yrs");
        JMenuItem itema = new JMenuItem("5.50% at 15yrs");
        JMenuItem itemb = new JMenuItem("5.75% at 30yrs");
        bar.add(menu);
        menu.add(item);
        menu.add(itema);
        menu.add(itemb);
        //File7.add(item);
        //File7.add(itema);
        //File7.add(itemb);
        pane.add(File7);
0
 

Author Comment

by:charliebaby
ID: 17895465
Thats a beautifal thing, thank you so much it works great, thank you so much.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

635 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