package com.test;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class LoginViewTest extends JFrame {
JLabel titleNameLabel=new JLabel("学生成绩管理系统",JLabel.CENTER);
SpringLayout spLayout=new SpringLayout();
JPanel centerPanel=new JPanel(spLayout);
JLabel userNameLabel=new JLabel("用户名:");
JLabel userPwdLabel=new JLabel("登录密码:");
JTextField nameTxd=new JTextField();
JPasswordField pwdFiled=new JPasswordField();
JButton loginBtn=new JButton("登录");
JButton resetBtn=new JButton("重置");
public LoginViewTest(){
super("学生成绩管理系统");
Container contentPane =getContentPane();
titleNameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
titleNameLabel.setPreferredSize(new Dimension(0,80));
Font centerFont=new Font("华文行楷",Font.PLAIN,20);
userNameLabel.setFont(centerFont);
nameTxd.setPreferredSize(new Dimension(200,30));
userPwdLabel.setFont(centerFont);
pwdFiled.setPreferredSize(new Dimension(200,30));
loginBtn.setFont(centerFont);
resetBtn.setFont(centerFont);
contentPane.setBackground(Color.pink);
centerPanel.add(userNameLabel);
centerPanel.add(userPwdLabel);
centerPanel.add(nameTxd);
centerPanel.add(pwdFiled);
centerPanel.add(loginBtn);
centerPanel.add(resetBtn);
contentPane.add(titleNameLabel,BorderLayout.NORTH);
contentPane.add(centerPanel,BorderLayout.CENTER);
Spring titleLabelWidth=Spring.width(userNameLabel);
Spring titleTextWidth=Spring.width(nameTxd);
Spring spaceWidth=Spring.constant(20);
Spring totalWidth=Spring.sum(Spring.sum(titleLabelWidth,titleTextWidth),spaceWidth);
int offSetX=totalWidth.getValue()/2;
SpringLayout.Constraints titleLabelCon=spLayout.getConstraints(userNameLabel);
spLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offSetX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
spLayout.putConstraint(SpringLayout.WEST,nameTxd,20,SpringLayout.EAST,userNameLabel);
spLayout.putConstraint(SpringLayout.NORTH,nameTxd,0,SpringLayout.NORTH,userNameLabel);
spLayout.putConstraint(SpringLayout.NORTH,userPwdLabel,20,SpringLayout.SOUTH,userNameLabel);
spLayout.putConstraint(SpringLayout.EAST,userPwdLabel,0,SpringLayout.EAST,userNameLabel);
spLayout.putConstraint(SpringLayout.WEST,pwdFiled,20,SpringLayout.EAST,userPwdLabel);
spLayout.putConstraint(SpringLayout.NORTH,pwdFiled,0,SpringLayout.NORTH,userPwdLabel);
spLayout.putConstraint(SpringLayout.EAST,loginBtn,-20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,loginBtn,50,SpringLayout.SOUTH,pwdFiled);
spLayout.putConstraint(SpringLayout.WEST,resetBtn,20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,resetBtn,50,SpringLayout.SOUTH,pwdFiled);
URL imgUrl=LoginViewTest.class.getClassLoader().getResource("LoginView.jpg");
setIconImage(new ImageIcon(imgUrl).getImage());
setSize(600,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public static void main(String[] args){
new LoginViewTest();
}
}