p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: "not found in class" error !


Message #1 by dominskis@a... on Tue, 9 Apr 2002 00:29:26
Dear People,
 In doing Ivor Horton's "Beginning Java 2" Chapter 15 page 726  in 
combination with Jacquie Barker's "Beginning Java Objects " Chapter 16 
page 535 Calculator Program 
when trying to implement a modal dialog using JOptionPanel class method in 
Sketcher I have one error message. It reads:


"SketchFrame.java" : method showMessageDialog
(sketchercalculator.SketchFrame, java.lang.String, java.lang.String, 
java.lang.String, int) not found in class javax.swing.JOptionPane at line 
301, column 26

below is the revised coding for SketcherCalculator and SketchFrame classes
Thank you in advance
Stan



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

//Sketcher Program version  #10
// part 1 adding a modal dialog  part 2 add a Help menu with an about item

public class SketcherCalculator
{
  //declare the application window reference variable as private
  private static SketchFrame myJFrame;

  //declare the application window object
  private static SketcherCalculator theApp;

  public SketcherCalculator()
   {
     myJFrame =  new SketchFrame("Sketcher , Enjoying the design process", 
theApp);

     myJFrame.setSize(400,400);

     Dimension myFrameSize = myJFrame.getSize();
     Toolkit myToolkit = myJFrame.getToolkit();
     Dimension myScreenSize = myToolkit.getDefaultToolkit().getScreenSize
();

     //center the frame on the screen
     myJFrame.setLocation((myScreenSize.width -  myFrameSize.width)/2,
                         (myScreenSize.height - myFrameSize.height)/2);

    //charnge the backgroud color of the content pane
    myJFrame.getContentPane().setBackground(Color.yellow);

    //create the WindowAdapter object that is the listener for the 
myJFrame object
    myJFrame.addWindowListener(new WindowHandler() );

    myJFrame.setVisible(true);
   }

   //Handler class for window events
   class WindowHandler extends WindowAdapter
    {
      //Handler method for window closing event
        public void windowClosing(WindowEvent e)
           {
             //release window resources
             myJFrame.dispose();

             //end the application
             System.exit(0);
           }
    }

   public static void main(String[] args)
    {
      new SketcherCalculator();
      new Calculator6();
    }
}

package sketchercalculator;

 import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.border.*;
  import java.io.*;

  //Sketcher program version #10 .
  // part 1 adding a modal dialog  part 2 add a Help menu with an about 
item
  public  class SketchFrame extends JFrame implements Constants, 
ActionListener
   {
     public SketchFrame(String title, SketcherCalculator theApp)
      {
        setTitle(title);
        setJMenuBar(myMenuBar);

        //create the file menu
        JMenu myFileMenu = new JMenu("File");

        //create the element menu
        JMenu myElementMenu = new JMenu("Elements");

        //part 2 create help manu
        JMenu helpMenu =  new JMenu("Help");

        //create a shortcut for the file menu
        myFileMenu.setMnemonic('F');

        //create a shortcut for the element menu
        myElementMenu.setMnemonic('E');

        //part 2 create a shortcuo for the  Help menu
        helpMenu.setMnemonic('H');

        //add a suitable Action object to a menu
        //and a helper method to economize on the numbe of lines of code

        //create the action items for the file menu
      FileAction  newAction = new FileAction("New",KeyStroke.getKeyStroke
('N',Event.CTRL_MASK), "Create new sketch");
      FileAction  openAction = new FileAction("Open",KeyStroke.getKeyStroke
('O', Event.CTRL_MASK), "Open existing sketch");
      FileAction  closeAction = new FileAction("Close", "Close sketch");
      FileAction  saveAction =  new FileAction
("Save",KeyStroke.getKeyStroke('S',Event.CTRL_MASK), "Save sketch");
       FileAction saveAsAction =    new FileAction("Save As...", "Save as 
new file");
       FileAction printAction = new FileAction("Print", 
KeyStroke.getKeyStroke('P', Event.CTRL_MASK), "Print sketch");

        //construct the file pull down menu using actions.....
          addMenuItem(myFileMenu, newAction);
          addMenuItem(myFileMenu, openAction);
          addMenuItem(myFileMenu, closeAction);
          myFileMenu.addSeparator();
          addMenuItem(myFileMenu, saveAction);
          addMenuItem(myFileMenu,saveAsAction);
          myFileMenu.addSeparator();
          addMenuItem(myFileMenu, printAction);

        //version #8 add a tooltip sring to   pass
        //we will add the pull down menu items using actions.....
        addMenuItem(myElementMenu, lineAction = new TypeAction
("Line",LINE, "Draw Lines"));
        addMenuItem(myElementMenu, rectangleAction = new TypeAction
("Rectangle",RECTANGLE, "Draw Rectangles"));
        addMenuItem(myElementMenu, circleAction = new TypeAction
("Circle",CIRCLE, "Draw Cirlies"));
        addMenuItem(myElementMenu, curveAction = new TypeAction
("Curve",CURVE, "Draw curves"));

        myElementMenu.addSeparator();

        //add color submenu
        JMenu colorMenu = new JMenu("Color");
        myElementMenu.add(colorMenu);

        // we will add the color menu items here using actions....
         addMenuItem(colorMenu, redAction = new ColorAction
("Red",Color.red, "Draw in red"));
         addMenuItem(colorMenu, yellowAction = new ColorAction
("Yellow",Color.yellow, "Draw in yellow"));
         addMenuItem(colorMenu, greenAction = new ColorAction
("Green",Color.green, "Draw in green"));
         addMenuItem(colorMenu, blueAction = new ColorAction
("Blue",Color.blue, "Draw in blue"));

        myMenuBar.add(myFileMenu);
        myMenuBar.add(myElementMenu);

        //add the open file image
        openAction.putValue(Action.SMALL_ICON, new ImageIcon
("Images/open.gif"));

        //add file buttons
        myToolBar.addSeparator();
        addToolBarButton(newAction);
        addToolBarButton(openAction);
        addToolBarButton(saveAction);
        addToolBarButton(printAction);

        //add element type buttons
        myToolBar.addSeparator();
        addToolBarButton(lineAction);
        addToolBarButton(rectangleAction);
        addToolBarButton(circleAction);
        addToolBarButton(curveAction);

        //add element color buttons
        myToolBar.addSeparator();
        addToolBarButton(redAction);
        addToolBarButton(yellowAction);
        addToolBarButton(greenAction);
        addToolBarButton(blueAction);
        myToolBar.addSeparator();

        //Create a toolbar border
        myToolBar.setBorder(BorderFactory.createCompoundBorder(
                            BorderFactory.createLineBorder(Color.darkGray),
                            BorderFactory.createEmptyBorder(2,2,4,2)));

          //inhibit (with false) the floating toolbar
          myToolBar.setFloatable(false);

          //add the About item to the Help menu
          //create the item
          aboutItem = new JMenuItem("About");

          //the listener is the frame
          aboutItem.addActionListener(this);

          //add item to menu
          helpMenu.add(aboutItem);

          //add the help menu
          myMenuBar.add(helpMenu);


         // version #7 add the toolbar to the content pane
         getContentPane().add(myToolBar, BorderLayout.NORTH);

          //version #9  add the status pane to the content pane
          getContentPane().add(statusBar, BorderLayout.SOUTH);

     }

       //version #7 add inner classes defining action objects here....

       class FileAction extends AbstractAction
         {
           //constructor
           FileAction(String name)
            {
              super(name);
              String iconFileName = "Images/" + name + ".gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
             }

            //constructor
            FileAction(String name, KeyStroke keystroke)
             {
               this(name);
               if(keystroke != null)
                 putValue(ACCELERATOR_KEY, keystroke);
             }

              //version # 8 adding constructors
              FileAction(String name,KeyStroke keystroke, String tooltip)
               {
                this(name, keystroke);   //call the other constructor
                if(tooltip != null)
                  putValue(SHORT_DESCRIPTION, tooltip);
               }

              //takes care of a tooltip for an Action item without an 
accelerator keystroke
              FileAction(String name, String tooltip)
               {
                 this(name);     //call the other constructor
                 if(tooltip != null)
                  putValue(SHORT_DESCRIPTION, tooltip);
               }

             //Event handler
             public void actionPerformed(ActionEvent e)
              {
                //action code added later...
              }

            //add action objects as members here.......
             private FileAction newAction, openAction, closeAction, 
saveAction,
                     saveAsAction,printAction;

        }
        // version #8 incorprorate the tooltip argunents
       //VERSION # 7 add inner class for the element type menus

       class TypeAction extends AbstractAction
        {
          TypeAction(String name, int typeID)
           {
             super(name);
             this.typeID = typeID;
             String iconFileName = "Images/" + name + ".gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
           }

            //version #8 constructor add for the tooltip
            TypeAction(String name, int typeID, String tooltip)
             {
               this(name, typeID);
               if(tooltip != null)
                putValue(SHORT_DESCRIPTION, tooltip);
             }

            public void actionPerformed(ActionEvent e)
            {
             int  elementType;
              elementType =  typeID;

              //version #9  to update the status bar when the element type 
changes
              statusBar.setTypePane(typeID);
            }

            private int typeID;
         }

        //version #8 add a constructor for the tooltip
        // version # 7 add an inner class for the color type menus
        class ColorAction extends AbstractAction
         {
           public ColorAction(String name, Color color)
            {
              super(name);
              this.color = color;
              String iconFileName = "Images/" + name + ".gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
            }

            public ColorAction(String name, Color color, String tooltip)
             {
                this(name,color);
                if(tooltip != null)
                  putValue(SHORT_DESCRIPTION,tooltip);
             }

            public void actionPerformed(ActionEvent e)
             {
              Color elementColor;
              elementColor = color;

              //this is temporary . Just to show it works
              getContentPane().setBackground(color);

              //version #9 to update the status bar when the color element 
changes
              statusBar.setColorPane(color);
             }
            private Color color;
         }

        //add helper method
        private JMenuItem addMenuItem(JMenu menu, Action action)
         {
           //add the menu item
           JMenuItem item = menu.add(action);

           KeyStroke keystroke = (KeyStroke)action.getValue
(action.ACCELERATOR_KEY);
           if(keystroke != null)
            item.setAccelerator(keystroke);

            return item;
         }

              //declare colorActionreference variables
        private ColorAction redAction;
        private ColorAction yellowAction;
        private ColorAction greenAction;
        private ColorAction blueAction;

        //for the members that will store references to the TypeAction 
objects
        private TypeAction lineAction, rectangleAction, circleAction, 
curveAction;

       //create window menu bar
       private JMenuBar myMenuBar = new JMenuBar();

        // version #7  create a toolbar
        private JToolBar myToolBar = new JToolBar();

          //version #9 define the status bar as a data member
          StatusBar statusBar = new StatusBar();

          private JButton addToolBarButton(Action action)
           {
            //add a toolbar button
            JButton myJButton = myToolBar.add(action);

            // Add toolbar button border
            myJButton.setBorder(BorderFactory.createRaisedBevelBorder());

            //ensure no text on the button
            myJButton.setText(null);
            return myJButton;
          }


                     //implementation to handle the About menu tiem's 
events
        public void actionPerformed(ActionEvent e)
         {
          if (e.getSource() == aboutItem)
           {
             //create about dialog with the app window as parent
             JOptionPane.showMessageDialog(this, "About 
Sketcher", "Sketcher Copywrite Ivor Horton 2000", "About Sketcher", 
JOptionPane.INFORMATION_MESSAGE);

           }
          }


        //version 10 part 2
        private JMenuItem aboutItem;

       //variable to hold the current element color
       private Color myElementColor = DEFAULT_ELEMENT_COLOR;

       //variable to hold the current element type
       private int myElementTYPE =  DEFAULT_ELEMENT_TYPE;
     }

Message #2 by dominskis@a... on Tue, 9 Apr 2002 04:41:24
Dear Jacquie Barker,
 I am trying to do a combination program that includes the Calculator 
Program "Beginning Java Objects" Chapter 16 page 535 and Ivor 
Horton's "Beginning Java 2" Chapter 15 page 726 Sketcher Program. I 
included the calculator program as a class in the Sketcher  program.
I have one error message that reads:

"SketchFrame.java": Error #: 300 : method showMessageDialog
(sketchercalculator.SketchFrame, java.lang.String, java.lang.String, 
java.lang.String, int) not found in class javax.swing.JOptionPane at line 
301, column 26


below is the coding 
Thank you in advance
 Stan Dominski

package sketchercalculator;

 import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.border.*;
  import java.io.*;

  //Sketcher program version #10 .
  // part 1 adding a modal dialog  part 2 add a Help menu with an about 
item
  public  class SketchFrame extends JFrame implements Constants, 
ActionListener
   {
     public SketchFrame(String title, SketcherCalculator theApp)
      {
        setTitle(title);
        setJMenuBar(myMenuBar);

        //create the file menu
        JMenu myFileMenu = new JMenu("File");

        //create the element menu
        JMenu myElementMenu = new JMenu("Elements");

        //part 2 create help manu
        JMenu helpMenu =  new JMenu("Help");

        //create a shortcut for the file menu
        myFileMenu.setMnemonic('F');

        //create a shortcut for the element menu
        myElementMenu.setMnemonic('E');

        //part 2 create a shortcuo for the  Help menu
        helpMenu.setMnemonic('H');

        //add a suitable Action object to a menu
        //and a helper method to economize on the numbe of lines of code

        //create the action items for the file menu
      FileAction  newAction = new FileAction("New",KeyStroke.getKeyStroke
('N',Event.CTRL_MASK), "Create new sketch");
      FileAction  openAction = new FileAction("Open",KeyStroke.getKeyStroke
('O', Event.CTRL_MASK), "Open existing sketch");
      FileAction  closeAction = new FileAction("Close", "Close sketch");
      FileAction  saveAction =  new FileAction
("Save",KeyStroke.getKeyStroke('S',Event.CTRL_MASK), "Save sketch");
       FileAction saveAsAction =    new FileAction("Save As...", "Save as 
new file");
       FileAction printAction = new FileAction("Print", 
KeyStroke.getKeyStroke('P', Event.CTRL_MASK), "Print sketch");

        //construct the file pull down menu using actions.....
          addMenuItem(myFileMenu, newAction);
          addMenuItem(myFileMenu, openAction);
          addMenuItem(myFileMenu, closeAction);
          myFileMenu.addSeparator();
          addMenuItem(myFileMenu, saveAction);
          addMenuItem(myFileMenu,saveAsAction);
          myFileMenu.addSeparator();
          addMenuItem(myFileMenu, printAction);

        //version #8 add a tooltip sring to   pass
        //we will add the pull down menu items using actions.....
        addMenuItem(myElementMenu, lineAction = new TypeAction
("Line",LINE, "Draw Lines"));
        addMenuItem(myElementMenu, rectangleAction = new TypeAction
("Rectangle",RECTANGLE, "Draw Rectangles"));
        addMenuItem(myElementMenu, circleAction = new TypeAction
("Circle",CIRCLE, "Draw Cirlies"));
        addMenuItem(myElementMenu, curveAction = new TypeAction
("Curve",CURVE, "Draw curves"));

        myElementMenu.addSeparator();

        //add color submenu
        JMenu colorMenu = new JMenu("Color");
        myElementMenu.add(colorMenu);

        // we will add the color menu items here using actions....
         addMenuItem(colorMenu, redAction = new ColorAction
("Red",Color.red, "Draw in red"));
         addMenuItem(colorMenu, yellowAction = new ColorAction
("Yellow",Color.yellow, "Draw in yellow"));
         addMenuItem(colorMenu, greenAction = new ColorAction
("Green",Color.green, "Draw in green"));
         addMenuItem(colorMenu, blueAction = new ColorAction
("Blue",Color.blue, "Draw in blue"));

        myMenuBar.add(myFileMenu);
        myMenuBar.add(myElementMenu);

        //add the open file image
        openAction.putValue(Action.SMALL_ICON, new ImageIcon
("Images/open.gif"));

        //add file buttons
        myToolBar.addSeparator();
        addToolBarButton(newAction);
        addToolBarButton(openAction);
        addToolBarButton(saveAction);
        addToolBarButton(printAction);

        //add element type buttons
        myToolBar.addSeparator();
        addToolBarButton(lineAction);
        addToolBarButton(rectangleAction);
        addToolBarButton(circleAction);
        addToolBarButton(curveAction);

        //add element color buttons
        myToolBar.addSeparator();
        addToolBarButton(redAction);
        addToolBarButton(yellowAction);
        addToolBarButton(greenAction);
        addToolBarButton(blueAction);
        myToolBar.addSeparator();

        //Create a toolbar border
        myToolBar.setBorder(BorderFactory.createCompoundBorder(
                            BorderFactory.createLineBorder(Color.darkGray),
                            BorderFactory.createEmptyBorder(2,2,4,2)));

          //inhibit (with false) the floating toolbar
          myToolBar.setFloatable(false);

          //add the About item to the Help menu
          //create the item
          aboutItem = new JMenuItem("About");

          //the listener is the frame
          aboutItem.addActionListener(this);

          //add item to menu
          helpMenu.add(aboutItem);

          //add the help menu
          myMenuBar.add(helpMenu);


         // version #7 add the toolbar to the content pane
         getContentPane().add(myToolBar, BorderLayout.NORTH);

          //version #9  add the status pane to the content pane
          getContentPane().add(statusBar, BorderLayout.SOUTH);

     }

       //version #7 add inner classes defining action objects here....

       class FileAction extends AbstractAction
         {
           //constructor
           FileAction(String name)
            {
              super(name);
              String iconFileName = "Images/" + name + ".gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
             }

            //constructor
            FileAction(String name, KeyStroke keystroke)
             {
               this(name);
               if(keystroke != null)
                 putValue(ACCELERATOR_KEY, keystroke);
             }

              //version # 8 adding constructors
              FileAction(String name,KeyStroke keystroke, String tooltip)
               {
                this(name, keystroke);   //call the other constructor
                if(tooltip != null)
                  putValue(SHORT_DESCRIPTION, tooltip);
               }

              //takes care of a tooltip for an Action item without an 
accelerator keystroke
              FileAction(String name, String tooltip)
               {
                 this(name);     //call the other constructor
                 if(tooltip != null)
                  putValue(SHORT_DESCRIPTION, tooltip);
               }

             //Event handler
             public void actionPerformed(ActionEvent e)
              {
                //action code added later...
              }

            //add action objects as members here.......
             private FileAction newAction, openAction, closeAction, 
saveAction,
                     saveAsAction,printAction;

        }
        // version #8 incorprorate the tooltip argunents
       //VERSION # 7 add inner class for the element type menus

       class TypeAction extends AbstractAction
        {
          TypeAction(String name, int typeID)
           {
             super(name);
             this.typeID = typeID;
             String iconFileName = "Images/" + name + ".gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
           }

            //version #8 constructor add for the tooltip
            TypeAction(String name, int typeID, String tooltip)
             {
               this(name, typeID);
               if(tooltip != null)
                putValue(SHORT_DESCRIPTION, tooltip);
             }

            public void actionPerformed(ActionEvent e)
            {
             int  elementType;
              elementType =  typeID;

              //version #9  to update the status bar when the element type 
changes
              statusBar.setTypePane(typeID);
            }

            private int typeID;
         }

        //version #8 add a constructor for the tooltip
        // version # 7 add an inner class for the color type menus
        class ColorAction extends AbstractAction
         {
           public ColorAction(String name, Color color)
            {
              super(name);
              this.color = color;
              String iconFileName = "Images/" + name + ".gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
            }

            public ColorAction(String name, Color color, String tooltip)
             {
                this(name,color);
                if(tooltip != null)
                  putValue(SHORT_DESCRIPTION,tooltip);
             }

            public void actionPerformed(ActionEvent e)
             {
              Color elementColor;
              elementColor = color;

              //this is temporary . Just to show it works
              getContentPane().setBackground(color);

              //version #9 to update the status bar when the color element 
changes
              statusBar.setColorPane(color);
             }
            private Color color;
         }

        //add helper method
        private JMenuItem addMenuItem(JMenu menu, Action action)
         {
           //add the menu item
           JMenuItem item = menu.add(action);

           KeyStroke keystroke = (KeyStroke)action.getValue
(action.ACCELERATOR_KEY);
           if(keystroke != null)
            item.setAccelerator(keystroke);

            return item;
         }

              //declare colorActionreference variables
        private ColorAction redAction;
        private ColorAction yellowAction;
        private ColorAction greenAction;
        private ColorAction blueAction;

        //for the members that will store references to the TypeAction 
objects
        private TypeAction lineAction, rectangleAction, circleAction, 
curveAction;

       //create window menu bar
       private JMenuBar myMenuBar = new JMenuBar();

        // version #7  create a toolbar
        private JToolBar myToolBar = new JToolBar();

          //version #9 define the status bar as a data member
          StatusBar statusBar = new StatusBar();

          private JButton addToolBarButton(Action action)
           {
            //add a toolbar button
            JButton myJButton = myToolBar.add(action);

            // Add toolbar button border
            myJButton.setBorder(BorderFactory.createRaisedBevelBorder());

            //ensure no text on the button
            myJButton.setText(null);
            return myJButton;
          }


                     //implementation to handle the About menu tiem's 
events
        public void actionPerformed(ActionEvent e)
         {
          if (e.getSource() == aboutItem)
           {
             //create about dialog with the app window as parent
             JOptionPane.showMessageDialog(this, "About 
Sketcher", "Sketcher Copywrite Ivor Horton 2000", "About Sketcher", 
JOptionPane.INFORMATION_MESSAGE);

           }
          }


        //version 10 part 2
        private JMenuItem aboutItem;

       //variable to hold the current element color
       private Color myElementColor = DEFAULT_ELEMENT_COLOR;

       //variable to hold the current element type
       private int myElementTYPE =  DEFAULT_ELEMENT_TYPE;
     }

package sketchercalculator;

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

//Sketcher Program version  #10
// part 1 adding a modal dialog  part 2 add a Help menu with an about item

public class SketcherCalculator
{
  //declare the application window reference variable as private
  private static SketchFrame myJFrame;

  //declare the application window object
  private static SketcherCalculator theApp;

  public SketcherCalculator()
   {
     myJFrame =  new SketchFrame("Sketcher , Enjoying the design process", 
theApp);

     myJFrame.setSize(400,400);

     Dimension myFrameSize = myJFrame.getSize();
     Toolkit myToolkit = myJFrame.getToolkit();
     Dimension myScreenSize = myToolkit.getDefaultToolkit().getScreenSize
();

     //center the frame on the screen
     myJFrame.setLocation((myScreenSize.width -  myFrameSize.width)/2,
                         (myScreenSize.height - myFrameSize.height)/2);

    //charnge the backgroud color of the content pane
    myJFrame.getContentPane().setBackground(Color.yellow);

    //create the WindowAdapter object that is the listener for the 
myJFrame object
    myJFrame.addWindowListener(new WindowHandler() );

    myJFrame.setVisible(true);
   }

   //Handler class for window events
   class WindowHandler extends WindowAdapter
    {
      //Handler method for window closing event
        public void windowClosing(WindowEvent e)
           {
             //release window resources
             myJFrame.dispose();

             //end the application
             System.exit(0);
           }
    }

   public static void main(String[] args)
    {
      new SketcherCalculator();
      new Calculator6();
    }
}

package sketchercalculator;


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

public class Calculator6 extends JFrame
 {
   private Container contentPane;

   private JPanel leftPanel;
   private JPanel centerPanel;
   private JPanel buttonPanel;
   private JTextField input1TextField;
   private JTextField input2TextField;
   private JLabel answerLabel;
   private JButton plusButton;
   private JButton minusButton;
   private JButton multiplyButton;
   private JButton divideButton;

   //constructor

   public Calculator6()
    {
     //invoke the generic JFrame calculator
      super(" Calculator");

     //the content pane container is now declared to be
     // an attribute. "this"is unnecessary

     contentPane = this.getContentPane();
     this.setSize(250,100);

     //center the frame of the screen
     Dimension frameSize = this.getSize();
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    this.setLocation( ( screenSize.width - frameSize.width) / 2,
                    (screenSize.height = frameSize.height) / 2);

    leftPanel = new JPanel();
    leftPanel.setLayout(new GridLayout(3,1));
    leftPanel.add(new JLabel("Input 1:   "));
    leftPanel.add(new JLabel("input 2:   "));
    leftPanel.add(new JLabel("Answer:   "));
    contentPane.add(leftPanel,BorderLayout.WEST);

    centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(3,1));
    input1TextField = new JTextField(10);
    input2TextField = new JTextField(10);
    answerLabel = new JLabel();
    centerPanel.add(input1TextField);
    centerPanel.add(input2TextField);
    centerPanel.add(answerLabel);
    contentPane.add(centerPanel,BorderLayout.CENTER);

    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(5,1) );
    plusButton = new JButton("+");
    minusButton = new JButton("-");
    multiplyButton = new JButton("*");
    divideButton = new JButton("/");

    buttonPanel.add(plusButton);
    buttonPanel.add(minusButton);
    buttonPanel.add(multiplyButton);
    buttonPanel.add(divideButton);

    contentPane.add(buttonPanel, BorderLayout.EAST);

    //add behaviors
    ActionListener l = new ActionListener()
     {
      public void actionPerformed(ActionEvent e)
       {
         double d1 = new Double(input1TextField.getText() ).doubleValue();
         double d2 = new Double(input2TextField.getText() ).doubleValue();

          if (e.getSource() == plusButton)
             answerLabel.setText(" " + (d1 + d2) );

          else
           if(e.getSource() == minusButton)
            answerLabel.setText(" " + (d1 - d2) );

          else
          if(e.getSource() == multiplyButton)
           answerLabel.setText(" " + (d1 * d2) );

          else
          if(e.getSource() == divideButton)
            answerLabel.setText(" " + (d1 / d2) );

           else
             answerLabel.setText("Input invalid");

      }
   };

   plusButton.addActionListener(l);
   minusButton.addActionListener(l);
   multiplyButton.addActionListener(l);
   divideButton.addActionListener(l);


   //set a WindowListiner to terminate the application when the
   //user closes the window.
   //the listener is an instance of an anonymous inner class

   WindowListener w = new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
       {
         Calculator6.this.dispose();
         System.exit(0);
       }
     };

     //register it with the frame since it is the JFrame
     //object that generates the window closing event

     this.addWindowListener(w);

     this.setVisible(true);

    }

 }

package sketchercalculator;

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

//Sketcher Program version  # 10
// part 1 adding a modal dialog  part 2 add a Help menu with an about item
class StatusBar extends JPanel implements Constants
{
  public StatusBar()
   {
     setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3));
     setBackground(Color.lightGray);
     setBorder(BorderFactory.createLineBorder(Color.lightGray));
     setColorPane(DEFAULT_ELEMENT_COLOR);
     setTypePane(DEFAULT_ELEMENT_TYPE);

     //add color pane to status bar
     add(colorPane);

     //add type pane to status bar
     add(typePane);
   }
     //add color panel label
     public void setColorPane(Color color)
      {
        // part 2 ...Code to set color pane text
        String text;
        if(color.equals(Color.red))
          text = "RED";

          else if (color.equals(Color.yellow))
           text = "YELLOW";

          else if (color.equals(Color.green))
           text =  "GREEN";

          else if ( color.equals(Color.blue))
           text = "BLUE";

           else
            text = "CUSTOM COLOR";

           colorPane.setForeground(color);

           //set the pane text
           colorPane.setText(text);
       }

       //part 2 set type pane label
       public void setTypePane(int elementType)
        {
          //declare variable for the type pane
          String text = "LINE";

          switch(elementType)
           {
             case LINE:
              text = "LINE";
              break;
             case RECTANGLE:
              text = "RECTANGLE";
              break;
             case CIRCLE:
              text = "CIRCLE";
              break;
            case CURVE:
             text = "CURVE";
             break;
           }
        //set the pane text
        typePane.setText(text);
      }
       //panes in the  status bar
       private StatusPane colorPane = new StatusPane("BLUE");
       private StatusPane typePane = new StatusPane("LINE");

       //Class defining a status bar pane
           class StatusPane extends JLabel
           {
             public StatusPane (String text)
              {
                setBackground(Color.lightGray);
                setForeground(Color.black);

                //set the fixed font
                setFont(paneFont);

                //center the pane text
                setHorizontalAlignment(CENTER);

                setBorder(BorderFactory.createBevelBorder
(BevelBorder.LOWERED));

                setPreferredSize(new Dimension(100,20));

                //set the text in the pane
                setText(text);
              }
              //font for pane text
              private Font paneFont = new Font("Serif", Font.BOLD, 10);
             }
   }


package sketchercalculator;


//Sketcher version #10
// part 1 adding a modal dialog  part 2 add a Help menu with an about item
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

public interface Constants
{
  //Element type definitions
  int LINE = 101;
  int RECTANGLE = 102;
  int CIRCLE = 103;
  int CURVE = 104;

  //Initial conditions
  int DEFAULT_ELEMENT_TYPE = LINE;
  Color DEFAULT_ELEMENT_COLOR = Color.blue;
}




  Return to Index