8 Replies - 734 Views - Last Post: 12 January 2012 - 09:52 AM Rate Topic: -----

#1 WinkyCode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 155
  • Joined: 23-February 11

What is it that I am missing?

Posted 07 January 2012 - 06:09 AM

Dear follow members of DIC,
:genius:

Some background:
I am trying to learn Java because it is fun. I have grasp the basic and are now trying to make a simple UI.

The problem:
I have created a simple adding program that adds value 1 and value 2 into a sum when you push the button "add". Though, it do not do that. Upon pressing "Add" nothing happens.

I have checked the code agains the book and looking through the code ten times. No errors. The programs runs fine but it do not work.

The code:
//This is a test program for a UI
//using JFrame and a simple adding code
//taking the value of first number and sum it up with
//the second value
//Example: Number 1: 4 Number 2: 4 The sum: 8
//What is it that I am missing in the code?
//Code from the book "Java Programming 24-hour trainer"
//ISBN 978-0-470-88964-0
import javax.swing.*;
import java.awt.FlowLayout;

public class SimpleCalculator {
  public static void main(String[] args){
    //Create a panel
       JPanel windowContent = new JPanel();
       
    //Set a layout manage for this panel
      FlowLayout fl = new FlowLayout();
      windowContent.setLayout(fl);
      
      //Create controls in memory
      JLabel label1 = new JLabel("Number 1: ");
      JTextField field1 = new JTextField(10);
      JLabel label2 = new JLabel("Number 2: ");
      JTextField field2 = new JTextField(10);
      JLabel label3 = new JLabel("Sum ");
      JTextField result = new JTextField(10);
      JButton go = new JButton("Add");
      
      //Add controls to the panel
        windowContent.add(label1);
        windowContent.add(field1);
        windowContent.add(label2);
        windowContent.add(field2);
        windowContent.add(label3);
        windowContent.add(result);
        windowContent.add(go);
        
        //Create the frame and add the panel to it
        JFrame frame = new JFrame("My first calculator");
        
        //Add the panel to the top-level container
        frame.setContentPane(windowContent);
        
        //set the size and make the window visible
        frame.setSize(444,444);
        frame.setVisible(true);
        }
        }



The computer:
I have an IBM ThinkPad T43 using an Intel Centrino 740 processor running at 1.8 GHz. RAM is 1.5 GB.

The operating system is Windows 7 Professional 32-bit.

The Java is, according to CMD:

java version "1.6.0_30"

Java™ SE Runtime Environment (build 1.6.0_30-b12)

Java HotSpot™ Client VM (build 20.5-b03, mixed mode, sharing)

javac 1.6.0_30

The question:
What is it that I am missing in the code? What have I done wrong?

Thank you for looking at my thread. :)

Is This A Good Question/Topic? 0
  • +

Replies To: What is it that I am missing?

#2 Cocker081288  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 108
  • Joined: 11-April 09

Re: What is it that I am missing?

Posted 07 January 2012 - 06:17 AM

Your issue is that you have only created the button and it doesn't do anything once clicked.

You need to create a listener that will wait for the button to be that will then add the two numbers together and tell it to display it in a object (such as a jtextfield).

The hyper link should be of good help http://docs.oracle.c...onlistener.html

Let me know if you have any issues.
Was This Post Helpful? 2
  • +
  • -

#3 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: What is it that I am missing?

Posted 07 January 2012 - 06:18 AM

You will want to add an actionListener to your button. To keep it simple, just implement the interface 'ActionListener' to your class.

public class SimpleCalculator implements ActionListener {

JButton button = new JButton("Add");
button.addActionListener(this)

Then you will need to add method

public void actionPerformed(ActionEvent e)
{
perform the action here, which is adding the two numbers and placing them in the 3rd textfield.
}
Was This Post Helpful? 2
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: What is it that I am missing?

Posted 07 January 2012 - 06:23 AM

Your button isnot listened by anything and you didnt tell it to do anything, what you need is to implement ActionListener interface and implement ActionPerformed() abstract method where you will put code to be executed when a button is clicked...
*Ninjaed, a good examples given above ;)

This post has been edited by smohd: 07 January 2012 - 06:27 AM

Was This Post Helpful? 3
  • +
  • -

#5 WinkyCode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 155
  • Joined: 23-February 11

Re: What is it that I am missing?

Posted 07 January 2012 - 06:59 AM

Ok, I think I am getting it. Am I on the right track with this code? And yes, now I am getting errors. :)

//This is a test program for a UI
//using JFrame and a simple adding code
//taking the value of first number and sum it up with
//the second value
//Example: Number 1: 4 Number 2: 4 The sum: 8
//What is it that I am missing in the code?
//Code from the book "Java Programming 24-hour trainer"
//ISBN 978-0-470-88964-0
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.*;


public class SimpleCalculator 
implements WindowListener,ActionListener {

  public static void main(String[] args){
    //Create a panel
       JPanel windowContent = new JPanel();
       
    //Set a layout manage for this panel
      FlowLayout fl = new FlowLayout();
      windowContent.setLayout(fl);
      
      //Create controls in memory
      JLabel label1 = new JLabel("Number 1: ");
      JTextField field1 = new JTextField(10);
      JLabel label2 = new JLabel("Number 2: ");
      JTextField field2 = new JTextField(10);
      JLabel label3 = new JLabel("Sum ");
      JTextField result = new JTextField(10);
      JButton go = new JButton("Add");
      
      //Make the button "Add" active
      JButton Add;
      addWindowListener(this);
      private int numClicks = 0;
      Add.addActionListener(this);
      public void actionPerformed(ActionEvent e) {
      label1 = value1;
      label2 = value2;
      result = value1 + value2;
      }

      //Add controls to the panel
        windowContent.add(label1);
        windowContent.add(field1);
        windowContent.add(label2);
        windowContent.add(field2);
        windowContent.add(label3);
        windowContent.add(result);
        windowContent.add(go);
        
        //Create the frame and add the panel to it
        JFrame frame = new JFrame("My first calculator");
        
        //Add the panel to the top-level container
        frame.setContentPane(windowContent);
        
        //set the size and make the window visible
        frame.setSize(444,444);
        frame.setVisible(true);
        }
        }
        


Was This Post Helpful? 0
  • +
  • -

#6 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: What is it that I am missing?

Posted 07 January 2012 - 07:19 AM

A few things wrong there =)

Value1, Value2, Result are not declared anywhere.

label1, label2 does not change text. You will need to use label1.setText(String text)

You place the actionPerformed method inside another method, it should be placed in it's own area.

numClicks cannot be declared private as it is inside a method.

You try to add an actionListener from a static method, which is not valid. Non-static members can access static members, but not the other way around.

To fix the last point, You have two choices.

First one is your class should look similar to this:

public class SimpleCalculator 
extends JFrame implements WindowListener,ActionListener {

public SimpleCalculator
{
   // add components
   
   Add.addActionListener(this);

}

public static void main(String[] args){
   new SimpleCalculator();
}

}



That's not entirely correcy, being a swing GUI, but no need to get into that.

Second option is to create a static class (this is less of an OOP approach and you should use the first way)

	static ActionListener al = new ActionListener()
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			// TODO Auto-generated method stub
			
		}
		
	};



then use Add.addActionListener(al);
Was This Post Helpful? 2
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: What is it that I am missing?

Posted 07 January 2012 - 07:30 AM

You are new to programming and you are jumping into GUI apps. Whats the point?
A lot of them require knowledge of inheritance since you might be extending JFrame (you dont have to), knowledge of anonymous classes (again, you might not be using it) but overall, its advanced stuff. Stick to console apps for a while. Learn classes, inheritance and polyorphism as well as encapsulation. Then build some silly programs and once you get tired of it, start GUIs.
Was This Post Helpful? 3
  • +
  • -

#8 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: What is it that I am missing?

Posted 07 January 2012 - 07:37 AM

I never thought about that darek, but that is a good suggestion. Sometimes I think I should revisit console applications again incase I have missed some useful programming concepts, I had quickly gone on to GUI applications myself.

+1 for 100 :)

This post has been edited by Mylo: 07 January 2012 - 07:38 AM

Was This Post Helpful? 1
  • +
  • -

#9 WinkyCode  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 155
  • Joined: 23-February 11

Re: What is it that I am missing?

Posted 12 January 2012 - 09:52 AM

Hello everyone!

Just an update. I am reading through everything and testing the code. I will report back an code that will work. Just need to read and re-write code. :cool2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1