I made a simple calculator in java and I want to zip it up in an excutable jar file. I have 3 classes including my main in my .java file and am having trouble creating the jar archive. The source code for this program is:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
// Frame for button panel
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
// sets title & icon image for window
setTitle("Java Calculator v1.0 by D. Burkland");
Image img = kit.getImage("calc.gif");
setIconImage(img);
// add panel to frame
CalculatorPanel calculator = new CalculatorPanel();
add(calculator);
pack();
// centers frame
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
// center & set dimensions of frame
setSize(width, height);
setLocation(screenWidth / 3, screenHeight / 3);
// set file chooser
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
// set up menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu filemenu = new JMenu("File");
JMenu helpmenu = new JMenu("Help");
menuBar.add(filemenu);
menuBar.add(helpmenu);
// action of the exit text
JMenuItem exitItem = new JMenuItem("Exit");
filemenu.add(exitItem);
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
JMenuItem helpItem = new JMenuItem("About");
helpmenu.add(helpItem);
helpItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null, "Java Calculator By: D. Burkland Summer 2006");
}
});
}
public static final int width = 350;
public static final int height = 200;
private JFileChooser chooser;
}
// Calculator panel
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
// add the display
display = new JButton("Hello");
display.setEnabled(false);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
// adds the buttons in a 4 x 4 grid
calculator = new JPanel();
calculator.setLayout(new GridLayout(4, 4));
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
add(calculator, BorderLayout.CENTER);
}
/**
Adds a button to the center panel
@param lable the button label
@param listener the button listener
*/
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
calculator.add(button);
}
// Inserts the button action string to the end of the display
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
// Executes the command that the button action string demotes
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
// Carries out the pending calculation
public void calculate(double x)
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}
private JButton display;
private JPanel calculator;
private double result;
private String lastCommand;
private boolean start;
}
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame calculator = new CalculatorFrame();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.setVisible(true);
}
}
I am using Eclipse to create the jar files so if anybody has any advice I would be very grateful if you shared it.
Thanks,
Danny