这个是稍微又改了一下,改变密码框,并且添加了按钮监控事件。
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UserInterface extends JFrame implements WindowListener,ActionListener{
JFrame jf;
JPanel jpan1,jpan2,jpan3;
JLabel username,password,lspace;
JTextField user;
JPasswordField pass;
JButton login,register;
public void go(){
jf=new JFrame("UserInterface");
jf.setSize(350, 250);
jf.setResizable(false);
jf.setLayout(new GridLayout(6,1,0,5));
jpan1=new JPanel();
jpan2=new JPanel();
jpan3=new JPanel();
username=new JLabel("Username");
password=new JLabel("Password");
user=new JTextField(12);
pass=new JPasswordField(12);
login=new JButton("Login");
register=new JButton("Register");
login.addActionListener(this);
register.addActionListener(this);
jpan1.setSize(200, 50);
jpan1.add(username);
jpan1.add(user);
jpan2.setSize(200, 50);
jpan2.add(password);
jpan2.add(pass);
jpan3.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
jpan3.add(login);
jpan3.add(register);
lspace=new JLabel();
jf.add(lspace);
jf.add(jpan1);
jf.add(jpan2);
lspace=new JLabel();
jf.add(lspace);
jf.add(jpan3);
lspace=new JLabel();
jf.add(lspace);
jf.setVisible(true);
}
public static void main(String[] args){
UserInterface ui=new UserInterface();
ui.go();
}
public void windowDeactivated(WindowEvent e){
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("Login")){
System.out.println("到数据库中去验证用户名和密码");
}
if(e.getActionCommand().equals("Register")){
System.out.println("跳转到注册页面");
}
}
}
[
本帖最后由 琢石成器 于 2013-11-16 22:17 编辑 ]