Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cannot resolve to a type

Posted on 2009-04-02
11
Medium Priority
?
307 Views
Last Modified: 2012-05-06
please help me on StyleHandler and ItemHandler because it is " cannot resolve to a type" Thank you.

======================
import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;


public class MenuFrame extends JFrame {
      private final Color colorValues[]={Color.BLACK,Color.BLUE,Color.RED,Color.GREEN};
      private JRadioButtonMenuItem colorItems[];// color menu items
      private JRadioButtonMenuItem fonts[];     // font menu items
      private JCheckBoxMenuItem styleItems[];   //font style menu items
      
      private JLabel displayJLabel;        // displays sample text
      private ButtonGroup fontButtonGroup; // manages font menu items
      private ButtonGroup colorButtonGroup;// manages color menu items
      private int style;                   // used to create style for font
      
      //no-argument constructor set up GUI
      public MenuFrame()
      {
            super("Using JMenus");
            
            JMenu fileMenu = new JMenu("File");
            fileMenu.setMnemonic('F'); // set mnemonic to F
            
            //create About ... menu item
            JMenuItem aboutItem = new JMenuItem("About ...");
            aboutItem.setMnemonic('A'); // set mnemonic to A
            fileMenu.add(aboutItem);    // add about item to file menu
            
            aboutItem.addActionListener(
                        new ActionListener() // anonymous inner class
                        {
                              //display message dialog when user selects About...
                              public void actionPerformed(ActionEvent event)
                              {
                                    JOptionPane.showMessageDialog(MenuFrame.this,
                                                "This is an example\nof using menus",
                                                "About",JOptionPane.PLAIN_MESSAGE);
                              }// end method actionPerformed
                        }//end anonymous innner class
            );//end call to addActionListener
            
            JMenuItem exitItem = new JMenuItem("Exit"); // create exit item
            exitItem.setMnemonic('x'); // set mnemonic to x
            fileMenu.add(exitItem);    // add exit item to file menu
            exitItem.addActionListener(
                        new ActionListener() // anonymous inner class
                        {
                              //terminate application when user clicks exitItem
                              public void actionPerformed (ActionEvent event)
                              {
                                    System.exit(0); // exit application
                              }// end method actionPerformed
                        }// end anonymous inner class
                        );//end call to addActionListener
            
            JMenuBar bar = new JMenuBar(); // create menu bar
            setJMenuBar(bar);              // add menu bar to application
            bar.add(fileMenu);             //add file menu to menu bar
            
            JMenu formatMenu = new JMenu("Format"); // create format menu
            formatMenu.setMnemonic('r');
            
            //array listing string colors
            String colors[]={"Black","Blue","Red","Green"};
            
            JMenu colorMenu = new JMenu("Color");// create color menu
            colorMenu.setMnemonic('C');          // set mnemonic to C
            
            //create radio button menu items for colors
            colorItems = new JRadioButtonMenuItem[colors.length];
            colorButtonGroup=new ButtonGroup();          //manages colors
            ItemHandler itemHandler = new ItemHandler(); //handler for colors
            
            
            //create color ratio button menu items
            for(int count = 0; count <colors.length;count++)
            {
                  colorItems[count]= new JRadioButtonMenuItem (colors[count]); // create item
                  colorMenu.add(colorItems[count]);  // add item to color menu
                  colorButtonGroup.add(colorItems[count]); // add to group
                  colorItems[count].addActionListener(itemHandler);
            }// end for
            
            colorItems[0].setSelected(true);  // select first Color item
            
            formatMenu.add(colorMenu);        // add color menu to format menu
            formatMenu.addSeparator();        //add separator in menu
            
            //array listing font names
            String fontNames[]={"Serif","Monospaced","SansSerif"};
            JMenu fontMenu = new JMenu("Font"); // create font menu
            fontMenu.setMnemonic('n');          // set mnemonic to n
            
            //creat radio button menu items for font names
            fonts = new JRadioButtonMenuItem[fontNames.length];
            fontButtonGroup = new ButtonGroup(); // manages font names
            
            // create Font radio button menu items
            for(int count = 0; count < fonts.length; count++)
            {
                  fonts[count]= new JRadioButtonMenuItem(fontNames[count]);
                  fontMenu.add(fonts[count]);                    //add font to font menu
                  fontButtonGroup.add(fonts[count]);             //add to button group
                  fonts[count].addActionListener(itemHandler);  //add handler
            }//end for
            
            fonts[0].setSelected(true); //select first Font Menu item
            fontMenu.addSeparator();    //add separator bar to font menu
            
            String styleNames[]={"Bold","Italic"};      // names of styles
            styleItems = new JCheckBoxMenuItem[styleNames.length];
            StyleHandler styleHandler = new StyleHandler(); // style handler
            
            //create style checkbox menu items
            for(int count =0; count <styleNames.length; count++)
            {
                  styleItems[count]= new JCheckBoxMenuItem(styleNames[count]);    // for style
                  fontMenu.add(styleItems[count]);                                // add to font menu
                  styleItems[count].addItemListener(styleHandler);                //handler
            } // end for
            
            
//            formatMenu.add(fontMenu); // add Font menu to Format menu
//            bar.add(formatMenu);      // add Format menu to menu bar
            
            private class ItemHandler implements ActionListener
            {
                  //process color and font selections
                  public void actionPerformed(ActionEvent event)
                  {
                        //process color selection
                        for(int count = 0; count<colorItems.length;count++)
                        {
                              if(colorItems[count].isSelected())
                              {
                                    displayJLabel.setForeground(colorValues[count]);
                                    break;
                              }//end if
                        }//end for
                        
                        //process font selection
                        for(int count = 0; count < fonts.length;count++)
                        {
                              if(event.getSource() == fonts[count])
                              {
                                    displayJLabel.setFont(new Font (fonts[count].getText(),style,72));
                              }//end if
                        }//end for
                        repaint(); // redraw application
                  }// end method actionPerformed
            }// end class ItemHandler
            
            //inner class to handle item events from check box menu items
            private class  StyleHandler implements ItemListener
            {
                  //process font style selections
                  public void itemStateChanged(ItemEvent e)
                  {
                        style = 0; // initialize style
                        
                        // check for bold selection
                        if(styleItems[0].isSelected())
                                    style += Font.ITALIC; // add italic to style
                        displayJLabel.setFont(
                                    new Font (displayJLabel.getFont().getName(),style,72));
                        repaint(); // redraw application
                  }// end method itemStateChanged
            }// end class StyleHandler
      }//end class MenuFrame
      
      
}
=============
import javax.swing.JFrame;

public class MenuTest {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            MenuFrame menuFrame = new MenuFrame(); // create MenuFrame
            menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            menuFrame.setLocationRelativeTo(null);
            menuFrame.setSize(500,200);
            menuFrame.setVisible(true);
      }// end main
}//end class MenuTest
===========
0
Comment
Question by:valleytech
[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
  • 6
  • 5
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24054529
where are those classes coming from?

0
 

Author Comment

by:valleytech
ID: 24054630
inside the MenuFrame class
0
 
LVL 92

Expert Comment

by:objects
ID: 24054763
IU see that :) I meant what api are they from, why are you using them?

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:valleytech
ID: 24054876
ItemHandler to generate event and set font or color
StyleHandler determine new style of font
0
 
LVL 92

Expert Comment

by:objects
ID: 24054905
where do they come from? what api are they p[art of?
you need to add the api's jars/classes to your build path


0
 

Author Comment

by:valleytech
ID: 24054976
after i double check from the book, I believe I have imported all api already .
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24055043
sorry, didn't look deep enough in your code :)
they are inner classes but you have put them inside a method, try:

import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;


public class MenuFrame extends JFrame {
      private final Color colorValues[]={Color.BLACK,Color.BLUE,Color.RED,Color.GREEN};
      private JRadioButtonMenuItem colorItems[];// color menu items
      private JRadioButtonMenuItem fonts[];     // font menu items
      private JCheckBoxMenuItem styleItems[];   //font style menu items
     
      private JLabel displayJLabel;        // displays sample text
      private ButtonGroup fontButtonGroup; // manages font menu items
      private ButtonGroup colorButtonGroup;// manages color menu items
      private int style;                   // used to create style for font
     
      //no-argument constructor set up GUI
      public MenuFrame()
      {
            super("Using JMenus");
           
            JMenu fileMenu = new JMenu("File");
            fileMenu.setMnemonic('F'); // set mnemonic to F
           
            //create About ... menu item
            JMenuItem aboutItem = new JMenuItem("About ...");
            aboutItem.setMnemonic('A'); // set mnemonic to A
            fileMenu.add(aboutItem);    // add about item to file menu
           
            aboutItem.addActionListener(
                        new ActionListener() // anonymous inner class
                        {
                              //display message dialog when user selects About...
                              public void actionPerformed(ActionEvent event)
                              {
                                    JOptionPane.showMessageDialog(MenuFrame.this,
                                                "This is an example\nof using menus",
                                                "About",JOptionPane.PLAIN_MESSAGE);
                              }// end method actionPerformed
                        }//end anonymous innner class
            );//end call to addActionListener
           
            JMenuItem exitItem = new JMenuItem("Exit"); // create exit item
            exitItem.setMnemonic('x'); // set mnemonic to x
            fileMenu.add(exitItem);    // add exit item to file menu
            exitItem.addActionListener(
                        new ActionListener() // anonymous inner class
                        {
                              //terminate application when user clicks exitItem
                              public void actionPerformed (ActionEvent event)
                              {
                                    System.exit(0); // exit application
                              }// end method actionPerformed
                        }// end anonymous inner class
                        );//end call to addActionListener
           
            JMenuBar bar = new JMenuBar(); // create menu bar
            setJMenuBar(bar);              // add menu bar to application
            bar.add(fileMenu);             //add file menu to menu bar
           
            JMenu formatMenu = new JMenu("Format"); // create format menu
            formatMenu.setMnemonic('r');
           
            //array listing string colors
            String colors[]={"Black","Blue","Red","Green"};
           
            JMenu colorMenu = new JMenu("Color");// create color menu
            colorMenu.setMnemonic('C');          // set mnemonic to C
           
            //create radio button menu items for colors
            colorItems = new JRadioButtonMenuItem[colors.length];
            colorButtonGroup=new ButtonGroup();          //manages colors
            ItemHandler itemHandler = new ItemHandler(); //handler for colors
           
           
            //create color ratio button menu items
            for(int count = 0; count
0
 

Author Comment

by:valleytech
ID: 24055139
I totally agree with you. I missed
    }//end class MenuFrame  <-- end of MenuFrame constructor



0
 

Author Comment

by:valleytech
ID: 24055434
did u take a look at my 3 frame question ?
0
 
LVL 92

Expert Comment

by:objects
ID: 24055549
I did, bit busy at the moment unfortunately.
0
 

Author Comment

by:valleytech
ID: 24055566
sure. Thank you a lot.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 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