内容:
1,登录页面布局设置
2,增加系统托盘代码
增加系统托盘主要代码:
//设置托盘图标
SystemTray systemTray;
TrayIcon trayIcon;
/**
* 设置底层托盘
*/
if (SystemTray.isSupported()){
systemTray = SystemTray.getSystemTray();
URL imgUrl = LoginView.class.getClassLoader().getResource("xiaoji.png");
trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
//设置托盘图片大小自动缩放,要不然可能会不显示
trayIcon.setImageAutoSize(true);
//然后加入到系统托盘中
try {
//正式开发中不仅仅是抛异常,而且是打印日志
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
// 然后给这个jf窗体加一个事件,窗体最小化
//使用了匿名内部类
this.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
LoginView.this.dispose();
}
});
//托盘事件监听
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int clickCount = e.getClickCount();
if (clickCount==1){
LoginView.this.setExtendedState(JFrame.NORMAL);
}
LoginView.this.setVisible(true);
}
});
}
代码放入整体之中:
package com.roadjava.student.view;
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 LoginView extends JFrame {
//在外面分析一下他的组件
//最上面的标题
JLabel nameLabel = new JLabel("乐之者java",SwingConstants.CENTER);
SpringLayout springLayout = new SpringLayout();
//设置一个容器,采用弹簧布局
JPanel centerPanel = new JPanel(springLayout);
/**
* 主要内容
*/
JLabel userNameLabel =new JLabel("用户名");
JTextField userTxt = new JTextField();
JLabel pwdLabel = new JLabel("密码");
JPasswordField pwdField = new JPasswordField();
JButton loginBtn = new JButton("登录");
JButton resetBtn = new JButton("重置");
//设置托盘图标
SystemTray systemTray;
TrayIcon trayIcon;
public LoginView(){
super("乐之者java学生管理");
Container contentPane = getContentPane();
//设置最上方的字体,样式, 普通(不加粗,不斜体),大小
nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
nameLabel.setPreferredSize(new Dimension(0,80)); //设置其宽高,宽度默认自动填充,高度80
Font centerFont = new Font("楷体",Font.PLAIN,20);
userNameLabel.setFont(centerFont);
pwdLabel.setFont(centerFont);
loginBtn.setFont(centerFont);
resetBtn.setFont(centerFont);
//输入框大小的宽度和高度
userTxt.setPreferredSize(new Dimension(200,30));
pwdField.setPreferredSize(new Dimension(200,30));
//把组件加入到面板
centerPanel.add(userNameLabel);
centerPanel.add(userTxt);
centerPanel.add(pwdLabel);
centerPanel.add(pwdField);
centerPanel.add(loginBtn);
centerPanel.add(resetBtn);
//然后放到内容面板里面去
contentPane.add(nameLabel,BorderLayout.NORTH); //把标题放到北边
contentPane.add(centerPanel,BorderLayout.CENTER); //把这个放到中间
//弹簧布局
//布局userNameLabel
//求标签和user框+20的一共距离,求和
Spring childWidth=Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(userTxt)),Spring.constant(20));
//然后设置
int offsetX=childWidth.getValue()/2;
springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offsetX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
//userNameLabel距离北边的centerPanel,20px
springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
//userTxt
springLayout.putConstraint(SpringLayout.WEST,userTxt,20,SpringLayout.EAST,userNameLabel);
springLayout.putConstraint(SpringLayout.NORTH,userTxt,0,SpringLayout.NORTH,userNameLabel);
//
//pwdLabel
springLayout.putConstraint(SpringLayout.EAST,pwdLabel,0,SpringLayout.EAST,userNameLabel);
springLayout.putConstraint(SpringLayout.NORTH,pwdLabel,50,SpringLayout.NORTH,userNameLabel);
//pwdField
springLayout.putConstraint(SpringLayout.WEST,pwdField,20,SpringLayout.EAST,pwdLabel);
springLayout.putConstraint(SpringLayout.NORTH,pwdField,0,SpringLayout.NORTH,pwdLabel);
//loginBtn
springLayout.putConstraint(SpringLayout.WEST,loginBtn,20,SpringLayout.WEST,pwdLabel);
springLayout.putConstraint(SpringLayout.NORTH,loginBtn,20,SpringLayout.SOUTH,pwdLabel);
//resetBtn
springLayout.putConstraint(SpringLayout.WEST,resetBtn,40,SpringLayout.EAST,loginBtn);
springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,loginBtn);
contentPane.add(nameLabel,BorderLayout.NORTH);
contentPane.add(centerPanel,BorderLayout.CENTER);
/**
* 设置底层托盘
*/
if (SystemTray.isSupported()){
systemTray = SystemTray.getSystemTray();
URL imgUrl = LoginView.class.getClassLoader().getResource("xiaoji.png");
trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
//设置托盘图片大小自动缩放,要不然可能会不显示
trayIcon.setImageAutoSize(true);
//然后加入到系统托盘中
try {
//正式开发中不仅仅是抛异常,而且是打印日志
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
// 然后给这个jf窗体加一个事件,窗体最小化
//使用了匿名内部类
this.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
LoginView.this.dispose();
}
});
//托盘事件监听
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int clickCount = e.getClickCount();
if (clickCount==1){
LoginView.this.setExtendedState(JFrame.NORMAL);
}
LoginView.this.setVisible(true);
}
});
}
//设置图标
URL resource = LoginView.class.getClassLoader().getResource("xiaoji.png");
setIconImage(new ImageIcon(resource).getImage());
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new LoginView();
}
}
6,绑定事件
看一下结构
LoginView代码
package com.roadjava.student.view;
import com.roadjava.handler.LoginHandler;
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 LoginView extends JFrame {
//在外面分析一下他的组件
//最上面的标题
JLabel nameLabel = new JLabel("乐之者java",SwingConstants.CENTER);
SpringLayout springLayout = new SpringLayout();
//设置一个容器,采用弹簧布局
JPanel centerPanel = new JPanel(springLayout);
/**
* 主要内容
*/
JLabel userNameLabel =new JLabel("用户名");
JTextField userTxt = new JTextField();
JLabel pwdLabel = new JLabel("密码");
JPasswordField pwdField = new JPasswordField();
JButton loginBtn = new JButton("登录");
JButton resetBtn = new JButton("重置");
//设置托盘图标
SystemTray systemTray;
TrayIcon trayIcon;
//在这里定义一下
LoginHandler loginHandler;
public LoginView(){
super("乐之者java学生管理");
loginHandler = new LoginHandler(this);
Container contentPane = getContentPane();
//设置最上方的字体,样式, 普通(不加粗,不斜体),大小
nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
nameLabel.setPreferredSize(new Dimension(0,80)); //设置其宽高,宽度默认自动填充,高度80
Font centerFont = new Font("楷体",Font.PLAIN,20);
userNameLabel.setFont(centerFont);
pwdLabel.setFont(centerFont);
loginBtn.setFont(centerFont);
resetBtn.setFont(centerFont);
//输入框大小的宽度和高度
userTxt.setPreferredSize(new Dimension(200,30));
pwdField.setPreferredSize(new Dimension(200,30));
//把组件加入到面板
centerPanel.add(userNameLabel);
centerPanel.add(userTxt);
centerPanel.add(pwdLabel);
centerPanel.add(pwdField);
centerPanel.add(loginBtn);
centerPanel.add(resetBtn);
loginBtn.addActionListener(loginHandler);
resetBtn.addActionListener(loginHandler);
//增加按键事件
loginBtn.addKeyListener(loginHandler);
//然后放到内容面板里面去
contentPane.add(nameLabel,BorderLayout.NORTH); //把标题放到北边
contentPane.add(centerPanel,BorderLayout.CENTER); //把这个放到中间
//弹簧布局
layoutCenter();
contentPane.add(nameLabel,BorderLayout.NORTH);
contentPane.add(centerPanel,BorderLayout.CENTER);
/**
* 设置底层托盘
*/
if (SystemTray.isSupported()){
systemTray = SystemTray.getSystemTray();
URL imgUrl = LoginView.class.getClassLoader().getResource("xiaoji.png");
trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
//设置托盘图片大小自动缩放,要不然可能会不显示
trayIcon.setImageAutoSize(true);
//然后加入到系统托盘中
try {
//正式开发中不仅仅是抛异常,而且是打印日志
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
// 然后给这个jf窗体加一个事件,窗体最小化
//使用了匿名内部类
this.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
LoginView.this.dispose();
}
});
//托盘事件监听
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int clickCount = e.getClickCount();
if (clickCount==1){
LoginView.this.setExtendedState(JFrame.NORMAL);
}
LoginView.this.setVisible(true);
}
});
}
//设置loginBtn为默认按钮,点击回车,会触发这个按钮
getRootPane().setDefaultButton(loginBtn);
//设置图标
URL resource = LoginView.class.getClassLoader().getResource("xiaoji.png");
setIconImage(new ImageIcon(resource).getImage());
setSize(600,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
private void layoutCenter() {
//布局userNameLabel
//求标签和user框+20的一共距离,求和
Spring childWidth=Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(userTxt)),Spring.constant(20));
//然后设置
int offsetX=childWidth.getValue()/2;
springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offsetX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
//userNameLabel距离北边的centerPanel,20px
springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
//userTxt
springLayout.putConstraint(SpringLayout.WEST,userTxt,20,SpringLayout.EAST,userNameLabel);
springLayout.putConstraint(SpringLayout.NORTH,userTxt,0,SpringLayout.NORTH,userNameLabel);
//
//pwdLabel
springLayout.putConstraint(SpringLayout.EAST,pwdLabel,0,SpringLayout.EAST,userNameLabel);
springLayout.putConstraint(SpringLayout.NORTH,pwdLabel,50,SpringLayout.NORTH,userNameLabel);
//pwdField
springLayout.putConstraint(SpringLayout.WEST,pwdField,20,SpringLayout.EAST,pwdLabel);
springLayout.putConstraint(SpringLayout.NORTH,pwdField,0,SpringLayout.NORTH,pwdLabel);
//loginBtn
springLayout.putConstraint(SpringLayout.WEST,loginBtn,20,SpringLayout.WEST,pwdLabel);
springLayout.putConstraint(SpringLayout.NORTH,loginBtn,20,SpringLayout.SOUTH,pwdLabel);
//resetBtn
springLayout.putConstraint(SpringLayout.WEST,resetBtn,40,SpringLayout.EAST,loginBtn);
springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,loginBtn);
}
public static void main(String[] args) {
new LoginView();
}
public JTextField getUserTxt() {
return userTxt;
}
public void setUserTxt(JTextField userTxt) {
this.userTxt = userTxt;
}
public JPasswordField getPwdField() {
return pwdField;
}
public void setPwdField(JPasswordField pwdField) {
this.pwdField = pwdField;
}
}
LoginHandler代码(主要用来实现功能)
package com.roadjava.handler;
import com.roadjava.student.view.LoginView;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class LoginHandler extends KeyAdapter implements ActionListener {
private LoginView loginView;
public LoginHandler(LoginView loginView){
this.loginView = loginView;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jButton = (JButton) e.getSource();//获取源,并强转换为jbutton
String text = jButton.getText();
if ("重置".equals(text)){
//原来重置,这个功能就是将内容变成空内容呀
loginView.getUserTxt().setText("");
loginView.getPwdField().setText("");
//System.out.println("重置");
}else if ("登录".equals(text)){
login();
// System.out.println("登录"); //所以说,想要把数据输出到控制台,就直接使用sout就可以了
}
}
private void login() {
String user = loginView.getUserTxt().getText();
char[] chars= loginView.getPwdField().getPassword();
String pwd =new String(chars);
System.out.println(user+":"+pwd);
//查询db
boolean flag =false;
if (flag) {
}else{
//模态对话框,不点击,就不会取消
JOptionPane.showMessageDialog(loginView,"用户名密码错误");
}
}
@Override
public void keyPressed(KeyEvent e){
if (KeyEvent.VK_ENTER == e.getKeyCode()){
login();
}
}
}
使用了模态对话框
重置功能:
笔记:
测试功能前,先控制台输出:直接sout即可
模态对话框,就那一句
重置功能就是直接把文本框内容设定为空,空格都不用填