Java Calculator (Menus & Button Arrays)

The decimal point button has a glitch. More than one decimal point can

Page 1 of 1

8 Replies - 6583 Views - Last Post: 27 August 2009 - 09:30 AM Rate Topic: -----

#1 ReinaDark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-August 09

Java Calculator (Menus & Button Arrays)

Posted 20 August 2009 - 06:49 AM

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Calculator extends Frame implements ActionListener
 {
	private Button keys[];
	private Panel keypad;
	private TextField lcd;
	private double op1;
	private boolean first;
	private boolean foundKey;
	private boolean clearText;
	private int lastOp;
	private DecimalFormat calcPattern;
	
public Calculator() 
   	{ 
   		// create an instance of the menu
   		MenuBar mnuBar = new MenuBar();
   		setMenuBar(mnuBar);

		// create an instance of the File menu
		Menu mnuFile = new Menu("File", true);
		mnuBar.add(mnuFile);
			MenuItem mnuFileExit = new MenuItem("Exit");
			mnuFile.add(mnuFileExit);
			
		// construct and populate the Edit menu
		Menu mnuEdit = new Menu("Edit", true);
		mnuBar.add(mnuEdit);
			MenuItem mnuEditClear = new MenuItem("Clear");
			mnuEdit.add(mnuEditClear);
			mnuEdit.insertSeparator(1);
			MenuItem mnuEditCopy = new MenuItem("Copy");
			mnuEdit.add(mnuEditCopy);
			MenuItem mnuEditPaste = new MenuItem("Paste");
			mnuEdit.add(mnuEditPaste);

		// construct and populate the About menu
		Menu mnuAbout = new Menu("About", true);
		mnuBar.add(mnuAbout);
			MenuItem mnuAboutCalculator = new MenuItem("About Calculator");
			mnuAbout.add(mnuAboutCalculator);
			
		// add the ActionListener to each menu item
		mnuFileExit.addActionListener(this);
		mnuEditClear.addActionListener(this);
		mnuEditCopy.addActionListener(this);
		mnuEditPaste.addActionListener(this);
		mnuAboutCalculator.addActionListener(this);
		
		// add the ActionCommand to each menu item
		mnuFileExit.setActionCommand("Exit");
		mnuEditClear.setActionCommand("Clear");
		mnuEditCopy.setActionCommand("Copy");
		mnuEditPaste.setActionCommand("Paste");
		mnuAboutCalculator.setActionCommand("About");
		
		// construct components and initialize beginning values
		lcd = new TextField(20);
			lcd.setEditable(false);
		keypad = new Panel();
		keys = new Button[16];
		first = true;
		op1 = 0.0;
		clearText = true;
		lastOp = 0;
		calcPattern = new DecimalFormat("#######.#######");
		
		// construct and assign captions to the Buttons
		for (int i=0; i<=9; i++)
			keys[i] = new Button(String.valueOf(i));
			
		keys[10] = new Button("/");
		keys[11] = new Button("*");
		keys[12] = new Button("-");
		keys[13] = new Button("+");
		keys[14] = new Button("=");
		keys[15] = new Button(".");
		
		// set Frame and keypad layout to grid layout
		setLayout(new BorderLayout());
		keypad.setLayout(new GridLayout(4,4,10,10));
		
	for (int i=7; i<=10; i++)  // 7, 8, 9, divide
		keypad.add(keys[i]);
		
	for (int i=4; i<=6; i++)  // 4, 5, 6
		keypad.add(keys[i]);
	
	keypad.add(keys[11]);	 // multiply
	
	for (int i=1; i<=3; i++)  // 1, 2, 3
		keypad.add(keys[i]);
		
	keypad.add(keys[12]);	 // subtract
	
	keypad.add(keys[0]);	  // 0 key
	
	for (int i=15; i>=13; i--) 
		keypad.add(keys[i]);  // decimal point, =, add (+) keys
		
	for (int i=0; i<keys.length; i++)
		keys[i].addActionListener(this);
		
	add(lcd, BorderLayout.NORTH);
	add(keypad, BorderLayout.CENTER);
	
	addWindowListener(
		new WindowAdapter()
			{
			public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
		
   	}	// end of constructor method	
   	
   	public void actionPerformed(ActionEvent e)
   	{
   		// test for menu item clicks
   		String arg = e.getActionCommand();
   		if (arg == "Exit")
   			System.exit(0);
   			
   		if (arg == "Clear")
   		{
   			clearText = true;
   			first = true;
   			op1 = 0.0;
   			lcd.setText("");
   			lcd.requestFocus();
   		}
   		
   		if (arg == "Copy")
   		{
   			Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
   			StringSelection contents = new StringSelection(lcd.getText());
   			cb.setContents(contents, null);
   		}
   		
		if (arg == "Paste")
   		{
   			Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
   			Transferable content = cb.getContents(this);
   			try
   			{
   				String s = (String)content.getTransferData(DataFlavor.stringFlavor);
   				lcd.setText(calcPattern.format(Double.parseDouble(s)));
   			}
   			catch (Throwable exc)
   			{
   				lcd.setText("");
   			}
   		}   		
   			
   		if (arg == "About")
   		{
   			String message = "Calculator ver. 1.0\nOpenExhibit Software\nCopyright 2007\nAll rights reserved";
   			JOptionPane.showMessageDialog(null,message, "About Calculator", JOptionPane.INFORMATION_MESSAGE);
   		}	
   			
   		// test for button clicks
   		foundKey = false; 
   			
   		// search for clicked key
   		for (int i=0; i<keys.length && !foundKey; i++)
   		{
   			if(e.getSource() == keys[i])
   			{
   				foundKey = true;
   				switch(i)
   				{
   					// number and decimal point buttons
   					case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 15:
   						if(clearText)
   						{
   							lcd.setText("");
   							clearText = false;
   						}
   						lcd.setText(lcd.getText() + keys[i].getLabel());
   						break;
   						
   						// operator buttons
   						case 10: case 11: case 12: case 13: case 14:
   							clearText = true;
   							
   							if (first) //first operand
   							{
   								if(lcd.getText().length()==0) op1 = 0.0;
   								else op1 = Double.parseDouble(lcd.getText());
   								
   								first = false;
   								clearText = true;
   								lastOp = i; // save last operator
   							}
   							else // second operand
   							{
   								switch(lastOp)
   								{
   									case 10: //divide button
   										op1 /=Double.parseDouble(lcd.getText());
   										break;
   									case 11: //multiply button
   										op1 *=Double.parseDouble(lcd.getText());
   										break;
   									case 12: //minus button
   										op1 -=Double.parseDouble(lcd.getText());
   										break;
   									case 13: //plus button
   										op1 +=Double.parseDouble(lcd.getText());
   										break;
   								} //end of switch(lastOp)
   								lcd.setText(calcPattern.format(op1));
   								clearText = true;
   								
   								if (i==14) first = true; //equal button
   								else lastOp = i; //save last operator
   							} //end else
   							break;
   				}  //end of switch(i)
   			}  //end of if
   		}  //end of for
   	}  //end of actionPerformed
   	
   	public static void main(String args[])
   		{
	   		//set frame properties
	   		Calculator f = new Calculator();
	   		f.setTitle("Calculator Application");
	   		f.setBounds(200,200,300,300);
	   		f.setVisible(true);
	   		
	   		//set image properties
	   		Image icon = Toolkit.getDefaultToolkit().getImage("calcImage.gif");
	   		f.setIconImage(icon);
	   	} //end of main
 }  //end of class


***added code tags -jjsaw5***

Is This A Good Question/Topic? 0
  • +

Replies To: Java Calculator (Menus & Button Arrays)

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2731
  • View blog
  • Posts: 15,808
  • Joined: 24-September 08

Re: Java Calculator (Menus & Button Arrays)

Posted 20 August 2009 - 06:54 AM

:code:

What errors are you having, or are you just posting your code for people to steal?
Was This Post Helpful? 0
  • +
  • -

#3 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Java Calculator (Menus & Button Arrays)

Posted 20 August 2009 - 07:38 AM

ok dude i've found where is your probleme and it is working perfectly with me and plz let me know if it helpful
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Calculator extends Frame implements ActionListener
{
private Button keys[];
private Panel keypad;
private TextField lcd;
private double op1;
private boolean first;
private boolean foundKey;
private boolean clearText;
private int lastOp;
private DecimalFormat calcPattern;
int dot;
String f;
public Calculator() 
{ 
// create an instance of the menu
MenuBar mnuBar = new MenuBar();
setMenuBar(mnuBar);

// create an instance of the File menu
Menu mnuFile = new Menu("File", true);
mnuBar.add(mnuFile);
MenuItem mnuFileExit = new MenuItem("Exit");
mnuFile.add(mnuFileExit);

// construct and populate the Edit menu
Menu mnuEdit = new Menu("Edit", true);
mnuBar.add(mnuEdit);
MenuItem mnuEditClear = new MenuItem("Clear");
mnuEdit.add(mnuEditClear);
mnuEdit.insertSeparator(1);
MenuItem mnuEditCopy = new MenuItem("Copy");
mnuEdit.add(mnuEditCopy);
MenuItem mnuEditPaste = new MenuItem("Paste");
mnuEdit.add(mnuEditPaste);

// construct and populate the About menu
Menu mnuAbout = new Menu("About", true);
mnuBar.add(mnuAbout);
MenuItem mnuAboutCalculator = new MenuItem("About Calculator");
mnuAbout.add(mnuAboutCalculator);

// add the ActionListener to each menu item
mnuFileExit.addActionListener(this);
mnuEditClear.addActionListener(this);
mnuEditCopy.addActionListener(this);
mnuEditPaste.addActionListener(this);
mnuAboutCalculator.addActionListener(this);

// add the ActionCommand to each menu item
mnuFileExit.setActionCommand("Exit");
mnuEditClear.setActionCommand("Clear");
mnuEditCopy.setActionCommand("Copy");
mnuEditPaste.setActionCommand("Paste");
mnuAboutCalculator.setActionCommand("About");

// construct components and initialize beginning values
lcd = new TextField(20);
lcd.setEditable(false);
keypad = new Panel();
keys = new Button[16];
first = true;
op1 = 0.0;
clearText = true;
lastOp = 0;
calcPattern = new DecimalFormat("#######.#######");

// construct and assign captions to the Buttons
for (int i=0; i<=9; i++)
keys[i] = new Button(String.valueOf(i));

keys[10] = new Button("/");
keys[11] = new Button("*");
keys[12] = new Button("-");
keys[13] = new Button("+");
keys[14] = new Button("=");
keys[15] = new Button(".");

// set Frame and keypad layout to grid layout
setLayout(new BorderLayout());
keypad.setLayout(new GridLayout(4,4,10,10));

for (int i=7; i<=10; i++) // 7, 8, 9, divide
keypad.add(keys[i]);

for (int i=4; i<=6; i++) // 4, 5, 6
keypad.add(keys[i]);

keypad.add(keys[11]); // multiply

for (int i=1; i<=3; i++) // 1, 2, 3
keypad.add(keys[i]);

keypad.add(keys[12]); // subtract

keypad.add(keys[0]); // 0 key

for (int i=15; i>=13; i--) 
keypad.add(keys[i]); // decimal point, =, add (+) keys

for (int i=0; i<keys.length; i++)
keys[i].addActionListener(this);

add(lcd, BorderLayout.NORTH);
add(keypad, BorderLayout.CENTER);

addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);

} // end of constructor method 

public void actionPerformed(ActionEvent e)
{
// test for menu item clicks
String arg = e.getActionCommand();
if (arg == "Exit")
System.exit(0);

if (arg == "Clear")
{
clearText = true;
first = true;
op1 = 0.0;
lcd.setText("");
lcd.requestFocus();
dot=0;
}

if (arg == "Copy")
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection contents = new StringSelection(lcd.getText());
cb.setContents(contents, null);
}

if (arg == "Paste")
{
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable content = cb.getContents(this);
try
{
String s = (String)content.getTransferData(DataFlavor.stringFlavor);

lcd.setText(calcPattern.format(Double.parseDouble(s)));
}
catch (Throwable exc)
{
lcd.setText("");
}
} 

if (arg == "About")
{
String message = "Calculator ver. 1.0\nOpenExhibit Software\nCopyright 2007\nAll rights reserved";
JOptionPane.showMessageDialog(null,message, "About Calculator", JOptionPane.INFORMATION_MESSAGE);
} 

// test for button clicks
foundKey = false; 

// search for clicked key
for (int i=0; i<keys.length && !foundKey; i++)
{
if(e.getSource() == keys[i])
{
foundKey = true;
switch(i)
{
// number and decimal point buttons
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 15:
if(clearText)
{
lcd.setText("");
clearText = false;
}

System.out.println(keys[i].getLabel());
if(dot==1&&keys[i].getLabel()!="."	)
{
	
lcd.setText(lcd.getText() + keys[i].getLabel());
}else if(dot==0)
	lcd.setText(lcd.getText() + keys[i].getLabel());
if(keys[i].getLabel()==".")
	dot=1;
System.out.println(f+"  "+dot);
break;

// operator buttons
case 10: case 11: case 12: case 13: case 14:
clearText = true;

if (first) //first operand
{dot=0;
if(lcd.getText().length()==0) op1 = 0.0;
else op1 = Double.parseDouble(lcd.getText());

first = false;
clearText = true;
lastOp = i; // save last operator
}
else // second operand
{dot=0;
switch(lastOp)
{

case 10: //divide button
op1 /=Double.parseDouble(lcd.getText());
break;
case 11: //multiply button
op1 *=Double.parseDouble(lcd.getText());
break;
case 12: //minus button
op1 -=Double.parseDouble(lcd.getText());
break;
case 13: //plus button
op1 +=Double.parseDouble(lcd.getText());
break;
} //end of switch(lastOp)

lcd.setText(calcPattern.format(op1));
clearText = true;

if (i==14) first = true; //equal button
else lastOp = i; //save last operator
} //end else
break;
} //end of switch(i)
} //end of if
} //end of for
} //end of actionPerformed

public static void main(String args[])
{
//set frame properties
Calculator f = new Calculator();
f.setTitle("Calculator Application");
f.setBounds(200,200,300,300);
f.setVisible(true);

//set image properties
Image icon = Toolkit.getDefaultToolkit().getImage("calcImage.gif");
f.setIconImage(icon);
} //end of main
} //end of class 


plz let me know if it helpful!
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Java Calculator (Menus & Button Arrays)

Posted 20 August 2009 - 07:52 PM

View PostoQMr FoxQo, on 20 Aug, 2009 - 06:38 AM, said:

ok dude i've found where is your probleme and it is working perfectly with me and plz let me know if it helpful

For the benefit of the readers of this post can we know what the problem was ?
Was This Post Helpful? 0
  • +
  • -

#5 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Java Calculator (Menus & Button Arrays)

Posted 20 August 2009 - 08:22 PM

:code: and please, at least post the problem
Was This Post Helpful? 0
  • +
  • -

#6 ReinaDark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-August 09

Re: Java Calculator (Menus & Button Arrays)

Posted 26 August 2009 - 06:36 AM

The decimal point button has a glitch. More than one decimal point can be entered! i am trying to figure out how to get only one decimal point when you click the button!

This post has been edited by ReinaDark: 26 August 2009 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#7 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2731
  • View blog
  • Posts: 15,808
  • Joined: 24-September 08

Re: Java Calculator (Menus & Button Arrays)

Posted 26 August 2009 - 06:44 AM

Disable the button after click maybe?
Was This Post Helpful? 0
  • +
  • -

#8 ReinaDark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-August 09

Re: Java Calculator (Menus & Button Arrays)

Posted 27 August 2009 - 07:28 AM

View PostNeoTifa, on 26 Aug, 2009 - 05:44 AM, said:

Disable the button after click maybe?


not sure how to diable after click! i have been racking my brain over what should be a simple fix!
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2731
  • View blog
  • Posts: 15,808
  • Joined: 24-September 08

Re: Java Calculator (Menus & Button Arrays)

Posted 27 August 2009 - 09:30 AM

String input = JTextField.getText();
if(input.contains('.')==true) { //I dunno if this is correct, but you can try, if not just do a for loop checking for a period
decimalButton.disable();
}
else {
decimalButton.enable();
}



Something like that. Now I didn't read your code lol so this might not match up to what you have.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1