Swing组件与密码框
-
只显示文字的标签对象:JLabel
-
标签显示图像:ImageIcon
-
标签上同时显示文本和图像:Jlabel (String text、Icon icon、int horizontalAlignment)
-
-
-
文本框:JTextField (获取文本框中的文本:String str=txtf. getText();)(选项中所有文本 selectAll();)(requestFocus();为控件获取焦点,即置光标)
package src;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Elab extends JFrame
{
ImageIcon icon=null;
JLabel labuser=null;
JLabel labpwd=null;
JTextField txtfuser=null;
JPasswordField pwdf=null;
JButton btn1=null;
JButton btn2=null;
JPanel p=null;
public Elab(String title)
{
super(title);
init();
}
public void init()
{
icon=new ImageIcon("image\\user.gif");
labuser=new JLabel("用户:",icon,SwingConstants.CENTER);
labpwd=new JLabel("密码:",new ImageIcon("image\\pwd.gif"),SwingConstants.CENTER);
txtfuser=new JTextField(9);
pwdf=new JPasswordField(9);
btn1=new JButton("登陆");
btn2=new JButton("取消");
p=new JPanel();
p.add(labuser);
p.add(txtfuser);
p.add(labpwd);
p.add(pwdf);
btn1.setIcon(new ImageIcon("image\\ok.gif"));
btn2.setIcon(new ImageIcon("image\\cancel.gif"));
p.add(btn1);
p.add(btn2);
this.getContentPane().add(p);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(