
刚好上实验课,给你发一个自己刚写完的, 可能会帮到你的.
实现的功能看截图:
源代码:import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class PasswordVerification extends JFrame implements ActionListener{
JLabel userLabel;//定义用户标签提示
JLabel passwordLabel;//定义密码标签提示
JTextField userText;//定义用户文本框
JPasswordField passwordText;//定义密码文本框
int count=1;//统计输入信息的次数
public PasswordVerification() {
super("请输入信息");
Container container=getContentPane();//得到容器对象container;
container.setLayout(new FlowLayout());//设置默认布局
userLabel=new JLabel("用户名");//创建用户标签
passwordLabel=new JLabel("密 码");//创建密码标签
userText=new JTextField(10);//创建用户文本输入框
passwordText=new JPasswordField(10);//创建密码输入文本框
passwordText.addActionListener(this);//注册事件监听者;
container.add(userLabel);
container.add(userText);
container.add(passwordLabel);
container.add(passwordText);
setSize(400,80);
setVisible(true);
}
public void actionPerformed(ActionEvent e){//事件处理
String userName=new String("陈三");//假设为正确的用户名;
String password=new String("12345678");//假设为正确的密码;
if(e.getSource()==passwordText){
count++;
char[] passwords=passwordText.getPassword();
if(userText.getText().equals(userName)&&password.equals(new String(passwords)))
{
JOptionPane.showMessageDialog(null, "欢迎您:" + userName);
System.exit(0);
}
else if(count>3)
System.exit(0);
else{
JOptionPane.showMessageDialog(null,userText.getText()+"请输入正确信息");
}
}
}
public static void main(String args[]){
PasswordVerification pv=new PasswordVerification();
pv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
MyFrame.settitle("程序左上角显示的名字");
//新建一个面板
JPanel panel = new JPanel();
//新建一个文本框
JTextFiled text = new JTextFiled();//多行的文本框是TextArea
//新建一个按钮
JButton button = new JButton("按钮");
//将文本框放入面板
panel.add(text);
//将按钮放到面板
panel.add(button);
//将面板放到frame
this.add(panel);//或者新建一个frame,然后加入
addWindowListener(
new WindowAdapter(){
System.exit(1);
}
);
}
netBeans中新建个java项目,然后新建一个窗口就行了。
直接把按钮 文本框什么的往里面拖。摆好位置就行。