Creating a GUI that accepts user input help

Reply

Join Date: Sep 2004
Posts: 10
Reputation: Jason Marceau is an unknown quantity at this point 
Solved Threads: 0
Jason Marceau Jason Marceau is offline Offline
Newbie Poster

Creating a GUI that accepts user input help

 
0
  #1
Feb 21st, 2005
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);


}}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Creating a GUI that accepts user input help

 
0
  #2
Feb 21st, 2005
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
   public TaxCalc()
   {
      // some code goes here
    }

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

        //set the variable to the text of another text field
        textFieldSalesAmount.setText(i + "");
   }
  public static void main(String[] args)
  {
    TaxCalc tc = new TaxCalc();
   }
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 780
Reputation: OurNation is an unknown quantity at this point 
Solved Threads: 9
OurNation's Avatar
OurNation OurNation is offline Offline
Master Poster

Re: Creating a GUI that accepts user input help

 
0
  #3
Feb 21st, 2005
I am currently reading a book on java and this is how I do a GUI frame

  1. /*
  2.  * GUIFrame
  3.  * An extension of Frame that uses a WindowAdapter to
  4.  * handle the WindowEvents and is centered.
  5.  */
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9.  
  10. public class GUIFrame extends Frame {
  11.  
  12. public GUIFrame(String title) {
  13. super(title);
  14. setBackground(SystemColor.control);
  15.  
  16. addWindowListener(new WindowAdapter() {
  17. //only need to override the method needed
  18. public void windowClosing(WindowEvent e) {
  19. dispose();
  20. System.exit(0);
  21. }
  22. });
  23. }
  24.  
  25. /* Centers the Frame when setVisible(true) is called */
  26. public void setVisible(boolean visible) {
  27. if (visible) {
  28. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  29. setLocation((d.width - getWidth())/2,
  30. (d.height - getHeight())/2);
  31. }
  32. super.setVisible(visible);
  33. }

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

  1. NewGraph ng = new NewGraph( );
  2. GUIFrame frame = new GUIFrame("Graph");
  3. frame.add(g);
  4. frame.pack( );
  5. frame.setVisible(true);
  6. }

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:

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

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. :-|
PETA People for the Eating of Tasty Animals.


FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 10
Reputation: ecyarter is an unknown quantity at this point 
Solved Threads: 0
ecyarter ecyarter is offline Offline
Newbie Poster

Re: Creating a GUI that accepts user input help

 
0
  #4
Feb 21st, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC