Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding functionality to the Calculator 2

Status
Not open for further replies.

yigit

Technical User
Jun 28, 2006
14
0
0
TR
Please help how i can add functionality to this...How can i make the buttons respond to mouse clicks. thanks for your time.i used gridbaglayout by the way.


package ui;
import func.*;
import java.awt.*;
import javax.swing.*;

public class CalcMainFrame
extends JFrame {

final static boolean SHOULD_FILL = true;
final static boolean SHOULD_WEIGHT_X = true;
final static boolean RIGHT_TO_LEFT = false;

public CalcMainFrame() {
super("MOUSE CLICK PROB");
}

public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}

JButton button;
JTextField textField1;
textField1 = new JTextField("", 5);

pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

if (SHOULD_FILL) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}

if (SHOULD_WEIGHT_X) {
c.weightx = 1.0;
c.ipady = 10;
c.ipadx = 10;
}
c.gridx = 0;
c.gridy = 0;
pane.add(textField1);

button = new JButton(" 1 ");
c.gridx = 0;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);
pane.add(button, c);

button = new JButton(" 2 ");
c.gridx = 1;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 3 ");
c.gridx = 2;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" + ");
c.gridx = 3;
c.gridy = 1;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 4 ");
c.gridx = 0;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 5 ");
c.gridx = 1;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 6 ");
c.gridx = 2;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" - ");
c.gridx = 3;
c.gridy = 2;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 7 ");
c.gridx = 0;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 8 ");
c.gridx = 1;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 9 ");
c.gridx = 2;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" / ");
c.gridx = 3;
c.gridy = 3;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" C ");
c.gridx = 0;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" 0 ");
c.gridx = 1;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" = ");
c.gridx = 2;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);

button = new JButton(" x ");
c.gridx = 3;
c.gridy = 4;
button.setFont(new Font("Arial", Font.BOLD, 15));
button.setForeground(Color.green);
button.setBackground(Color.black);

pane.add(button, c);
}

public static void main(String argv[]) {
JFrame.setDefaultLookAndFeelDecorated(true);

CalcMainFrame frame = new CalcMainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addComponentsToPane(frame.getContentPane());

frame.pack();
frame.setVisible(true);
}
}
 
Hi

Code:
[gray](...)[/gray]
[red]import java.awt.event.*;[/red]

public class CalcMainFrame
  extends JFrame
  [red]implements ActionListener[/red] {

  public static void addComponentsToPane(Container pane)
  {
    [gray](...)[/gray]

    button = new JButton(" 1 ");
    [gray](...)[/gray]
    pane.add(button, c);

    [red]button.gomb.addActionListener(this);[/red]

    [gray](...)[/gray]
  }

  [gray](...)[/gray]

  [red]public void actionPerformed(ActionEvent event)
  {
    Object source=event.getSource();

    if (source==button) {
      System.out.println("The button was pressed");
    }
  }[/red]
}

Feherke.
 
thank you feherke but i forgot to give you my calculations part.The problem is to make the calculator respond to both mouse and keyboard and how do i connect this and the first part which is main..thanks to everyone in advance

package func;

import java.awt.*;
import java.awt.event.*;

public class Calculations
implements ActionListener, KeyListener {
int MAX_INPUT_LENGTH = 17;
int INPUT_MODE = 0;
int RESULT_MODE = 1;
int ERROR_MODE = 2;
int displayMode;
Label displayLabel;
boolean clearOnNextDigit;
double lastNumber;
char lastOperator;

public Calculations() {
//should i add more here?
displayResult(0);
clearAll();
}

void setDisplayString(String s) {
displayLabel.setText(s);
}

String getDisplayString() {
return displayLabel.getText();
}

void clearAll() {
setDisplayString("0");
lastOperator = 0;
lastNumber = 0;
displayMode = INPUT_MODE;
clearOnNextDigit = true;
}

void clearLastEntry() {
setDisplayString("0");
clearOnNextDigit = true;
displayMode = INPUT_MODE;
}

void displayResult(double result) {
setDisplayString(Double.toString(result));
lastNumber = result;
displayMode = RESULT_MODE;
clearOnNextDigit = true;
}

void displayError(String errorMessage) {
setDisplayString(errorMessage);
lastNumber = 0;
displayMode = ERROR_MODE;
clearOnNextDigit = true;
}

public void actionPerformed(ActionEvent e) {
processButton(e.getActionCommand());
}

void processButton(String command) {
if (command.equals("0")) {
addDigit(0);
}
if (command.equals("1")) {
addDigit(1);
}
if (command.equals("2")) {
addDigit(2);
}
if (command.equals("3")) {
addDigit(3);
}
if (command.equals("4")) {
addDigit(4);
}
if (command.equals("5")) {
addDigit(5);
}
if (command.equals("6")) {
addDigit(6);
}
if (command.equals("7")) {
addDigit(7);
}
if (command.equals("8")) {
addDigit(8);
}
if (command.equals("9")) {
addDigit(9);
}
if (command.equals("*")) {
processOperator('*');
}
if (command.equals("-")) {
processOperator('-');
}
if (command.equals("/")) {
processOperator('/');
}
if (command.equals("+")) {
processOperator('+');
}
if (command.equals("=")) {
processEquals();
}
}

double getNumberInDisplay() {
String input = displayLabel.getText();
return Double.parseDouble(input);
}

double processLastOperator() throws DivideByZeroException {
double result = 0;
double numberInDisplay = getNumberInDisplay();
switch (lastOperator) {
case '*':
result = lastNumber * numberInDisplay;
break;
case '+':
result = lastNumber + numberInDisplay;
break;
case '-':
result = lastNumber - numberInDisplay;
break;
case '/':
if (numberInDisplay == 0) {
throw (new DivideByZeroException());
}
result = lastNumber / numberInDisplay;
break;
}
return result;
}

void processOperator(char op) {
if (displayMode != ERROR_MODE) {
double numberInDisplay = getNumberInDisplay();
if (lastOperator != 0) {
try {
double result = processLastOperator();
displayResult(result);
lastNumber = result;
}
catch (DivideByZeroException e) {
displayError("Division by zero!");
}
}
else {
lastNumber = numberInDisplay;
}
clearOnNextDigit = true;
lastOperator = op;
}
}

void processEquals() {
if (displayMode != ERROR_MODE) {
try {
double result = processLastOperator();
displayResult(result);
}
catch (DivideByZeroException e) {
displayError("Division by zero!");
}
lastOperator = 0;
}
}

void processSignChange() {
if (displayMode == INPUT_MODE) {
String input = getDisplayString();
if (input.length() > 0) {
if (input.indexOf("-") == 0) {
setDisplayString(input.substring(1));
}
else {
setDisplayString("-" + input);
}
}
}
else if (displayMode == RESULT_MODE) {
double numberInDisplay = getNumberInDisplay();
if (numberInDisplay != 0) {
displayResult( -numberInDisplay);
}
}

}

void addDigit(int digit) {
/// QUESTION: what are the next two lines for?
if (clearOnNextDigit) {
setDisplayString("");
}
String inputString = getDisplayString();
if ( (!inputString.equals("0") || digit > 0) &&
inputString.length() < MAX_INPUT_LENGTH) {
setDisplayString(inputString + digit);
}
displayMode = INPUT_MODE;
clearOnNextDigit = false;
}

public void keyPressed(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
String command;
char keyChar = e.getKeyChar();
if (keyChar == KeyEvent.VK_ENTER) {
command = new String("=");
}
else if (keyChar == KeyEvent.VK_ESCAPE) {
command = new String("C");
}
else {
byte bytes[] = {
(byte) keyChar};
command = new String(bytes);
}
processButton(command);
}
}

class DivideByZeroException
extends Exception {
DivideByZeroException() {
super("Divide by zero");
}
}
 
Hi

Sorry yigit, I have not enough time to overview your code. I just copy&pasted a few lines from one of my older codes. Even that with a typo. Adding the ActionListener should look like this :
Code:
button.addActionListener(this);

Feherke.
 
i wondered what 'gomb' was:), anyway is the problem so hard. Its hard for me since im very new to this subject but the code should be reasonably easy to understand..I really need to know how i am to solve this problem between the two parts.
 
yigit - You cannot come on here, posting loads of unformatted code saying "fix it for me" - and then complain that it should be easy. Do your own research or take what people have given you and make an effort yourself.

Your choice.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
thank you sedj for your valuable critism. You are correct, i was wrong to put all the code and demand an answer but please try to think of it from my way. I have no java background, i am thrown in the middle of the java in to the GUI stuff without having any prior java knowledge and i have written this code with tremendous effort and i am stuck in most probably a small part but i can't see it. Thank you for your answer, and time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top