M
matrix584
I gotta make another class to use this, and i have no idea how...any
ideas
(please dont post the answer, i just need a little guidance)
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import javax.swing.border.*;
class HPCalculatorApp
extends JFrame
implements ActionListener
{
private JMenuBar menu;
private JMenuItem fileExit;
private static final int
ZERO = 0,
ONE = 1,
TWO = 2,
THREE = 3,
FOUR = 4,
FIVE = 5,
SIX = 6,
SEVEN = 7,
EIGHT = 8,
NINE = 9,
SIGN = 10,
DOT = 11,
ADD = 12,
SUBTRACT = 13,
MULTIPLY = 14,
DIVIDE = 15,
CLEAR = 16,
BACK = 17,
STORE = 18,
RESTORE = 19,
ENTER = 20,
NUM_BUTTONS = 21;
private static final String[] LABELS = {
"+/-", ".", "+", "-", "*", "/", "C", "<-", "MS", "MR", "ENTER"
};
private JButton[] bButtons;
private JLabel visor;
private Font font;
private HPCalculator hp;
private String number;
private boolean entered;
public HPCalculatorApp()
{
hp = new HPCalculator();
number = new String();
entered = false;
font = new Font("SansSerif", Font.PLAIN, 12);
setTitle( "HP Calculator" );
setSize(180, 240);
BorderLayout mainLayout = new BorderLayout();
getContentPane().setLayout(mainLayout);
mainLayout.setVgap(1);
// Create visor
visor = new JLabel("", SwingConstants.RIGHT);
visor.setFont(font);
visor.setPreferredSize(new Dimension(100, 20));
visor.setBorder(new BevelBorder(BevelBorder.LOWERED));
visor.setBackground(Color.white);
getContentPane().add(visor, BorderLayout.NORTH);
// Create buttons
int WIDTH = 30;
int HEIGHT = 30;
bButtons = new JButton[NUM_BUTTONS];
for (int i=0; i<NUM_BUTTONS-1; i++) {
bButtons = new JButton(Integer.toString(i));
bButtons.setPreferredSize(new Dimension(WIDTH, HEIGHT));
bButtons.setForeground(Color.blue);
bButtons.setFont(font);
bButtons.setMargin(new Insets(1, 1, 1, 1));
bButtons.addActionListener(this);
}
for (int i=SIGN; i<=ENTER; i++) {
bButtons = new JButton(LABELS[i-SIGN]);
bButtons.setPreferredSize(new Dimension(WIDTH, HEIGHT));
bButtons.setForeground(Color.red);
bButtons.setFont(font);
bButtons.setMargin(new Insets(1, 1, 1, 1));
bButtons.addActionListener(this);
}
bButtons[SIGN].setForeground(Color.blue);
bButtons[DOT].setForeground(Color.blue);
bButtons[ENTER].setPreferredSize(new Dimension(100, 18));
bButtons[ENTER].setForeground(Color.white);
bButtons[ENTER].setBackground(new Color(155, 155, 155));
bButtons[ENTER].setMargin(new Insets(1, 1, 1, 1));
int VGAP = 1, HGAP = 1;
JPanel centerPanel = new JPanel();
GridLayout grid = new GridLayout(4, 1);
grid.setHgap(HGAP);
grid.setVgap(VGAP);
centerPanel.setLayout(grid);
JPanel row1 = new JPanel();
JPanel row2 = new JPanel();
JPanel row3 = new JPanel();
JPanel row4 = new JPanel();
JPanel row5 = new JPanel();
FlowLayout defLayout;
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row1.setLayout(defLayout);
row1.add(bButtons[NINE]);
row1.add(bButtons[EIGHT]);
row1.add(bButtons[SEVEN]);
row1.add(bButtons[ADD]);
row1.add(bButtons[BACK]);
centerPanel.add(row1);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row2.setLayout(defLayout);
row2.add(bButtons[SIX]);
row2.add(bButtons[FIVE]);
row2.add(bButtons[FOUR]);
row2.add(bButtons[SUBTRACT]);
row2.add(bButtons[CLEAR]);
centerPanel.add(row2);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row3.setLayout(defLayout);
row3.add(bButtons[THREE]);
row3.add(bButtons[TWO]);
row3.add(bButtons[ONE]);
row3.add(bButtons[MULTIPLY]);
row3.add(bButtons[STORE]);
centerPanel.add(row3);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row4.setLayout(defLayout);
row4.add(bButtons[ZERO]);
row4.add(bButtons[SIGN]);
row4.add(bButtons[DOT]);
row4.add(bButtons[DIVIDE]);
row4.add(bButtons[RESTORE]);
centerPanel.add(row4);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row5.setLayout(defLayout);
row5.add(bButtons[ENTER]);
centerPanel.add(row5);
getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(row5, BorderLayout.SOUTH);
// Create menu
menu = new JMenuBar();
setJMenuBar(menu);
fileExit = new JMenuItem("Exit");
fileExit.setFont(font);
fileExit.addActionListener(this);
menu.add(fileExit);
pack();
//Anonymous inner class to terminate program.
this.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit( 0 );
}
} );//end addWindowListener
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof JMenuItem) {
JMenuItem i = (JMenuItem)e.getSource();
if (i == fileExit)
System.exit(0);
}
if (e.getSource() instanceof JButton) {
if (processNumber(e.getSource()))
return;
if (processOperation(e.getSource()))
return;
if (processClear(e.getSource()))
return;
if (processMemory(e.getSource()))
return;
if (processEnter(e.getSource()))
return;
}
}
public static void main(String args[])
{
// Create an instance of the test application
HPCalculatorApp mainFrame = new HPCalculatorApp();
mainFrame.setVisible(true);
}
private void backspace()
{
if (number.length() == 0)
return;
if (entered) {
clearNumber();
return;
}
number = number.substring(0, number.length()-1);
if (number.length() == 1 && number.startsWith("-"))
clearNumber();
else if (number.length() == 2 && number.startsWith("-0"))
clearNumber();
else if (number.length() == 1 && number.startsWith("0"))
clearNumber();
}
private void clearVisor()
{
clearNumber();
updateVisor();
}
private void clearNumber()
{
number = "";
}
private void updateVisor()
{
visor.setText(number);
}
private double getNumber(String n)
{
double num;
try {
num = (double)Double.parseDouble(n);
}
catch (NumberFormatException e) {
return 0.0;
}
return num;
}
private boolean checkNumber(String n)
{
try {
Double.parseDouble(n);
}
catch (NumberFormatException e) {
return false;
}
return true;
}
private boolean addToNumber(int i)
{
if (entered == true || number.length() == 0) {
entered = false;
if (i != 0)
number = Integer.toString(i);
else
number = "0.";
return true;
}
String n = new String(number);
n += i;
if (!checkNumber(n))
return false;
number = n;
entered = false;
return true;
}
private void toNumber(double n)
{
number = Double.toString(n);
}
private boolean invertSign()
{
if (number.length() == 0)
return false;
if (number.startsWith("-"))
number = number.substring(1);
else
number = "-" + number;
entered = false;
return true;
}
private boolean addDot()
{
if (number.length() == 0) {
entered = false;
number = "0.";
return true;
}
if (number.indexOf('.') != -1)
return false;
number += '.';
//?? entered = false;
return true;
}
private boolean processNumber(Object o)
{
for (int i=ZERO; i<=NINE; i++) {
if (bButtons == (JButton)o) {
if (addToNumber(i)) {
updateVisor();
return true;
}
}
}
if (bButtons[SIGN] == (JButton)o) {
if (invertSign()) {
updateVisor();
return true;
}
}
if (bButtons[DOT] == (JButton)o) {
if (addDot()) {
updateVisor();
return true;
}
}
return false;
}
private void add()
{
if (!entered)
enter();
try {
hp.add();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private void subtract()
{
if (!entered)
enter();
try {
hp.subtract();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private void divide()
{
if (!entered)
enter();
try {
hp.divide();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private void multiply()
{
if (!entered)
enter();
try {
hp.multiply();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private boolean processOperation(Object o)
{
if (bButtons[ADD] == (JButton)o) {
add();
return true;
}
else if (bButtons[SUBTRACT] == (JButton)o) {
subtract();
return true;
}
else if (bButtons[DIVIDE] == (JButton)o) {
divide();
return true;
}
else if (bButtons[MULTIPLY] == (JButton)o) {
multiply();
return true;
}
return false;
}
private boolean processClear(Object o)
{
if (bButtons[CLEAR] == (JButton)o) {
clearVisor();
return true;
}
else if (bButtons[BACK] == (JButton)o) {
backspace();
updateVisor();
return true;
}
return false;
}
private boolean processMemory(Object o)
{
if (!checkNumber(number))
return false;
double n;
if (bButtons[STORE] == (JButton)o) {
n = getNumber(number);
hp.store(n);
return true;
}
else if (bButtons[RESTORE] == (JButton)o) {
n = hp.restore();
toNumber(n);
updateVisor();
entered = false;
return true;
}
return false;
}
private boolean enter()
{
if (!checkNumber(number))
return false;
hp.enter(getNumber(number));
entered = true;
return true;
}
private boolean processEnter(Object o)
{
if (bButtons[ENTER] == (JButton)o) {
return enter();
}
return false;
}
}
ideas
(please dont post the answer, i just need a little guidance)
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import javax.swing.border.*;
class HPCalculatorApp
extends JFrame
implements ActionListener
{
private JMenuBar menu;
private JMenuItem fileExit;
private static final int
ZERO = 0,
ONE = 1,
TWO = 2,
THREE = 3,
FOUR = 4,
FIVE = 5,
SIX = 6,
SEVEN = 7,
EIGHT = 8,
NINE = 9,
SIGN = 10,
DOT = 11,
ADD = 12,
SUBTRACT = 13,
MULTIPLY = 14,
DIVIDE = 15,
CLEAR = 16,
BACK = 17,
STORE = 18,
RESTORE = 19,
ENTER = 20,
NUM_BUTTONS = 21;
private static final String[] LABELS = {
"+/-", ".", "+", "-", "*", "/", "C", "<-", "MS", "MR", "ENTER"
};
private JButton[] bButtons;
private JLabel visor;
private Font font;
private HPCalculator hp;
private String number;
private boolean entered;
public HPCalculatorApp()
{
hp = new HPCalculator();
number = new String();
entered = false;
font = new Font("SansSerif", Font.PLAIN, 12);
setTitle( "HP Calculator" );
setSize(180, 240);
BorderLayout mainLayout = new BorderLayout();
getContentPane().setLayout(mainLayout);
mainLayout.setVgap(1);
// Create visor
visor = new JLabel("", SwingConstants.RIGHT);
visor.setFont(font);
visor.setPreferredSize(new Dimension(100, 20));
visor.setBorder(new BevelBorder(BevelBorder.LOWERED));
visor.setBackground(Color.white);
getContentPane().add(visor, BorderLayout.NORTH);
// Create buttons
int WIDTH = 30;
int HEIGHT = 30;
bButtons = new JButton[NUM_BUTTONS];
for (int i=0; i<NUM_BUTTONS-1; i++) {
bButtons = new JButton(Integer.toString(i));
bButtons.setPreferredSize(new Dimension(WIDTH, HEIGHT));
bButtons.setForeground(Color.blue);
bButtons.setFont(font);
bButtons.setMargin(new Insets(1, 1, 1, 1));
bButtons.addActionListener(this);
}
for (int i=SIGN; i<=ENTER; i++) {
bButtons = new JButton(LABELS[i-SIGN]);
bButtons.setPreferredSize(new Dimension(WIDTH, HEIGHT));
bButtons.setForeground(Color.red);
bButtons.setFont(font);
bButtons.setMargin(new Insets(1, 1, 1, 1));
bButtons.addActionListener(this);
}
bButtons[SIGN].setForeground(Color.blue);
bButtons[DOT].setForeground(Color.blue);
bButtons[ENTER].setPreferredSize(new Dimension(100, 18));
bButtons[ENTER].setForeground(Color.white);
bButtons[ENTER].setBackground(new Color(155, 155, 155));
bButtons[ENTER].setMargin(new Insets(1, 1, 1, 1));
int VGAP = 1, HGAP = 1;
JPanel centerPanel = new JPanel();
GridLayout grid = new GridLayout(4, 1);
grid.setHgap(HGAP);
grid.setVgap(VGAP);
centerPanel.setLayout(grid);
JPanel row1 = new JPanel();
JPanel row2 = new JPanel();
JPanel row3 = new JPanel();
JPanel row4 = new JPanel();
JPanel row5 = new JPanel();
FlowLayout defLayout;
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row1.setLayout(defLayout);
row1.add(bButtons[NINE]);
row1.add(bButtons[EIGHT]);
row1.add(bButtons[SEVEN]);
row1.add(bButtons[ADD]);
row1.add(bButtons[BACK]);
centerPanel.add(row1);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row2.setLayout(defLayout);
row2.add(bButtons[SIX]);
row2.add(bButtons[FIVE]);
row2.add(bButtons[FOUR]);
row2.add(bButtons[SUBTRACT]);
row2.add(bButtons[CLEAR]);
centerPanel.add(row2);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row3.setLayout(defLayout);
row3.add(bButtons[THREE]);
row3.add(bButtons[TWO]);
row3.add(bButtons[ONE]);
row3.add(bButtons[MULTIPLY]);
row3.add(bButtons[STORE]);
centerPanel.add(row3);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row4.setLayout(defLayout);
row4.add(bButtons[ZERO]);
row4.add(bButtons[SIGN]);
row4.add(bButtons[DOT]);
row4.add(bButtons[DIVIDE]);
row4.add(bButtons[RESTORE]);
centerPanel.add(row4);
defLayout = new FlowLayout();
defLayout.setHgap(HGAP);
defLayout.setVgap(VGAP);
row5.setLayout(defLayout);
row5.add(bButtons[ENTER]);
centerPanel.add(row5);
getContentPane().add(centerPanel, BorderLayout.CENTER);
getContentPane().add(row5, BorderLayout.SOUTH);
// Create menu
menu = new JMenuBar();
setJMenuBar(menu);
fileExit = new JMenuItem("Exit");
fileExit.setFont(font);
fileExit.addActionListener(this);
menu.add(fileExit);
pack();
//Anonymous inner class to terminate program.
this.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit( 0 );
}
} );//end addWindowListener
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof JMenuItem) {
JMenuItem i = (JMenuItem)e.getSource();
if (i == fileExit)
System.exit(0);
}
if (e.getSource() instanceof JButton) {
if (processNumber(e.getSource()))
return;
if (processOperation(e.getSource()))
return;
if (processClear(e.getSource()))
return;
if (processMemory(e.getSource()))
return;
if (processEnter(e.getSource()))
return;
}
}
public static void main(String args[])
{
// Create an instance of the test application
HPCalculatorApp mainFrame = new HPCalculatorApp();
mainFrame.setVisible(true);
}
private void backspace()
{
if (number.length() == 0)
return;
if (entered) {
clearNumber();
return;
}
number = number.substring(0, number.length()-1);
if (number.length() == 1 && number.startsWith("-"))
clearNumber();
else if (number.length() == 2 && number.startsWith("-0"))
clearNumber();
else if (number.length() == 1 && number.startsWith("0"))
clearNumber();
}
private void clearVisor()
{
clearNumber();
updateVisor();
}
private void clearNumber()
{
number = "";
}
private void updateVisor()
{
visor.setText(number);
}
private double getNumber(String n)
{
double num;
try {
num = (double)Double.parseDouble(n);
}
catch (NumberFormatException e) {
return 0.0;
}
return num;
}
private boolean checkNumber(String n)
{
try {
Double.parseDouble(n);
}
catch (NumberFormatException e) {
return false;
}
return true;
}
private boolean addToNumber(int i)
{
if (entered == true || number.length() == 0) {
entered = false;
if (i != 0)
number = Integer.toString(i);
else
number = "0.";
return true;
}
String n = new String(number);
n += i;
if (!checkNumber(n))
return false;
number = n;
entered = false;
return true;
}
private void toNumber(double n)
{
number = Double.toString(n);
}
private boolean invertSign()
{
if (number.length() == 0)
return false;
if (number.startsWith("-"))
number = number.substring(1);
else
number = "-" + number;
entered = false;
return true;
}
private boolean addDot()
{
if (number.length() == 0) {
entered = false;
number = "0.";
return true;
}
if (number.indexOf('.') != -1)
return false;
number += '.';
//?? entered = false;
return true;
}
private boolean processNumber(Object o)
{
for (int i=ZERO; i<=NINE; i++) {
if (bButtons == (JButton)o) {
if (addToNumber(i)) {
updateVisor();
return true;
}
}
}
if (bButtons[SIGN] == (JButton)o) {
if (invertSign()) {
updateVisor();
return true;
}
}
if (bButtons[DOT] == (JButton)o) {
if (addDot()) {
updateVisor();
return true;
}
}
return false;
}
private void add()
{
if (!entered)
enter();
try {
hp.add();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private void subtract()
{
if (!entered)
enter();
try {
hp.subtract();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private void divide()
{
if (!entered)
enter();
try {
hp.divide();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private void multiply()
{
if (!entered)
enter();
try {
hp.multiply();
}
catch (Error e) {
return;
}
toNumber(hp.result());
updateVisor();
entered = true;
}
private boolean processOperation(Object o)
{
if (bButtons[ADD] == (JButton)o) {
add();
return true;
}
else if (bButtons[SUBTRACT] == (JButton)o) {
subtract();
return true;
}
else if (bButtons[DIVIDE] == (JButton)o) {
divide();
return true;
}
else if (bButtons[MULTIPLY] == (JButton)o) {
multiply();
return true;
}
return false;
}
private boolean processClear(Object o)
{
if (bButtons[CLEAR] == (JButton)o) {
clearVisor();
return true;
}
else if (bButtons[BACK] == (JButton)o) {
backspace();
updateVisor();
return true;
}
return false;
}
private boolean processMemory(Object o)
{
if (!checkNumber(number))
return false;
double n;
if (bButtons[STORE] == (JButton)o) {
n = getNumber(number);
hp.store(n);
return true;
}
else if (bButtons[RESTORE] == (JButton)o) {
n = hp.restore();
toNumber(n);
updateVisor();
entered = false;
return true;
}
return false;
}
private boolean enter()
{
if (!checkNumber(number))
return false;
hp.enter(getNumber(number));
entered = true;
return true;
}
private boolean processEnter(Object o)
{
if (bButtons[ENTER] == (JButton)o) {
return enter();
}
return false;
}
}