import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/*
* 登陆-注册界面
* 添加相关的组件
* */
public class loginInterface extends JFrame {
JLabel s=new JLabel("欢迎登陆学生管理系统",JLabel.CENTER);//创建一个JLable标签
SpringLayout springLayout=new SpringLayout();//弹簧布局管理
JPanel panel=new JPanel(springLayout);//创建一个面板
JLabel s1=new JLabel("用户名:");
JTextField s11=new JTextField();//创建一个文本框
JLabel s2=new JLabel("密码:");
JPasswordField s22=new JPasswordField();//创建一个密码框
JButton s3=new JButton("登陆");//创建按钮组件
JButton s4=new JButton("注册");
/*
* 添加构造函数为当前窗口的模板,对各个组件进行相应的设置
* */
public loginInterface(){
Container c=getContentPane();//获取当前窗口中的类容--容器对象
setSize(700,600);//设置Frame窗口大小
c.setBackground(Color.pink);//设置Frame窗口背景颜色
panel.setBackground(Color.lightGray);//设置panel面板的背景颜色
setLocationRelativeTo(null);//窗口显示屏幕中央
setDefaultCloseOperation(EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序。仅在应用程序中使用
setResizable(false);//固定窗口大小
//设置相应的字体
s.setFont(new Font("楷体",Font.BOLD,50));
Font font=new Font("楷体",Font.BOLD,30);
s1.setFont(font);
s2.setFont(font);
s3.setFont(font);
s4.setFont(font);
//设置文本框及密码框的大小
s.setPreferredSize(new Dimension(0,80));
Dimension dimension=new Dimension(200,30);
s11.setPreferredSize(dimension);
s22.setPreferredSize(dimension);
//将相应的组件添加到面板中
panel.add(s1);
panel.add(s11);
panel.add(s2);
panel.add(s22);
panel.add(s3);
panel.add(s4);
//开始弹簧布局
//布局s1--标签,后面组件将其视为参照物进行布局
Spring layoutwhith= Spring.sum(Spring.sum(Spring.width(s1),Spring.width(s11)),Spring.constant(40));
int setupp=layoutwhith.getValue()/2;
springLayout.putConstraint(SpringLayout.WEST,s1,-setupp,SpringLayout.HORIZONTAL_CENTER,panel);
springLayout.putConstraint(SpringLayout.NORTH,s1,20,SpringLayout.NORTH,panel);
//布局s11--文本框
springLayout.putConstraint(SpringLayout.WEST,s11,20,SpringLayout.EAST,s1);
springLayout.putConstraint(SpringLayout.NORTH,s11,0,SpringLayout.NORTH,s1);
//布局s2--标签
springLayout.putConstraint(SpringLayout.EAST,s2,0,SpringLayout.EAST,s1);
springLayout.putConstraint(SpringLayout.NORTH,s2,20,SpringLayout.SOUTH,s1);
//布局s22--密码框
springLayout.putConstraint(SpringLayout.WEST,s22,20,SpringLayout.EAST,s2);
springLayout.putConstraint(SpringLayout.NORTH,s22,0,SpringLayout.NORTH,s2);
//布局s3--登录按钮
springLayout.putConstraint(SpringLayout.WEST,s3,-50,SpringLayout.EAST,s2);
springLayout.putConstraint(SpringLayout.NORTH,s3,30,SpringLayout.SOUTH,s2);
//布局s4--注册按钮
springLayout.putConstraint(SpringLayout.WEST,s4,100,SpringLayout.EAST,s3);
springLayout.putConstraint(SpringLayout.NORTH,s4,0,SpringLayout.NORTH,s3);
//显示类容
c.add(s,BorderLayout.NORTH);
c.add(panel,BorderLayout.CENTER);
setVisible(true);//设置窗口是否可见
}
//main方法
public static void main(String[] args) {
new loginInterface();
}
}
运行结果: