题目:
源代码:
package org.java.application;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class LoginWindow extends JFrame implements ActionListener{
JLabel lbzh, lbmm,lbconfirm;//声明帐号、密码、确认标签组件
JPasswordField pf;//声明输入密码框组件
JTextField tfzh;//声明输入帐号文本框组件
JButton btLogin;//声明登陆按钮组件
String zhanghao;//声明变量账号
String mima;//声明变量密码
@SuppressWarnings("static-access")
LoginWindow(){
JFrame jf = new JFrame("登录窗口");
jf.setVisible(true); //设置窗体可见
jf.setSize(500, 200);//设置窗口初显大小
jf.setLocationRelativeTo(null);
JPanel jp=new JPanel();
jp.setLayout(new BorderLayout(5,10));// 构造一个边界布局
JPanel jpTop = new JPanel();
jpTop.setLayout(new GridLayout(2,2,5,5));
JPanel jpInfo = new JPanel();
jpInfo.setLayout(new FlowLayout());
JPanel jpbtLogin = new JPanel();
jpbtLogin.setLayout(new FlowLayout());
lbzh=new JLabel("帐号",lbzh.CENTER);//创建组件并赋值
lbmm=new JLabel("密码",lbmm.CENTER);
lbconfirm=new JLabel(" ");
tfzh=new JTextField(15);
pf=new JPasswordField(15);
btLogin=new JButton("登录");
btLogin.addActionListener(this);
jpTop.add(lbzh);
jpTop.add(tfzh);
jpTop.add(lbmm);
jpTop.add(pf);
jpInfo.add(lbconfirm);
jpbtLogin.add(btLogin);
jp.add(jpTop,BorderLayout.NORTH);
jp.add(jpInfo,BorderLayout.CENTER);
jp.add(jpbtLogin,BorderLayout.SOUTH);
jf.add(jp);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
zhanghao = tfzh.getText();
mima = pf.getText();
if(zhanghao.equals("zhangyan") && mima.equals("162507233") )
lbconfirm.setText("登录成功");
else
lbconfirm.setText("帐号或密码有误,请重新输入");
}
public static void main(String[] args) {
new LoginWindow();
}
}