Java by API/javax.swing/JPasswordField — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:19, 31 мая 2010
Содержание
JPasswordField: getPassword()
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
public class Main extends JFrame {
public static void main(String[] args) {
Main that = new Main();
that.setVisible(true);
}
public Main() {
setSize(450, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new PasswordPanel(), BorderLayout.SOUTH);
}
}
class PasswordPanel extends JPanel {
JPasswordField pwf;
public PasswordPanel() {
pwf = new JPasswordField(10);
pwf.setEchoChar("#");
add(pwf);
pwf.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
System.out.println(new String(pwf.getPassword()));
}
});
}
}
new JPasswordField()
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JPasswordField Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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());
f.add(rowOne, BorderLayout.NORTH);
f.add(rowTwo, BorderLayout.SOUTH);
f.setSize(300, 200);
f.setVisible(true);
}
}
new JPasswordField(int columns)
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Main extends JFrame {
public Main() {
super("JLayeredPane Demo");
setSize(256, 256);
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.setOpaque(false);
JLabel label1 = new JLabel("Username:");
label1.setForeground(Color.white);
content.add(label1);
JTextField field = new JTextField(15);
content.add(field);
JLabel label2 = new JLabel("Password:");
label2.setForeground(Color.white);
content.add(label2);
JPasswordField fieldPass = new JPasswordField(15);
content.add(fieldPass);
setLayout(new FlowLayout());
add(content);
((JPanel) getContentPane()).setOpaque(false);
ImageIcon earth = new ImageIcon("largejexpLogo.png");
JLabel backlabel = new JLabel(earth);
getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
backlabel.setBounds(0, 0, earth.getIconWidth(), earth.getIconHeight());
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
new JPasswordField(String text, int columns)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
public class Main extends JFrame {
JPasswordField field = new JPasswordField("*", 10);
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Field=" + field.getText());
}
});
getContentPane().add(field);
pack();
setVisible(true);
}
public static void main(String arg[]) {
new Main();
}
}