Swing组件与密码框

Swing组件

  • 只显示文字的标签对象:JLabel

  • 标签显示图像:ImageIcon

  • 标签上同时显示文本和图像:Jlabel (String text、Icon icon、int horizontalAlignment)

    • 该构建器中,第一个参数是欲显示的文本,第二个参数是欲显示的图像,第三个参数为水平方向上的对齐方式,取值为:SwingConstants.LEFT、SwingConstants.RIGHT或SwingConstants. CENTER。

  • 文本框: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(200, 130);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public static void main(String[] args)
{
Elab fr=new Elab("Jlabel&JTfextField应用");
fr.setVisible(true);

}

}
  • 密码框:JPasswordField(JPasswordField继承自JTextField 类,因此可以使用JTextField 的方法。)

    注意:取得文本框中的文本时,使用方法getText(),该方法返回的是一个String类型的对象;而要取得密码框中的文本,使用方法getPassword(),该方法返回的是一个char数组

    设置到文本标签里面: setText(String.valueOf();

package src1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Prectice3 extends JFrame
{
JPanel jPanel;
JButton but1,but2;
JLabel lab1,lab2,lab3;
JTextField text1,text2;
JPasswordField pass2;
public Prectice3(String prectice)
{
super(prectice);
init();
}
public void init()
{
  jPanel=new JPanel(null);//无布局
 //p.setLayout(null);//无布局
  but1=new JButton("登录");
  but2=new JButton("关闭");
   lab1=new JLabel("账号");
  lab2=new JLabel("密码");
  lab3=new JLabel("密码");
  text1=new JTextField(15);
  pass2=new JPasswordField(15);
  text2=new JTextField(15);
 
  jPanel.add(lab1);
  lab1.setBounds(10, 10, 65, 15);
  jPanel.add(lab2);
  lab2.setBounds(10, 35, 65, 15);
  jPanel.add(text1);
text1.setBounds(75, 8, 170, 20);
  jPanel.add(pass2);
  pass2.setBounds(75, 33, 170, 20);
jPanel.add(but1);
but1.setBounds(60, 70, 60, 40);
  jPanel.add(but2);
  but2.setBounds(150, 70, 60, 40);
  jPanel.add(lab3);
  lab3.setBounds(90, 120, 130, 50);
  this.setContentPane(jPanel);
  this.setSize(300,250);
  this.setVisible(true);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.setResizable(true);
text1.addActionListener(new action());
but1.addActionListener(new action());
but2.addActionListener(new action());
pass2.addActionListener(new action() );
}
   public static void main(String[] args)
  {
Prectice3 prectice3=new Prectice3("无布局");
}
   class action implements ActionListener
  {


public void actionPerformed(ActionEvent arg0)
{
         if (arg0.getSource()==text1)
              {
  pass2.requestFocus();
  pass2.selectAll();
 
              }
             else if(arg0.getSource()==but1||arg0.getSource()==pass2)
            {
           pass2.requestFocus();
         
           String uses=text1.getText();
           String cude=new String(pass2.getPassword());
           lab3.setText(String.valueOf("账号:"+uses+"密码"+cude));//设置到文本标签里面
          // System.out.println("账号:"+uses+"密码:"+cude);
           
                  }
         else if (arg0.getSource()==but2)
        {
 System.exit(0);
}
       

}
 
  }
}
 
posted @ 2022-05-10 22:05  zjw_rp  阅读(159)  评论(0编辑  收藏  举报