Hi,

I am learning Java and I have decided that I should create my first project. I have the application code for a Tax calculator and I would like to add a GUI to it but I am struggling to work out how to enable the text field box to accept user input in the form of an integer variable. I think I may need an ActionEventListener class but don't really know how to write the code for that yet. In other words, the GUI will display a text field box where a user can enter their annual income then press enter. Then the program will calculate the tax payable and return a result.

Here is the code I have so far for the GUI, the application code is seperate:

import java.awt.*;
import java.applet.Applet.*;
public class TestLayout1 extends Frame{ 
    public static void main(String[] args) { 

        // create the variables to hold input
        int Income;

        TestLayout1 t = new TestLayout1();
        }  public TestLayout1() {
            // create a frame(a box to put things in)
            setTitle("Taxation Calculator");
            setBounds(200,200,500,500); 
            setLayout(new GridBagLayout());
            setVisible(true);
            setSize(400,400);

            //set layout for panel p1
            setLayout (new FlowLayout ());
            Label l1 = new Label ("Enter employee's income: ");
            TextField t1 = new TextField (12);


            // couldn't vertically align text fields, used spaces instead
            Label l2 = new Label ("Enter employee's age:        ");
            TextField t2 = new TextField (12);
            add(l1);
            add(t1);
            add(l2);
            add(t2);
            show();  

            t1.setText("");

            String str1 = t1.getText();

            System.out.print(str1);


        }}

First of all, I would recommend putting some of that code in the constructor method, rather than the main methd:

public class TaxCalc extends JFrame implements ActionListener
{
   //instance variables up here
   // like the textfields so you can get the input
  [B] public TaxCalc()
   {
      // some code goes here
    }[/B]

  public void actionPerformed(ActionEvent ae)
  {
       //this is how you get the input in variable form
        [B]Integer i = Integer.parseInt(textField.getText());[/B]

        //set the variable to the text of another text field
        textFieldSalesAmount.setText(i + "");
   }
  public static void main(String[] args)
  {
    TaxCalc tc = new TaxCalc();
   }
}

I am currently reading a book on java and this is how I do a GUI frame

/*
 * GUIFrame
 * An extension of Frame that uses a WindowAdapter to
 * handle the WindowEvents and is centered.
 */

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

public class GUIFrame extends Frame {

  public GUIFrame(String title) {
    super(title);
    setBackground(SystemColor.control);

    addWindowListener(new WindowAdapter() {
      //only need to override the method needed
      public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
      }
    });
  }

  /* Centers the Frame when setVisible(true) is called */
  public void setVisible(boolean visible) {
    if (visible) {
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      setLocation((d.width - getWidth())/2,
                  (d.height - getHeight())/2);
    }
    super.setVisible(visible);
  }

That is what is implemented in a progrmae for my GUI frames and in any code now I can put

NewGraph ng = new NewGraph( );
          GUIFrame frame = new GUIFrame("Graph");
          frame.add(g);
          frame.pack( );
          frame.setVisible(true);
     }

In this peticular instinace I was making a graph.

I'm not really an expert on any of this but in my book it says to do this:

TextField tf1 = new TextField(25);
tf1.setText("Type Stuff Here");
tf1.setFont(new Font("Timesroman", font.BOLD, 18));
//Then you put the frame in the GUI
frame.add(tf1);
}

In that text feild the Defualt text that appears there is "Type Stuff Here" the font is Timesroman and it is bold and it is set at size 18 also the full text feild is size 25. Hope some of that helped. :-|

To use the Integer.parseInt code you will want to validate your input first. If the user enters letters or null, for instance you will get a NumberFormatException and crash.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.