天天看點

登入界面

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestTextField extends Frame implements ActionListener {

TextField txtName;

TextField txtPassword;

Label lblTitle,lblName,lblPassword;

Button btnSubmit;

Button btnReset;

Panel p1,p2,p3,p4;

public TestTextField(){

super("登陸界面");

lblTitle=new Label("請輸入您的使用者資訊:");

lblName=new Label("使用者名:");

txtName=new TextField(10);

lblPassword=new Label("密碼:");

txtPassword=new TextField(10);

txtPassword.setEchoChar('*');

btnSubmit=new Button("送出");

btnReset=new Button("重置");

p1=new Panel();

p2=new Panel();

p3=new Panel();

p4=new Panel();

add(lblTitle,"North");

p1.setLayout(new BorderLayout());

p2.setLayout(new GridLayout(2,1));

p2.add(lblName);

p2.add(lblPassword);

p3.setLayout(new GridLayout(2,1));

p3.add(txtName);

p3.add(txtPassword);

p1.add(p2,"West");

p1.add(p3,"Center");

txtName.addActionListener(this);

txtPassword.addActionListener(this);

p4.add(btnSubmit);

p4.add(btnReset);

add(p1,"Center");

add(p4,"South");

this.btnSubmit.addActionListener(this);

this.btnReset.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

public void actionPerformed(ActionEvent e){

String s=e.getActionCommand();

if(s.equals("重置")){

clear();

}else if(s.equals("送出")){

submit();

}else if(e.getSource()==txtName){

txtPassword.requestFocus();

}else if(e.getSource()==txtPassword){

public void clear(){

txtName.setText("");

txtPassword.setText("");

txtName.requestFocus();

public void submit(){

String n=txtName.getText();

String paw=txtPassword.getText();

if(n.equals("admin") && paw.equals("1234")){

JOptionPane.showMessageDialog(this,"合法使用者,歡迎進入本系統");

}else{

JOptionPane.showMessageDialog(this, "非法使用者,禁止進入本系統");

public static void main(String[] args) {

TestTextField ttf=new TestTextField();

ttf.setSize(200,130);

ttf.setLocation(300,200);

ttf.setVisible(true);