JAVA编写一个界面 用户登陆系统

2024年11月14日 14:00
有1个网友回答
网友(1):

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

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

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
JLabel lbl1 = new JLabel("用户名:");
JLabel lbl2 = new JLabel("密 码:");
JTextField txt = new JTextField("admin",20);
JPasswordField pwd = new JPasswordField(20);
JButton btn = new JButton("登录");
JPanel pnl = new JPanel();
private int error = 0;

public MainFrame(String title) throws HeadlessException {
super(title);
init();
}

private void init() {
this.setResizable(false);

pwd.setEchoChar('*');

pnl.add(lbl1);
pnl.add(txt);
pnl.add(lbl2);
pnl.add(pwd);
pnl.add(btn);
this.getContentPane().add(pnl);

btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equals(new String(pwd.getPassword()))){
pnl.removeAll();
JLabel lbl3 = new JLabel();
ImageIcon icon = new ImageIcon(this.getClass().getResource("pic.jpg"));
lbl3.setIcon(icon);
pnl.add(lbl3);
}
else{
if(error < 3){
JOptionPane.showMessageDialog(null,"密码输入错误,请再试一次");
error++;
}
else{
JOptionPane.showMessageDialog(null,"对不起,您不是合法用户");
txt.setEnabled(false);
pwd.setEnabled(false);
btn.setEnabled(false);
}
}
}
});
}

public static void main(String[] args) {
MainFrame frm = new MainFrame("测试");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100, 100, 300, 120);
frm.setVisible(true);
}
}

随手写的, 没调试图片, 太麻烦