hi
i am currently building this calculator program and i need help adding a menu to this program, i have tried useing guides etc of the net but still nothing happen's.
the menu has to edit, view, help and sub menu's for
edit: copy, paste
view: standard, scientific, digit grouping
help: about calculator
can anyone help me out with this that i would really apreciate it. Also if anyone can help me ut with the look or functionality of it that would be great.
i am currently useing textpad to code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(
String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
A frame with a calculator panel.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
Container contentPane = getContentPane();
CalculatorPanel panel = new CalculatorPanel();
contentPane.add(panel);
pack();
}
}
/**
A panel with calculator buttons and a result display.
*/
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
// add the display
display = new JTextField("0");
display.setHorizontalAlignment(JTextField.RIGHT);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
ActionListener clear = new ClearAction();
// Numbers panel
JPanel numbersPanel = new JPanel();
numbersPanel.setLayout(new GridLayout(4, 6));
JButton n1Button = new JButton ("1");
JButton n2Button = new JButton ("2");
JButton n3Button = new JButton ("3");
JButton n4Button = new JButton ("4");
JButton n5Button = new JButton ("5");
JButton n6Button = new JButton ("6");
JButton n7Button = new JButton ("7");
JButton n8Button = new JButton ("8");
JButton n9Button = new JButton ("9");
JButton n0Button = new JButton ("0");
JButton ndecButton = new JButton (".");
JButton nposButton = new JButton ("+/-");
//NumbersPanel added
numbersPanel.add(n1Button);
numbersPanel.add(n2Button);
numbersPanel.add(n3Button);
numbersPanel.add(n4Button);
numbersPanel.add(n5Button);
numbersPanel.add(n6Button);
numbersPanel.add(n7Button);
numbersPanel.add(n8Button);
numbersPanel.add(n9Button);
numbersPanel.add(n0Button);
numbersPanel.add(nposButton);
numbersPanel.add(ndecButton);
//Numbers panel action listener added
n1Button.addActionListener(new InsertAction());
n2Button.addActionListener(new InsertAction());
n3Button.addActionListener(new InsertAction());
n4Button.addActionListener(new InsertAction());
n5Button.addActionListener(new InsertAction());
n6Button.addActionListener(new InsertAction());
n7Button.addActionListener(new InsertAction());
n8Button.addActionListener(new InsertAction());
n9Button.addActionListener(new InsertAction());
n0Button.addActionListener(new InsertAction());
ndecButton.addActionListener(new InsertAction());
nposButton.addActionListener(new CommandAction());
//command Panel Setup
JPanelcommandPanel = new JPanel();
commandPanel.setLayout(new GridLayout(4,2));
JButton sqrtButton = new JButton ("sqrt");
JButton timesButton = new JButton ("*");
JButton perButton = new JButton ("%");
JButton subButton = new JButton ("-");
JButton onexButton = new JButton ("1/x");
JButton plusButton = new JButton ("+");
JButton equalsButton = new JButton ("=");
JButton divideButton = new JButton ("/");
//command panels button added
commandPanel.add(divideButton);
commandPanel.add(sqrtButton);
commandPanel.add(timesButton);
commandPanel.add(perButton);
commandPanel.add(subButton);
commandPanel.add(onexButton);
commandPanel.add(plusButton);
commandPanel.add(equalsButton);
//Command Action Listeners for each button
sqrtButton.addActionListener(new CommandAction());
timesButton.addActionListener(new CommandAction());
perButton.addActionListener(new CommandAction());
subButton.addActionListener(new CommandAction());
onexButton.addActionListener(new CommandAction());
plusButton.addActionListener(new CommandAction());
equalsButton.addActionListener(new CommandAction());
divideButton.addActionListener(new CommandAction());
//Clear Panel Setup
JPanelclearPanel = new JPanel();
clearPanel.setLayout(new GridLayout(1,3));
JButton backButton = new JButton ("Backspace");
JButton ceButton = new JButton ("CE");
JButton cButton = new JButton ("C");
cButton.addActionListener(new ClearAction());
ceButton.addActionListener(new ClearAction());
//clear pannel buttons added
clearPanel.add(backButton);
clearPanel.add(ceButton);
clearPanel.add(cButton);
//Memory paneling
JPanelstoragePanel = new JPanel();
storagePanel.setLayout(new GridLayout(4,1));
JButton mcButton = new JButton ("MC");
JButton mrButton = new JButton ("MR");
JButton msButton = new JButton ("MS");
JButton mplusButton = new JButton ("M+");
//clear pannel buttons added
storagePanel.add(mcButton);
storagePanel.add(mrButton);
storagePanel.add(msButton);
storagePanel.add(mplusButton);
//grouping panel
JPanel groupbutton = new JPanel();
groupbutton.setLayout(new BorderLayout(1,1));
groupbutton.add(numbersPanel,BorderLayout.WEST);
groupbutton.add(commandPanel ,BorderLayout.EAST);
//grouping panel2
JPanel groupbutton2 = new JPanel();
groupbutton2.setLayout(new BorderLayout(1,1));
groupbutton2.add(groupbutton,BorderLayout.EAST);
groupbutton2.add(storagePanel ,BorderLayout.WEST);
//grouping clear button to top
JPanel groupbutton3 = new JPanel();
groupbutton3.setLayout(new BorderLayout(1,1));
groupbutton3.add(groupbutton2,BorderLayout.SOUTH);
groupbutton3.add(clearPanel ,BorderLayout.NORTH);
//Main Panel
add(display, BorderLayout.NORTH);
add(groupbutton3,BorderLayout.SOUTH);
//add(groupbutton2,BorderLayout.EAST);
//Color buttons_color = new Color(242,244,241);
// Color background_color = new Color(241,237,222);
//setBackground(background_color);
}
/**
This action inserts the button action string to the
end of the display text.
*/
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);
}
}
private class ClearAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
display.setText("0");
}
}
/**
This action executes the command that the button
action string denotes.
*/
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String command = evt.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.
@param x the value to be accumulated with the prior result.
*/
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;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}
private JTextField display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}