
package com.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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("重置");
//声明桌面系统托盘SystemTray
SystemTray systemTray;
TrayIcon trayIcon;
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.width(组件名)用户名和用户名文本框组件
Spring titleLabelWidth=Spring.width(userNameLabel);
Spring titleTextWidth=Spring.width(nameTxd);
Spring spaceWidth=Spring.constant(20);//userNameLabel和nameTxd的间距
Spring totalWidth=Spring.sum(Spring.sum(titleLabelWidth,titleTextWidth),spaceWidth);
int offSetX=totalWidth.getValue()/2;
//1.设置好用户名标签的位置(约束)
SpringLayout.Constraints titleLabelCon=spLayout.getConstraints(userNameLabel);
//用户名标签userNameLabel的西边参考centerPanel组件水平中心点方向左偏移offSetX的距离
spLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offSetX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
//设置用户名标签的垂直标签
spLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
//or使用setY()设置垂直偏移量
// titleLabelCon.setY(Spring.constant(50));//垂直偏移量
//2.设置用户名文本框nameTxd的位置(约束)
spLayout.putConstraint(SpringLayout.WEST,nameTxd,20,SpringLayout.EAST,userNameLabel);
spLayout.putConstraint(SpringLayout.NORTH,nameTxd,0,SpringLayout.NORTH,userNameLabel);
//3.设置密码标签userPwdLabel的位置
spLayout.putConstraint(SpringLayout.NORTH,userPwdLabel,20,SpringLayout.SOUTH,userNameLabel);
spLayout.putConstraint(SpringLayout.EAST,userPwdLabel,0,SpringLayout.EAST,userNameLabel);
//4.设置密码文本框pwdFiled的位置(约束)
spLayout.putConstraint(SpringLayout.WEST,pwdFiled,20,SpringLayout.EAST,userPwdLabel);
spLayout.putConstraint(SpringLayout.NORTH,pwdFiled,0,SpringLayout.NORTH,userPwdLabel);
//5.设置登录按钮loginBtn的位置
spLayout.putConstraint(SpringLayout.EAST,loginBtn,-20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,loginBtn,50,SpringLayout.SOUTH,pwdFiled);
//6.设置重置按钮resetBtn的位置
spLayout.putConstraint(SpringLayout.WEST,resetBtn,20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,resetBtn,50,SpringLayout.SOUTH,pwdFiled);
//判断当前系统是否支持系统托盘
if(SystemTray.isSupported()){
systemTray=SystemTray.getSystemTray();//初始化系统托盘
URL imgUrl = LoginViewTest.class.getClassLoader().getResource("LoginView.jpg");
trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());//初始化托盘图标
trayIcon.setImageAutoSize(true);//托盘图标可以自动缩放大小
try {
systemTray.add(trayIcon);//将托盘图标加入到系统托盘组件中,并抛出异常
} catch (AWTException e) {
e.printStackTrace();
}
//创建一个事件响应,最小化时销毁资源
this.addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
LoginViewTest.this.dispose();
}
});
//托盘事件监听(鼠标单击一次托盘,窗口将会正常显示)
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int clickCont=e.getClickCount();
if(clickCont==1){
LoginViewTest.this.setExtendedState(Jframe.NORMAL);
}
LoginViewTest.this.setVisible(true);
}
});
}
//设置窗口图片
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();
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)