hey guys,
finally finished it and it works fine.. if you want some simpler code then what i'd seen on the other forum ... use mine.
i did all of this straight from the lecture notes so there shouldn't be anything too confusing
but i will admit it took me a WHILE to get this working.. rusty i guess.. heh..
later
Jake
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.JFrame ;
public class MenuJFrame
extends JFrame
implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 400;
// without putting these here, they are unaccessable by the actionlistener
private JPanel westPanel;
private JPanel eastPanel;
private JTextField one;
private JTextField two;
private JTextField answer;
public MenuJFrame()
{
//main window
super("Java Calculator");
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(0,2));
//west panel
westPanel = new JPanel();
westPanel.setLayout(new GridLayout(3,0));
this.add(westPanel);
//east panel
eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(4,0));
this.add(eastPanel);
//text fields
one = new JTextField(30);
two = new JTextField(30);
westPanel.add(one);
westPanel.add(two);
//answer
answer = new JTextField("0.0");
//non-editable text field
answer.setEditable(false);
westPanel.add(answer);
//buttons for east
JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("*");
JButton divide = new JButton("/");
//add buttons to eastPanel
eastPanel.add(add);
add.addActionListener(this);
eastPanel.add(subtract);
subtract.addActionListener(this);
eastPanel.add(multiply);
multiply.addActionListener(this);
eastPanel.add(divide) ;
divide.addActionListener(this);
//Menu Bar
//create a new menubar , this is ONLY the grey bar at the top w/ nothing in it!!!
JMenuBar myMenuBar = new JMenuBar();
//this.setJMenuBar is a method which auto positions the menu bar @ the top
this.setJMenuBar(myMenuBar);
//create two fields, File and Clear.
JMenu File = new JMenu("File");
JMenu Clear = new JMenu("Clear");
//create a menuItem quit
JMenuItem quit = new JMenuItem("Quit");
//add action listener to quit, so the program quits when clicked
quit.addActionListener(this);
// add quit to the file menu
File.add(quit);
//create menu items for clearing the fields
JMenuItem clearA = new JMenuItem("Clear A");
JMenuItem clearB = new JMenuItem("Clear B");
JMenuItem wipe = new JMenuItem("Clear All");
//add action listeners for each
clearA.addActionListener(this);
clearB.addActionListener(this);
wipe.addActionListener(this);
// add all three menu items to the menu 'Clear'
Clear.add(clearA);
Clear.add(clearB);
Clear.add(wipe);
// add file and clear to the menu bar
myMenuBar.add(File);
myMenuBar.add(Clear);
}
//ActionListener
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
//if the button is '+' then add the two fields and display answer in bottom box
if (cmd == "+")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) + (Double.parseDouble(two.getText())))));
}
//if the button is '-' then subtract the two fields and display answer in bottom box
if (cmd == "-")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) - (Double.parseDouble(two.getText())))));
}
//if the button is '*' then multiply the two fields and display answer in bottom box
if (cmd == "*")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) * (Double.parseDouble(two.getText())))));
}
//if the button is "/" then divide the two fields and display answer in bottom box
if (cmd == "/")
{
answer.setText((Double.toString((Double.parseDouble(one.getText())) / (Double.parseDouble(two.getText())))));
}
//if button is "Quit" then close the program
if (cmd == "Quit")
{
System.exit(0);
}
// if button is 'Clear A' then clear A
if (cmd == "Clear A")
{
one.setText("");
}
// if button is 'Clear B' then clear B
if (cmd == "Clear B")
{
two.setText("");
}
// if button is 'Clear All' then clear all fields and leave 0.0 in the answer field
if (cmd == "Clear All")
{
one.setText("");
two.setText("");
answer.setText("0.0");
}
}
}
public class JavaCalculator
{
public static void main(String[] args)
{
MenuJFrame MyWindow = new MenuJFrame();
MyWindow.setVisible (true);
}
}
Subscribe to:
Post Comments (Atom)
2 comments:
Did you start on the latest lab? I was wondering if you knew how to delete a single element from a linked list. Thanks in advance.
Good post.
Post a Comment