4 Replies - 447 Views - Last Post: 06 June 2013 - 07:42 PM Rate Topic: -----

#1 Bubber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-June 13

Java button problem

Posted 05 June 2013 - 06:06 PM

I am very new to java, and programming in general. I started making this calculator but I got stuck on the equals button. the calculator is supposed to work like this: you type in your number, type in your operation, and type in your other number and press the equals button and it gives you your answer. However I do not know how to make the button...well...work.

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

public class tuna extends JFrame{
	
	int num1, num2;
	private JTextField numfield1;
	private JTextField numfield2;
	private JTextField operation;
	private JButton enter;
	
	public tuna(){
		super("Calculator!");
		setLayout(new FlowLayout());
		
		numfield1 = new JTextField(5);
		add(numfield1);
		operation = new JTextField(2);
		add(operation);
		numfield2 = new JTextField(5);
		add(numfield2);
		
		enter = new JButton("=");
		add(enter);
		
		int value;
		numfield1.setText(String.valueOf(num1));
		numfield2.setText(String.valueOf(num2));
		
		
		thehandler handler = new thehandler();
		numfield1.addActionListener(handler);
		operation.addActionListener(handler);
		numfield2.addActionListener(handler);
		
	}
	private class thehandler implements ActionListener{
		
		
		public void actionPerformed(ActionEvent e){
			
			
			enter.addActionListener(this);
			
			if(???.enter == enter) //this is where my problem is
				string = String.format("%d %s %d = %d", num1, operation, num2);
			}


			
		}
		
		
		public void actionPerformed(ActionEvent event){
			
			
			
			
		}
	}
	



Is This A Good Question/Topic? 0
  • +

Replies To: Java button problem

#2 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 133
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Java button problem

Posted 05 June 2013 - 10:50 PM

for a more general help Calculator

more specifically, move the action listener for enter in the constructor and,

if (e.getSource == enter) {
    //code here
}


Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java button problem

Posted 06 June 2013 - 03:48 AM

and no need to have two different handler
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: Java button problem

Posted 06 June 2013 - 06:37 AM

Also there is no need for the other controls to have a listener attached to them. You only really care about what happens when you press the enter button.

And class names should start with a capital letter.

This post has been edited by Ryano121: 06 June 2013 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java button problem

Posted 06 June 2013 - 07:42 PM

and you DO NOT needs a class thehandler which is also badly named as Ryano121 mentionned
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1