package sjl.login;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
class LoginAeim extends JFrame{
public void setTitle(String title) {
super.setTitle(title);
}
//声明需要用到的一些控件
private JLabel title;
private JLabel username;
private JLabel password;
private JTextField usernametext;
private JPasswordField passwordtext;
private JButton login;
private JButton cancel;
private JPanel pal;
private JLabel labimg;
private ImageIcon titleimg;
private ImageIcon img;
//窗体的构造方法
public LoginAeim(){
pal = new JPanel(); //创建面板
pal.setLayout(null); //设置默认布局为空. 在下面的布局中使用左边距和上边距来对位
this..add(pal); //将面板添加到窗体内容窗格中
titleimg = new ImageIcon("src/sjl/login/laoluo.jpg"); //创建图片对象,并取出图片.此处使用的是相对路径。
title = new JLabel(titleimg); //创建一个标签,同时传入要显示的图片
title.setIcon(titleimg); //设置要显示的图标
title.setBounds(0, 0, 400, 145); //标签的位置
//创建标签,用于显示最上面的标题中的图片 //将装有图片的标签添加到面板上
pal.add(title);
username = new JLabel("用户名",SwingConstants.RIGHT); //创建标签,设置其显示内容为用户名,并设置右对齐。
username.setBounds(280, 150, 60, 20); //设置标签的位置。 左边距,上边距,高,宽。
pal.add(username); //将标签添加到面板上
password = new JLabel("密 码",SwingConstants.RIGHT); //创建标签,设置其显示内容为密码,并设置右对齐。
password.setBounds(280, 200, 60, 20); //设置标签的位置。 左边距,上边距,高,宽。
pal.add(password); //将标签添加到面板上。
usernametext = new JTextField(); //创建广本框,输入用户名
usernametext.setBounds(100, 150, 180, 30); //设置文本框的位置
usernametext.setEditable(true); //设置文本框为可编辑
pal.add(usernametext); //将输入用户名的文本框添加到面板上
passwordtext = new JPasswordField(); //创建密码框,用于输入密码
passwordtext.setEditable(true); //设置密码框为可编辑
passwordtext.setBounds(100, 200, 180, 30); //设置文本框的位置
pal.add(passwordtext); //将密码框添加到面板上
login = new JButton("登录"); //创建登录按钮
login.setBounds(100, 250, 80, 30); //设置登录按钮的距离和尺寸。 (左边距,上边距,宽度,高度)
pal.add(login); //将登录按钮添加到面板上
cancel = new JButton("取消"); //创建取消按钮
cancel.setBounds(200, 250, 80, 30); //设置取消按钮的距离和尺寸。 (左边距,上边距,宽度,高度)
pal.add(cancel); //将取消按钮添加到面板上
this.setBounds(400, 200,400,330); //设置登录窗体的尺寸
img = new ImageIcon("src/sjl/login/photo.jpg"); //创建图片对象,并取出图片.使用的相对路来表示图片的位置
labimg = new JLabel(); //创建一个标签,同时传入要显示的图片
labimg.setIcon(img); //设置要显示的图标
labimg.setBounds(10, 150, 90, 80); //设置图片标签的位置以及大小
pal.add(labimg); //将图片标签添加到面板上
this.add(pal); //将面板添加到窗体上
}
}
public class Login {
public static void main(String[] args) {
JFrame frm = new LoginAeim(); //创建对象,将LoginAeim实例化.
frm.setTitle("学生管理登录-SJL"); //设置窗体的标题
frm.setDefaultCloseOperation(frm.EXIT_ON_CLOSE); //设置默认关闭方式。按右上角的关闭按钮进行关闭。
frm.setResizable(false); //设置最大化按钮不可用
frm.setVisible(true); //设置窗体可见
}
}