0
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
   public static void main (String [] args)
   {
      JFrame frame = new CalculatorFrame ();
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.show ();

}
}

class CalculatorFrame extends JFrame
{
 JTextField jtf;
JLabel jlb;
JTextField jtf1;
JLabel jlb1;
JTextField jtf2;
JLabel jlb2;
JButton jbt;
JButton jbt1;
JButton jbt2;
JButton jbt3;
   public CalculatorFrame ()
   {

      setTitle ("SIMPLE CALCULATOR");

      Container contentPane = getContentPane ();
      contentPane.setLayout(new FlowLayout());

jlb=new JLabel();
jlb.setText("integer1");
contentPane.add(jlb);

jtf=new JTextField(15);
contentPane.add(jtf);

jlb1=new JLabel();
jlb1.setText("integer2");
contentPane.add(jlb1);

jtf1=new JTextField(15);
contentPane.add(jtf1);

jlb2=new JLabel();
jlb2.setText("result");
contentPane.add(jlb2);

jtf2=new JTextField(15);
contentPane.add(jtf2);


JPanel buttonPanel = new JPanel ();

    addButton (buttonPanel, "add",
         new ActionListener ()
            {  
               public void actionPerformed (ActionEvent evt)
               {
    float a=Float.parseFloat(jtf.getText()), b=Float.parseFloat(jtf1.getText()), temp;
    jtf2.setText("");
                  temp = a+b; 
        jtf2.setText("" + temp);
               }
            });
    addButton (buttonPanel, "subtract",
         new ActionListener ()
            {  
               public void actionPerformed (ActionEvent evt)
               {
    float a=Float.parseFloat(jtf.getText()), b=Float.parseFloat(jtf1.getText()), temp;
    jtf2.setText("");
                  temp = a-b; 
        jtf2.setText("" + temp);
               }
            });

    addButton (buttonPanel, "multiply",
         new ActionListener ()
            {  
               public void actionPerformed (ActionEvent evt)
               {
    float a=Float.parseFloat(jtf.getText()), b=Float.parseFloat(jtf1.getText()), temp;
    jtf2.setText("");
                  temp = a*b; 
        jtf2.setText("" + temp);
               }
            });

    addButton (buttonPanel, "divide",
         new ActionListener ()
            {  
               public void actionPerformed (ActionEvent evt)
               {
    float a=Float.parseFloat(jtf.getText()), b=Float.parseFloat(jtf1.getText()), temp;
    jtf2.setText("");
                  temp = a/b; 
        jtf2.setText("" + temp);
               }
            });

      addButton (buttonPanel, "Close",
         new ActionListener ()
            {
               public void actionPerformed (ActionEvent evt)
               {
                  System.exit (0);
               }
            });
      contentPane.add (buttonPanel, BorderLayout.SOUTH);
   }


   public void addButton (Container c, String title,
      ActionListener listener)
   {
      JButton button = new JButton (title);
      c.add (button);
      button.addActionListener (listener);
   }

 }

when i compile the above program using the below statement

C:\Program Files\Java\jdk1.5.0_14\bin>javac Calculator.java
Note: Calculator.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details

but if i run the program i get the output....
for creating a jar file: i followed the following procedure

  1. first,i created a mainClass.txt file containing the info
    Main-Class: Calculator
  2. i used the statement
    C:\Program Files\Java\jdk1.5.0_14\bin>jar cmf mainClass.txt Calculator.jar *.class
    to create a jar file
  3. i had created a Calculator.jar file now..now when i try to execute the jar file by using
    C:\Program Files\Java\jdk1.5.0_14\bin>java -jar Calculator.jar
    i get the following error

    Exception in thread "main" java.lang.NoClassDefFoundError: Calculator

what should i do to execute my Calculator.jar file?

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by jwenting
0

Place ALL your classes in packages. While not officially required by the language specification it IS required by many tools and even were it not a very good idea.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.