Introduction of Java Text Field
TextField in java used for input purpose.We can use following method for this purpose.
Constructors TextField() TextField(String matter); TextField(int width); TextField(String matter, int width);
Text method void setText(String matter); String getText();
Foreground color method void setForeground(Color); Color getForeground();
Background color method void setBackground(Color); Color getBackground();
Location method void setLocation(int x, int y); void setLocation(Point); Point getLocation();
Labe Size method void setSize(int width, int height); void setSize(Dimension); Dimension getSize();
Visibility method void setVisible(boolean) boolean isVisible()
Enable and disable method void setEnabled(boolean) boolean isEnabled()
Programm
Save MyFrameListener.java
import java.awt.event.*;
class MyFrameListener extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
Save MyFrame.java
import java.awt.*;
class MyFrame extends Frame
{
TextField t1, t2, t3, t4;
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(600,400);
addControls();
setVisible(true);
}
private void addControls()
{
setLayout(new FlowLayout());
t1 = new TextField();
t2 = new TextField("Sample Text Field");
t3 = new TextField(15);
t4 = new TextField("Another Text Field",10);
t3.setBackground(Color.YELLOW);
t3.setForeground(Color.RED);
t2.setVisible(true);
t3.setEnabled(true);
t4.setEditable(false);
add(t1);
add(t2);
add(t3);
add(t4);
}
}
Save GUI1.java
public class GUI1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
No comments:
Post a Comment