//设计登陆窗口界面,当输入账号密码正确时,界面如图一所示,当输入账号密码有误时,界面如图二所示。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Login extends JFrame implements ActionListener{
JLabel lbzh, lbmm,lbconfirm; //声明帐号、密码、确认标签组件
JPasswordField pf;//声明输入密码框组件
JTextField tfzh; //声明输入帐号文本框组件
JButton btLogin;//声明登陆按钮组件
String zhanghao;//声明变量账号
String mima;//声明变量密码
Login(){
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);
}
public void actionPerformed(ActionEvent e) {
zhanghao = tfzh.getText();
mima = pf.getText();
if(zhanghao.equals("libinxue") && mima.equals("123456789") )
lbconfirm.setText("登录成功");
else
lbconfirm.setText("帐号或密码有误,请重新输入");
}
public static void main(String[] args) {
new Login();
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Login extends JFrame implements ActionListener{
JLabel lbzh, lbmm,lbconfirm; //声明帐号、密码、确认标签组件
JPasswordField pf;//声明输入密码框组件
JTextField tfzh; //声明输入帐号文本框组件
JButton btLogin;//声明登陆按钮组件
String zhanghao;//声明变量账号
String mima;//声明变量密码
Login(){
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);
}
public void actionPerformed(ActionEvent e) {
zhanghao = tfzh.getText();
mima = pf.getText();
if(zhanghao.equals("libinxue") && mima.equals("123456789") )
lbconfirm.setText("登录成功");
else
lbconfirm.setText("帐号或密码有误,请重新输入");
}
public static void main(String[] args) {
new Login();
}
}