用户登录界面代码

[quote]注:这只是个登录测试参考,是练习知识点用的,
具体功能不完善;用户登录名:lianzhou 密码:admin[/quote]

package org.tarena.day03;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UserLogin implements ActionListener {

JButton ok;

JButton back;

JTextField jtf1;

JTextField jpwd;

JFrame jf = new JFrame("用户登录");
public void creatFrame() {

// 添加组建;
JPanel jp = new JPanel(new GridLayout(3, 2));
JLabel jl1 = new JLabel("用户名");
jtf1 = new JTextField(15);
JLabel jl2 = new JLabel("密码");
jpwd = new JTextField(15);
ok = new JButton("确定");
ok.addActionListener(this);
back = new JButton("重置");
back.addActionListener(this);
jp.add(jl1);
jp.add(jtf1);
jp.add(jl2);
jp.add(jpwd);
jp.add(ok);
jp.add(back);

jf.add(jp);

// 把窗口的四个边空出来;
jf.add(BorderLayout.NORTH, new JPanel());
jf.add(BorderLayout.SOUTH, new JPanel());
jf.add(BorderLayout.EAST, new JPanel());
jf.add(BorderLayout.WEST, new JPanel());
// jf.pack();// 可自动调试窗口大小
jf.setSize(250, 150);
jf.setLocation(500, 500);
jf.setVisible(true);
jf.setResizable(false);
}

JFrame nf;

// 创建一个新的窗口;
public void newFrame(boolean flag) {
nf = new JFrame("用户界面");
JLabel jla = new JLabel();
if (flag) {
jla.setText("登录成功");
nf.add(jla);
} else {
// jla.setText("登录失败");
// nf.add(jla);
}
nf.setSize(300, 400);
nf.setLocation(400, 400);
nf.setVisible(true);
nf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
nf.addWindowListener(new WindowListener() {

public void windowActivated(WindowEvent e) {
}

public void windowClosed(WindowEvent e) {
}

public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(null, "是否真的要退出界面?","用户界面",
JOptionPane.YES_NO_CANCEL_OPTION);
if(i==0){
System.exit(i);
}//根据i的返回值来判断是否退出;
}

public void windowDeactivated(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowOpened(WindowEvent e) {
}
});
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
if ("lianzhou".equals(jtf1.getText())
&& "admin".equals(jpwd.getText())) {
jf.dispose();//隐藏登录窗口
newFrame(true);
} else {
jtf1.setText("登录失败!");
jpwd.setText("");
}
} else if (e.getSource() == back) {
jtf1.setText("");
jpwd.setText("");
}
}

public static void main(String[] args) {
UserLogin user = new UserLogin();
user.creatFrame();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值