Java tutorial-JButton JTextField and JLabel |
|||||||||||||||||||||||||||
Java GUIJButton JTextField and JLabel-The JButton is a Java Swing control. You can click on it to perform action.-The JTextField is a text box that allows you to enter text in it. -If you want to display text on a frame window, you can use JLabel. Example: import javax.swing.*; class ButtonTextFieldLabel extends JFrame{ JButton bnt1,bnt2,bnt3; JTextField tf; JLabel lbl;
public ButtonTextFieldLabel(){ super("Hello"); //create a text box, a label and 3 buttons objects tf=new JTextField("TextField"); lbl=new JLabel("Label"); bnt1=new JButton("Button1"); bnt2=new JButton("Button2"); bnt3=new JButton("Button3");
setLayout(null);/*setting the layout of displaying controls to null will enable you to specify the locations of your controls to display
*/ //set specific locations of the controls by using setBounds() method tf.setBounds(150,300,60,40);//x=90,y=100,width=60,height=40 lbl.setBounds(150,150,80,40); bnt1.setBounds(50,200,80,40); bnt2.setBounds(150,200,80,40); bnt3.setBounds(250,200,80,40); //add controls on the JFrame add(tf); add(lbl); add(bnt1); add(bnt2); add(bnt3); add(tf); setSize(500,500); setVisible(true);}
}
public class TestBTL{ public static void main(String[] args){ new ButtonTextFieldLabel(); }
}
|
|||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||