1个回答
展开全部
密码最好还是别用JTextField,这个是明文,用JPasswordField比较好,用户名可以用JTextField,下面是我几年前写的的一个程序,你可以看看
package packTC;
import java.awt.BorderLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener
{
JLabel lab1 = new JLabel("用户名:");
JTextField username = new JTextField(15);
JLabel lab2 = new JLabel("密 码:");
JPasswordField password = new JPasswordField(15);
JButton btn1 = new JButton("登陆");
JButton btn2 = new JButton("取消");
JPanel pa1 = new JPanel();
JPanel pa2 = new JPanel();
Login()
{
this.setLayout(new BorderLayout());
add(pa1,BorderLayout.CENTER);
add(pa2,BorderLayout.SOUTH);
pa1.add(lab1);
pa1.add(username);
pa1.add(lab2);
pa1.add(password);
pa2.add(btn1);
pa2.add(btn2);
this.setTitle("管理员登陆");
this.setSize(250, 150);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.validate();
btn1.addActionListener(this);
btn2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn1)
{
if(username.getText().equals("123")&&password.getText().equals("123"))
{
new ZipProFrame();
this.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
}
if(e.getSource() == btn2)
{
System.exit(0);
}
}
}
package packTC;
import java.awt.BorderLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener
{
JLabel lab1 = new JLabel("用户名:");
JTextField username = new JTextField(15);
JLabel lab2 = new JLabel("密 码:");
JPasswordField password = new JPasswordField(15);
JButton btn1 = new JButton("登陆");
JButton btn2 = new JButton("取消");
JPanel pa1 = new JPanel();
JPanel pa2 = new JPanel();
Login()
{
this.setLayout(new BorderLayout());
add(pa1,BorderLayout.CENTER);
add(pa2,BorderLayout.SOUTH);
pa1.add(lab1);
pa1.add(username);
pa1.add(lab2);
pa1.add(password);
pa2.add(btn1);
pa2.add(btn2);
this.setTitle("管理员登陆");
this.setSize(250, 150);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.validate();
btn1.addActionListener(this);
btn2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn1)
{
if(username.getText().equals("123")&&password.getText().equals("123"))
{
new ZipProFrame();
this.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
}
if(e.getSource() == btn2)
{
System.exit(0);
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询