Add a text field, a password field, and a text area. - Java Swing

Java examples for Swing:JTextField

Description

Add a text field, a password field, and a text area.

Demo Code

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main extends javax.swing.JFrame {
  JTextField username = new JTextField(15);
  JPasswordField password = new JPasswordField(15);
  JTextArea comments = new JTextArea(4, 15);
  JButton ok = new JButton("OK");
  JButton cancel = new JButton("Cancel");

  public Main() {
    super("Account Information");
    setSize(300, 220);//from   w  w  w  .  j  a  va 2s  . c  o m
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel pane = new JPanel();
    JLabel usernameLabel = new JLabel("Username: ");
    JLabel passwordLabel = new JLabel("Password: ");
    JLabel commentsLabel = new JLabel("Comments: ");
    comments.setLineWrap(true);
    comments.setWrapStyleWord(true);
    pane.add(usernameLabel);
    pane.add(username);
    pane.add(passwordLabel);
    pane.add(password);
    pane.add(commentsLabel);
    pane.add(comments);
    pane.add(ok);
    pane.add(cancel);
    add(pane);
    setVisible(true);
  }

  public static void main(String[] arguments) {

    Main auth = new Main();
  }
}

Related Tutorials