V
Veeral Patel
G'dday,
I am just a beginner in Java and have stumbled across a couple of
problems in writing a GUI applet. I have included the applet below at
the end of this message. My problem is that I have created a input
field which at the moment the USER simply types in a numerical value
and then presses the ENTER key (on keyboard) to produce the result. So
far it works. I decided to play with the JButton feature. Now I have
no idea how to get the JButton feature to work. At the moment I have
commented out the JBUtton lines. Can anyone tell me how to go about
configuring this JButton feature. I dont know how to pass the input
variable entered in the FIELD box to the append function when the USER
presses the ENTER Button on the applet.
My other question is how to fix the GUI components in one place. At
the moment if I enter a larger value say 3333333 the GUI components
shift. Is there a way to permanently fix the GUI components in one
location? Or do I have to use a different GUI component manager ?
Any feedback will be highly appreciated
thanks
Veeral
################################################################################
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
public class JFloor2 extends JApplet implements ActionListener
{
JLabel inputLabel;
JTextField inputField;
JTextArea outputArea;
//JButton PushButton; //comment this line out for no button support
public void init()
{
Container container = getContentPane(); //setting GUI
container.setLayout(new FlowLayout() ); //setting GUI
inputLabel = new JLabel("Enter input value");
container.add(inputLabel);
inputField = new JTextField(5); //creating field
inputField.addActionListener(this); //uncomment this line when
button feature is disabled
container.add(inputField);
outputArea = new JTextArea();
container.add(outputArea);
//PushButton = new JButton("ENTER"); //comment this line out
for no button support
//PushButton.addActionListener(this); //comment this line out
for no button support
//container.add(PushButton); //comment this line out for no
button support
}
public void actionPerformed(ActionEvent actionEvent)
{
double input = Double.parseDouble
(actionEvent.getActionCommand()); //this only works when user
directly presses ENTER key in the FIELD BOX.
outputArea.append("\n Input Value is " + input + " \n Non
Floored Value is " + (input + 0.5) + " \n Floored Value is "
+ (int)(Math.floor(input + 0.5)) + "\n");
}
}
I am just a beginner in Java and have stumbled across a couple of
problems in writing a GUI applet. I have included the applet below at
the end of this message. My problem is that I have created a input
field which at the moment the USER simply types in a numerical value
and then presses the ENTER key (on keyboard) to produce the result. So
far it works. I decided to play with the JButton feature. Now I have
no idea how to get the JButton feature to work. At the moment I have
commented out the JBUtton lines. Can anyone tell me how to go about
configuring this JButton feature. I dont know how to pass the input
variable entered in the FIELD box to the append function when the USER
presses the ENTER Button on the applet.
My other question is how to fix the GUI components in one place. At
the moment if I enter a larger value say 3333333 the GUI components
shift. Is there a way to permanently fix the GUI components in one
location? Or do I have to use a different GUI component manager ?
Any feedback will be highly appreciated
thanks
Veeral
################################################################################
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
public class JFloor2 extends JApplet implements ActionListener
{
JLabel inputLabel;
JTextField inputField;
JTextArea outputArea;
//JButton PushButton; //comment this line out for no button support
public void init()
{
Container container = getContentPane(); //setting GUI
container.setLayout(new FlowLayout() ); //setting GUI
inputLabel = new JLabel("Enter input value");
container.add(inputLabel);
inputField = new JTextField(5); //creating field
inputField.addActionListener(this); //uncomment this line when
button feature is disabled
container.add(inputField);
outputArea = new JTextArea();
container.add(outputArea);
//PushButton = new JButton("ENTER"); //comment this line out
for no button support
//PushButton.addActionListener(this); //comment this line out
for no button support
//container.add(PushButton); //comment this line out for no
button support
}
public void actionPerformed(ActionEvent actionEvent)
{
double input = Double.parseDouble
(actionEvent.getActionCommand()); //this only works when user
directly presses ENTER key in the FIELD BOX.
outputArea.append("\n Input Value is " + input + " \n Non
Floored Value is " + (input + 0.5) + " \n Floored Value is "
+ (int)(Math.floor(input + 0.5)) + "\n");
}
}