尝试开发一个登录窗体,包括用户名,密码提交及重置按键

package cn.nyist.swing;

import java.awt.Container;
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.JPasswordField;
import javax.swing.JTextField;

public class my1311text3 extends JFrame{
	
	public my1311text3() {
		setSize(250,200);
		final JPasswordField jt1=new JPasswordField(20);
//		jt1.setEchoChar('*');
		final JTextField jt2=new JTextField(20);
		final JButton jb1=new JButton("重置");
		final JButton jb2=new JButton("登录");
		final JLabel j1=new JLabel("用户名");
		final JLabel j2=new JLabel("密码");
		
		
		setVisible(true);
		Container cp=getContentPane();
		setBounds(300,200,300,150);
		cp.setLayout(null);
		j1.setBounds(10,10,50,18);
		j2.setBounds(10,40,50,18);
		jt2.setBounds(60,10,200,18);
		jt1.setBounds(60,40,200,18);
		jb1.setBounds(80,60,60,30);
		jb2.setBounds(160,60,60,30);
		cp.add(jt2);
		cp.add(jt1);
		cp.add(jb1);
		cp.add(jb2);
		cp.add(j1);
		cp.add(j2);
		jb1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				jt1.setText("");
				jt2.setText("");
			}
			
		});
		jb2.addActionListener(new ActionListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(jt2.getText().trim().equals("yjy")&&new String(jt1.getPassword()).equals("123456")) {
					JOptionPane.showMessageDialog(null,"登录成功");
				}else {
					JOptionPane.showMessageDialog(null, "登录失败");
				}
			}
			
		});
	}
	
	public static void main(String[] args) {
		new my1311text3();
		
	}
	
}

你可能感兴趣的:(java)