JTextField textField = new JTextField(); textField.setText("Enter your name"); textField.setHorizontalAlignment(JTextField.CENTER);
JTextField textField = new JTextField(); textField.setText("Enter your age"); textField.setHorizontalAlignment(JTextField.LEFT);
import javax.swing.*; public class MyTextField extends JFrame { public MyTextField() { JTextField textField = new JTextField(); textField.setText("Enter your password"); textField.setHorizontalAlignment(JTextField.RIGHT); add(textField); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public static void main(String[] args) { new MyTextField(); } }In this example, we create a new JFrame that contains a JTextField. We set the text of the text field to "Enter your password" and set its horizontal alignment to right. We then set the JFrame to exit when it is closed, pack it (so that it sizes itself according to its components), and make it visible.