public class Login {
private static JFrame frame;
public Login() {
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new JFrame();
frame.setTitle("登录页面");
frame.setSize(670, 300);
frame.setLocation(200, 200);
frame.setAlwaysOnTop(true);
frame.getContentPane().setLayout(null);
// 参数输入位置一
JLabel userName = new JLabel("*用户名:");
// 文字设置
userName.setForeground(Color.red);
userName.setBounds(30, 30, 120, 30);
userName.setLocation(50, 30);
userName.setFont(new Font("宋体", Font.PLAIN, 14));
frame.getContentPane().add(userName);
// 参数输入框一
JTextField userNameValue = new JTextField();
userNameValue.setLocation(134, 31);
userNameValue.setSize(454, 30);
userNameValue.setPreferredSize(new Dimension(100, 50));
userNameValue.setBackground(Color.WHITE);
// 对用户输入内容进行验证
userNameValue.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
// TODO Auto-generated method stub
JTextField textField = (JTextField) e.getSource();
String text = textField.getText();
if (text.length() == 0) {
return;
}
char charAt = text.charAt(text.length() - 1);
// 数字 0-9
// 英文字母 A-z
// 中文最常用的输入范围是u4E00-u9FA5,也有使用U+4E00~ U+9FFF 的,目前已知 U+9FA6~U+9FFF 之间的字符还属于空码
if (!(charAt >= '0' && charAt <= '9' || charAt >= 'A' && charAt <= 'z'
|| charAt >= '\u4E00' && charAt <= '\u9FA5')) {
// 提示
JOptionPane.showMessageDialog(textField, "只能输入中文,大小写字母,数字", "提示",
JOptionPane.PLAIN_MESSAGE);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// 去掉JTextField末尾字符
textField.setText(text.substring(0, text.length() - 1));
}
});
}
}
});
frame.getContentPane().add(userNameValue);
// 参数输入位置二
JLabel password = new JLabel("*密码:");
// 文字设置
password.setForeground(Color.red);
password.setBounds(71, 79, 120, 30);
password.setLocation(50, 80);
password.setFont(new Font("宋体", Font.PLAIN, 14));
frame.getContentPane().add(password);
// 参数输入框二
JTextField passwordValue = new JTextField("");
passwordValue.setLocation(134, 70);
passwordValue.setSize(454, 30);
passwordValue.setPreferredSize(new Dimension(100, 50));
passwordValue.setBackground(Color.WHITE);
// 对用户输入内容进行验证
passwordValue.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
// TODO Auto-generated method stub
JTextField textField = (JTextField) e.getSource();
String text = textField.getText();
if (text.length() == 0) {
return;
}
char charAt = text.charAt(text.length() - 1);
// 数字 0-9
// 英文字母 A-z
// 中文最常用的输入范围是u4E00-u9FA5
// 也有使用U+4E00~ U+9FFF 的,但目前 U+9FA6~U+9FFF 之间的字符还属于空码,暂时还未定义,但不能保证以后不会被定义
if (!(charAt >= '0' && charAt <= '9' || charAt >= 'A' && charAt <= 'z')) {
// 提示
JOptionPane.showMessageDialog(textField, "只能输入大小写字母,数字", "提示",
JOptionPane.PLAIN_MESSAGE);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// 去掉JTextField末尾字符
textField.setText(text.substring(0, text.length() - 1));
}
});
}
}
});
frame.getContentPane().add(passwordValue);
// 单选框
JCheckBox savePWCheckBox = new JCheckBox("保存密码");
// 默认不被选中
savePWCheckBox.setSelected(false);
savePWCheckBox.setBounds(50, 115, 130, 30);
savePWCheckBox.setLocation(50, 120);
frame.getContentPane().add(savePWCheckBox);
// 登录按钮
JButton loginButton = new JButton("登录");
loginButton.setBounds(200, 170, 120, 30);
loginButton.setLocation(200, 170);
// 事件监听--跳转文件界面
loginButton.addActionListener(getActionListener(userNameValue, passwordValue, frame));
// 增加快捷键
loginButton.registerKeyboardAction(getActionListener(userNameValue, passwordValue, frame),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
frame.getContentPane().add(loginButton);
// 取消按钮
JButton cancellButton = new JButton("取消");
cancellButton.setBounds(615, 337, 120, 30);
cancellButton.setLocation(350, 170);
cancellButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 这里需要注意 dispose是可以后台运行的
frame.dispose();
}
});
// 增加快捷键
cancellButton.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
frame.getContentPane().add(cancellButton);
// 这句话是窗口关闭确认的重点--即能否后台运行的重点
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
int option = JOptionPane.showConfirmDialog(frame, "确定退出系统? ", "提示 ",
JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
// 固定设置
frame.setVisible(true);// 显示
frame.setResizable(true);// 窗口可调
} catch (Exception e) {
e.printStackTrace();
}
}
});
// 添加应用托盘功能
if (SystemTray.isSupported()) {// 判断系统是否支持托盘
SystemTray tray = SystemTray.getSystemTray();// 获取系统托盘
// 创建一个托盘图标对象
Image image = Toolkit.getDefaultToolkit().getImage("resources/images/e6599b2722aefcc37912e87658aaa293.png");
TrayIcon icon = new TrayIcon(image);
/*
* try { trayicon = new
* TrayIcon(ImageIO.read(getClass().getClassLoader().getResource("d11.png"))
* ,"Tray demo",popup); } catch (IOException e1) {
* System.out.println("图片加载失败!"); }
*/
icon.setToolTip("FileUpload");
icon.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (e.getClickCount() == 2) {
frame.setVisible(true);
}
}
});
// 图片显示不出来的一个关键点--很多人做这块图片无法显示很可能是图片大小不能自适应系统
icon.setImageAutoSize(true);
try {
// 将托盘图表添加到系统托盘
tray.add(icon);
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 创建托盘弹出菜单
PopupMenu menu = new PopupMenu();
// 添加一个用于退出的按钮
MenuItem item = new MenuItem("exit");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(item);
// 添加弹出菜单到托盘图标
icon.setPopupMenu(menu);
}
}
// 登录验证
public static ActionListener getActionListener(JTextField userNameValue, JTextField passwordValue, JFrame frame) {
ActionListener actionListener = new ActionListener() {
// TODO Auto-generated method stub
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的用户名密码
String userNameWord = userNameValue.getText();
String passwordWord = passwordValue.getText();
// 判断用户名密码是否正确
if ("zlq".equals(userNameWord) && "123".equals(passwordWord)) {
// 关闭当前窗口
frame.dispose();
// 跳转到文件上传界面
FileUpload fileUpload = new FileUpload();
fileUpload.main(null);
}
if ("".equals(userNameWord) && "".equals(passwordWord)) {
// 提示
JOptionPane.showMessageDialog(frame, "不能为空", "提示", JOptionPane.PLAIN_MESSAGE);
}
}
};
return actionListener;
}
}