| | |
Creating a GUI that accepts user input help
![]() |
•
•
Join Date: Sep 2004
Posts: 10
Reputation:
Solved Threads: 0
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);
}}
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);
}}
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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();
}
} I am currently reading a book on java and this is how I do a GUI frame
That is what is implemented in a progrmae for my GUI frames and in any code now I can put
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:
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. :-|
Java Syntax (Toggle Plain Text)
/* * 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
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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. :-|
PETA People for the Eating of Tasty Animals.
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
![]() |
Similar Threads
- infinite loop with user input (Java)
- Restricting User input to Numerical Values in jtextbox (Java)
- Calculate GPA problems with user input, Help needed (Java)
- User Input without GUI... Need Guidance (Java)
Other Threads in the Java Forum
- Previous Thread: How to draw a line
- Next Thread: two compiler errors to do with arrays
| Thread Tools | Search this Thread |
addball android api applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game gis givemetehcodez graphics gui guidancer html ide image inetaddress integration intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop map method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program project radio scanner server service set sharepoint smart sms smsspam software sortedmaps sql subclass support swing textfield threads tree trolltech unlimited utility windows






