2 Replies - 598 Views - Last Post: 01 August 2013 - 06:33 PM Rate Topic: -----

#1 SpryteNM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-August 13

Reset button in Java AWT GUI for a project due tomorrow

Posted 01 August 2013 - 05:19 PM

I'm needing to build a simple GUI for an intro class that meets some basic requirements. I'm having some issues with only one part of my code, and I am not sure what to do to sort it out. For debugging purposes, I have stripped down my code to only the part I need help with. Our class is very basic, so I would like to make this as simple as possible. I've been trying to figure it out for a week with no avail.

Basically, I have a text field where a user inputs a number in inches. Pressing the Convert button converts that value to yards, in the form of a decimal. The user will take that decimal value and put it elsewhere in the program for additional calculations (not included here, because the rest is huge!). Because they will do this up to 10 times, I need to Reset the text field and the value it spits out, so that they can press "Convert" more than once.
I can get the text field to clear via the "Reset" button, but if another value is entered in the text field, the value "converted" doesn't update. I don't know if it's doing the calculation and not reinserting, because I don't know how. We didn't learn much about GUIs and my project is due tomorrow. Here's the code, which does compile on its own:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class quiltCalc extends Frame implements ActionListener {


//create textfields

TextField inches;


//Launch GUI from application
public static void main(String[] a)
 {
    quiltCalc fl = new quiltCalc();
 } //end of Main

// Frame definition
public quiltCalc()
 {
    //Set up the frame
    setBackground(new Color(239,228,176));
    setLayout(null);
    setBounds(20,20,900,700);
    setTitle("Berta's Quilt Calculator for ICT352");
    setTitle("quiltCalc");
    setVisible(true);

    //Add button q for Quit
    Button q = new Button("Quit");
    q.setBounds(getWidth()-100,getHeight()-50,90,30);
    q.addActionListener(this);
    add(q);


    //Display instructions for inch conversion to yards
    Label inchesInstruct = new Label
      ("Enter the amount of fabric you need, in inches, to convert to yards in decimal form:");
    inchesInstruct.setBounds(20,120,520,25);
    inchesInstruct.setFont(new Font("Helvetica",Font.PLAIN, 14));
    inchesInstruct.setForeground(new Color(0,0,0));
    add(inchesInstruct);

    //Adding text field for input of inches, to convert to yards
    inches = new TextField();
    inches.setBounds(550,120,40,25); 
    inches.setFont(new Font("DialogInput",0,18));
    add(inches);	

    //Add button to convert inches to yards
    Button c = new Button("Convert");
     c.setBounds(600,120,70,25);
     c.addActionListener(this);
     add(c);

    //Add button r for Reset of inch conversion
    Button r = new Button("Reset");
     r.setBounds(675,120,70,25);
     r.addActionListener(this);
     add(r);


 } //end of GUI frame definition



//Determine the action of the button
public void actionPerformed(ActionEvent e) 
 { 
	String z = e.getActionCommand();
	if (z.equals("Quit")) 
	{
	  System.exit(0); //Quit system
	}
	if (z.equals("Convert"))
	{
	   //Get inches entered and parse to string
	   String inchesIn = inches.getText();
           double inchesInput = Double.parseDouble(inchesIn);
	   //Perform conversion of inches in to a decimal of yards
	   double decimalOut = (int)(inchesInput / 36 * 1000) / 1000.0;
	   //Convert back to string
	   String decimalYards = Double.toString(decimalOut);
	   //Display entered text
	   Label converted = new Label(decimalYards);
           converted.setBounds(750,120,100,30);
           converted.setFont(new Font("Helvetica",Font.BOLD, 20));
           converted.setForeground(new Color(255,0,0));
	   add(converted);
	}
	if (z.equals("Reset"))
	{
	   
	   inches.setText("");

	   //How can I get the "Convert" button to work again?
	}

  }	
	
 }//End of Action Performed 


//End of quiltCalc Frame class 


Is This A Good Question/Topic? 0
  • +

Replies To: Reset button in Java AWT GUI for a project due tomorrow

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Reset button in Java AWT GUI for a project due tomorrow

Posted 01 August 2013 - 06:24 PM

Easy fix. Can you figure out what went wrong?

public class quiltCalc extends Frame implements ActionListener {


//create textfields

TextField inches;
Label converted;

//Launch GUI from application
public static void main(String[] a)
 {
    quiltCalc fl = new quiltCalc();
 } //end of Main

// Frame definition
public quiltCalc()
 {
    //Set up the frame
    setBackground(new Color(239,228,176));
    setLayout(null);
    setBounds(20,20,900,700);
    setTitle("Berta's Quilt Calculator for ICT352");
    setTitle("quiltCalc");
    setVisible(true);

    //Add button q for Quit
    Button q = new Button("Quit");
    q.setBounds(getWidth()-100,getHeight()-50,90,30);
    q.addActionListener(this);
    add(q);


    //Display instructions for inch conversion to yards
    Label inchesInstruct = new Label
      ("Enter the amount of fabric you need, in inches, to convert to yards in decimal form:");
    inchesInstruct.setBounds(20,120,520,25);
    inchesInstruct.setFont(new Font("Helvetica",Font.PLAIN, 14));
    inchesInstruct.setForeground(new Color(0,0,0));
    add(inchesInstruct);

    //Adding text field for input of inches, to convert to yards
    inches = new TextField();
    inches.setBounds(550,120,40,25); 
    inches.setFont(new Font("DialogInput",0,18));
    add(inches);	

    //add connverted label.
    converted = new Label();
    converted.setBounds(750,120,100,30);
    converted.setFont(new Font("Helvetica",Font.BOLD, 20));
    converted.setForeground(new Color(255,0,0));
    add(converted);

    //Add button to convert inches to yards
    Button c = new Button("Convert");
     c.setBounds(600,120,70,25);
     c.addActionListener(this);
     add(c);

    //Add button r for Reset of inch conversion
    Button r = new Button("Reset");
     r.setBounds(675,120,70,25);
     r.addActionListener(this);
     add(r);


 } //end of GUI frame definition



//Determine the action of the button
public void actionPerformed(ActionEvent e) 
 { 
	String z = e.getActionCommand();
	if (z.equals("Quit")) 
	{
	  System.exit(0); //Quit system
	}
	if (z.equals("Convert"))
	{
	   //Get inches entered and parse to string
	   String inchesIn = inches.getText();
           double inchesInput = Double.parseDouble(inchesIn);
	   //Perform conversion of inches in to a decimal of yards
	   double decimalOut = (int)(inchesInput / 36 * 1000) / 1000.0;
	   //Convert back to string
	   String decimalYards = Double.toString(decimalOut);
	   //Display entered text
	   
	   converted.setText(String.valueOf(decimalYards));
	}
	if (z.equals("Reset"))
	{
	   
	   inches.setText("");

	   //How can I get the "Convert" button to work again?
	}

  }	
	
 }//End of Action Performed 


//End of quiltCalc Frame class 


This post has been edited by farrell2k: 01 August 2013 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 SpryteNM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-August 13

Re: Reset button in Java AWT GUI for a project due tomorrow

Posted 01 August 2013 - 06:33 PM

That makes SO much more sense. And it worked like a charm. Thanks for helping a n00b like me with some easy stuff.

You're a lifesaver.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1