Java/Swing JFC/Password Field

Материал из Java эксперт
Перейти к: навигация, поиск

Create Password field

import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.UIManager;
public class PasswordField extends JFrame {
  public PasswordField() {
    super("JPasswordField Test");
    getContentPane().setLayout(new FlowLayout());
    JPasswordField textField1 = new JPasswordField("m", 1);
    JPasswordField textField2 = new JPasswordField("mm", 2);
    getContentPane().add(textField1);
    getContentPane().add(textField2);
    setSize(300, 190);
    setVisible(true);
  }
  public static void main(String argv[]) {
    new PasswordField();
  }
}





Password Field Sample

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class PasswordSample {
  public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    content.setLayout(new BorderLayout());
    Box rowOne = Box.createHorizontalBox();
    rowOne.add(new JLabel("Username"));
    rowOne.add(new JTextField());
    Box rowTwo = Box.createHorizontalBox();
    rowTwo.add(new JLabel("Password"));
    rowTwo.add(new JPasswordField());
    content.add(rowOne, BorderLayout.NORTH);
    content.add(rowTwo, BorderLayout.SOUTH);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}





PasswordField Sample 2

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class PasswordFieldSample {
  public static void main(String args[]) {
    String title = "Password Example";
    JFrame frame = new JFrame(title);
    Container content = frame.getContentPane();
    JPanel userPanel = new JPanel(new BorderLayout());
    JLabel userLabel = new JLabel("Username: ");
    userLabel.setDisplayedMnemonic(KeyEvent.VK_U);
    JTextField userTextField = new JTextField();
    userLabel.setLabelFor(userTextField);
    userPanel.add(userLabel, BorderLayout.WEST);
    userPanel.add(userTextField, BorderLayout.CENTER);
    JPanel passPanel = new JPanel(new BorderLayout());
    JLabel passLabel = new JLabel("Password: ");
    passLabel.setDisplayedMnemonic(KeyEvent.VK_P);
    JPasswordField passTextField = new JPasswordField();
    passLabel.setLabelFor(passTextField);
    passPanel.add(passLabel, BorderLayout.WEST);
    passPanel.add(passTextField, BorderLayout.CENTER);
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(userPanel, BorderLayout.NORTH);
    panel.add(passPanel, BorderLayout.SOUTH);
    content.add(panel, BorderLayout.NORTH);
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}