Swing元件與密碼框
阿新 • • 發佈:2022-05-10
-
只顯示文字的標籤物件: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(