Below code creates a JTextField and adds it to a JFrame window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<textarea class="java" cols="10" name="code" rows="10">import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JLabel; public class JTextFieldExample extends JFrame{ public JTextField userField; public JLabel userLabel; JTextFieldExample() { super("JFrame Example"); userField=new JTextField(); userLabel=new JLabel("Enter Username"); userLabel.setBounds(10, 10,100, 20); userField.setBounds(120, 10, 100, 20); add(userLabel); add(userField); setLayout(null); setSize(300,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JTextFieldExample(); } } </textarea> |
See also: