Solved

what mistake have I made

Posted on 2006-11-05
4
201 Views
Last Modified: 2010-03-31
When I declare this, "JMenuItem item;" now what mistake have I made in the code?

               menuBar = new JMenuBar();
           JMenu menu = new JMenu("Menu");
             item=new  JMenuitem()
             JMenuItem item = new JMenuItem("Item Labelxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            setJMenuBar(menuBar);
                  //item.addActionListener(actionListener);
                   File7.add(item);
                   File7.add(menu);
                   menu.add(item);
                  
            pane.add(File7);
                  //item.addActionListener(actionListener);
                  
                  item.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
       // this will get called
}});
//item.addActionListener(actionListener);

      setVisible(true);
     }
0
Comment
Question by:charliebaby
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17876660
Delete

>> item=new  JMenuitem()
0
 

Author Comment

by:charliebaby
ID: 17876698
how come There is no fuction to select  "JMenuItem item = new JMenuItem("Item Labelxxxxxxxxxxxxxxxxxxxxxxxxxxx");" when I run the program All I get is a solid unfuctioning menu bar?
then I will close out the question, and thanks again and again.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17876705
0
 

Author Comment

by:charliebaby
ID: 17876768

    frame.setJMenuBar(menuBar); is the only thing not included, been there before, constructed my menu from that link, Menu  shows up in the frame only with out any fuctionalty. Oh well any more advice would be great...heres your points thanks

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

Deleated THIS
 "item.addActionListener(actionListener);" as well as what you suggested >> item=new  JMenuitem() (gone) I am going to include the whole code so you can have a look:

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;
      
       JMenuBar menuBar;
      
        JMenuItem item;
          JComboBox term;
             

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

     JTextField LonAmt = new JTextField(8);

     JPanel File2 = new JPanel();

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

     JTextField Rat = new JTextField(8);

     JPanel File3 = new JPanel();

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

     JTextField Trm = new JTextField(8);

     JPanel File4 = new JPanel();

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

     JTextField MthlyPymnt = new JTextField(8);

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

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

     // create borders(Grid layout)
     public Hw_prog1() { // Reference the main method
          super("McBride.Financial.MortCal"); // Calling setSize
          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, 5, 10, 10); // calls the
                                                                         // TextFeild & sets
                                                                           // paramerters
          Container pane = getContentPane(); // holds the feilds in place &
           //menuBar = new JMenuBar();                                            // 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);
//item.addActionListener(actionListener);
          // ‚Äò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
               //JMenu menu = new JMenu("Menu Label");
   // menuBar.add(menu);
   
          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();
           JMenu menu = new JMenu("Menu");
             //item=new  JMenuitem()
             JMenuItem item = new JMenuItem("Item Labelxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            setJMenuBar(menuBar);
                  //item.addActionListener(actionListener);
                   File7.add(item);
                   File7.add(menu);
                   menu.add(item);
                  
   
            pane.add(File7);
                  //item.addActionListener(actionListener);
                  
                  item.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
       // this will get called
}});
//item.addActionListener(actionListener);

      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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

762 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

24 Experts available now in Live!

Get 1:1 Help Now