从JTextField和JPasswordField获取输入,然后检查字段是否为空

时间:2016-02-21 14:34:08

标签: java

JTextFieldJPasswordField获取输入,然后检查字段是否为空。如果为空,则应禁用保存按钮。如果不为空,则应启用保存按钮。密码字段和验证密码字段的输入也不相同,应该显示密码不匹配。

import java.util.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
class Example extends JFrame{
private JLabel l1,l2,l3,l4,l5,l6,l7;
private JTextField tf1;
private JPasswordField pf2,pf3;
private JButton b1,b2;
private JRadioButton rb1,rb2;
Example(){
    setSize(500,400);
    setDefaultCloseOperation(3);
    setLocationRelativeTo(null);
    JPanel p1=new JPanel();
    p1.setLayout(null);
    p1.setBackground(Color.gray);
    l1=new JLabel();
    l1.setBounds(10,10,60,60);
    l1.setIcon(new ImageIcon("1456072084_add-user.png"));
    l2=new JLabel("New User");
    l2.setFont(new Font("",0,25));
    l2.setBounds(200,5,130,100);
    l3=new JLabel("User Name");
    l3.setBounds(70,80,100,60);
    l4=new JLabel("Password");
    l4.setBounds(74,130,100,60);
    l5=new JLabel("Verify Password");
    l5.setBounds(40,180,100,60);
    l6=new JLabel();
    l6.setBounds(180,220,150,37);
    l7=new JLabel("Account Type");
    l7.setBounds(58,250,120,37);
    rb1=new JRadioButton("Administrator");
    rb1.setContentAreaFilled(false);
    rb1.setBounds(200,250,120,37);
    rb2=new JRadioButton("Limited");
    rb2.setContentAreaFilled(false);
    rb2.setBounds(350,250,120,37);
    ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(rb1);
    bgroup.add(rb2);
    tf1=new JTextField(20);
    tf1.setBounds(200,100,270,20);
    pf2=new JPasswordField();
    pf2.setBounds(200,150,270,20);
    pf3=new JPasswordField();
    pf3.setBounds(200,200,270,20);
    /*String jpf1Text=Arrays.toString(pf2.getPassword());
    String jpf2Text=Arrays.toString(pf3.getPassword());
   if(jpf1Text.equals(jpf2Text)) {
        l6.setText("");
    }else {
        l6.setText("Password doesn't match..");
    }*/
    b1=new JButton("Save");
    b1.setBounds(220,300,120,37);
    b1.setContentAreaFilled(false);
    b1.setEnabled(false);
    b1.setIcon(new ImageIcon("1456072202_save.png"));
    b2=new JButton("Cancel");
    b2.setBounds(350,300,120,37);
    b2.setContentAreaFilled(false);
    b2.setIcon(new ImageIcon("1456072227_delete.png"));
    p1.add(l1);
    p1.add(l2);
    p1.add(l3);
    p1.add(l4);
    p1.add(l5);
    p1.add(l6);
    p1.add(l7);
    p1.add(tf1);
    p1.add(pf2);
    p1.add(pf3);
    p1.add(rb1);
    p1.add(rb2);
    p1.add(b1);
    p1.add(b2);
    add(p1);
    }}
    class Demo{
    public static void main (String args[]){
    new Example().setVisible(true);     
    }
    }

0 个答案:

没有答案