完整代码.. http://pan.baidu.com/s/1dDIIxHZ
package com.shhhes.im.client;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;
import com.shhhes.im.listener.WindowMoveListener;
import com.shhhes.im.utils.FontUtil;
import com.shhhes.im.utils.JButtonUtil;
import com.shhhes.im.utils.WindowUtil;
import com.sun.awt.AWTUtilities;
/**
* 登陆界面
* @author 马凯
*
*/
public class LoginWindow extends JWindow {
/**
* 启动
* @param args
*/
public static void main(String[] args) {
new LoginWindow().launchWindow();
}
/**
* 加载窗体
*/
private void launchWindow() {
initWindow();
// initSystemTray();
setContentPane(createMainPanel());
setVisible(true);
}
/**
* 初始化系统托盘
*/
private void initSystemTray() {
if (!SystemTray.isSupported()) {
return;
}
SystemTray systemTray = SystemTray.getSystemTray();
TrayIcon trayIcon = new TrayIcon(new ImageIcon("images/Login/status/offline16.png").getImage(), "QQ2013");
try {
systemTray.add(trayIcon);
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
trayIcon.addMouseListener(new MouseAdapter(){
@Override
public void mouseReleased(MouseEvent e) {
if(e.isMetaDown()){
JWindow window = new JWindow();
Box baseBox = Box.createVerticalBox();
baseBox.setBackground(Color.white);
Container contentPane = window.getContentPane();
contentPane.add(baseBox);
JMenuItem onlineItem = new JMenuItem("我在线上");
onlineItem.setFont(FontUtil.getYaHei());
onlineItem.setIcon(new ImageIcon("images/Login/status/imonline.png"));
baseBox.add(onlineItem);
JMenuItem qmeItem = new JMenuItem("Q我吧");
qmeItem.setFont(FontUtil.getYaHei());
qmeItem.setIcon(new ImageIcon("images/Login/status/Qme.png"));
baseBox.add(qmeItem);
JMenuItem awayItem = new JMenuItem("离开");
awayItem.setFont(FontUtil.getYaHei());
awayItem.setIcon(new ImageIcon("images/Login/status/away.png"));
baseBox.add(awayItem);
JMenuItem busyItem = new JMenuItem("忙碌");
busyItem.setFont(FontUtil.getYaHei());
busyItem.setIcon(new ImageIcon("images/Login/status/busy.png"));
baseBox.add(busyItem);
JMenuItem muteItem = new JMenuItem("请勿打扰");
muteItem.setFont(FontUtil.getYaHei());
muteItem.setIcon(new ImageIcon("images/Login/status/mute.png"));
baseBox.add(muteItem);
window.pack();
Dimension d = window.getSize();
window.setAlwaysOnTop(true);
window.setLocation(e.getX() - (int)d.getWidth(), e.getY() - (int)d.getHeight());
window.setVisible(true);
}
}
});
}
/**
* 最外层的主Panel
* @return
*/
private JPanel createMainPanel() {
ImageJPanel panel = new ImageJPanel();
panel.setLayout(new BorderLayout());
panel.setImage("images/Login/background/noon.jpg");
panel.add(createTopPanel(), BorderLayout.NORTH);
panel.add(createCenterPanel());
panel.add(createBottomPanel(), BorderLayout.SOUTH);
return panel;
}
/**
* 中间用户输入和头像的Panel
* @return
*/
private JPanel createCenterPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setOpaque(false);//设置透明
panel.add(createUserImagePanel(), BorderLayout.WEST);//左边头像
panel.add(createUserInfoPanel());//中间,工号,密码
panel.add(createLinkPanel(), BorderLayout.EAST);//右边注册,找回密码链接
return panel;
}
/**
* 创建链接的Panel
* @return
*/
private JPanel createLinkPanel() {
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setOpaque(false);//设置透明
panel.setPreferredSize(new Dimension(71, 170));
// 注册按钮
JButton regButton = JButtonUtil.getImageButton("images/Login/zhuce_normal.png", "images/Login/zhuce_press.png", "images/Login/zhuce_hover.png");
regButton.setBounds(0, 110, 51, 16);
panel.add(regButton);
// 密码找回按钮
JButton findPwdButton = JButtonUtil.getImageButton("images/Login/mima_normal.png", "images/Login/mima_press.png", "images/Login/mima_hover.png");
findPwdButton.setBounds(0, 145, 51, 16);
panel.add(findPwdButton);
return panel;
}
/**
* 用户信息
* @return
*/
private JPanel createUserInfoPanel() {
JPanel panel = new JPanel();
panel.setOpaque(false);//设置透明
panel.setLayout(null);
panel.setPreferredSize(new Dimension(195, 251));
//帐号
JTextField usercode = new JTextField();
usercode.setBorder(new LineBorder(Color.GRAY, 1));
usercode.setFont(new Font("Verdana", Font.PLAIN, 12));
usercode.setForeground(Color.black);
usercode.setBounds(10, 108, 185, 26);
panel.add(usercode);
// 密码输入框
JPasswordField pwdfPassword = new JPasswordField("");
pwdfPassword.requestFocus(true);
pwdfPassword.setBorder(new LineBorder(Color.GRAY, 1));
pwdfPassword.setFont(FontUtil.getYaHei());
pwdfPassword.setForeground(Color.black);
pwdfPassword.setEchoChar('*');
pwdfPassword.setBounds(10, 141, 185, 26);
panel.add(pwdfPassword);
// 记住密码复选框
final JCheckBox rememberPasswordCheckBox = JButtonUtil.getIconCheckBox( "images/Login/checkbox_normal.png", "images/Login/checkbox_hover.png", "images/Login/checkbox_press.png", "images/Login/checkbox_selected_hover.png");
rememberPasswordCheckBox.setBounds(10, 170, 15, 15);
panel.add(rememberPasswordCheckBox);
//记住密码标签
JLabel rememberPasswordLabel = new JLabel("记住密码");
rememberPasswordLabel.setFont(FontUtil.getYaHei());
rememberPasswordLabel.setForeground(Color.black);
rememberPasswordLabel.setBounds(30, 170, 48, 15);
panel.add(rememberPasswordLabel);
rememberPasswordLabel.addMouseListener(new MouseAdapter() {//优化点击效果
@Override
public void mouseReleased(MouseEvent e) {
rememberPasswordCheckBox.setSelected(!rememberPasswordCheckBox.isSelected());
}
});
// 自动登陆复选框
final JCheckBox autoLoginCheckBox = JButtonUtil.getIconCheckBox( "images/Login/checkbox_normal.png", "images/Login/checkbox_hover.png", "images/Login/checkbox_press.png", "images/Login/checkbox_selected_hover.png");
panel.add(autoLoginCheckBox);
autoLoginCheckBox.setBounds(90, 170, 15, 15);
// 自动登陆标签
JLabel autoLoginLabel = new JLabel("自动登录");
autoLoginLabel.setFont(FontUtil.getYaHei());
autoLoginLabel.setForeground(Color.black);
panel.add(autoLoginLabel);
autoLoginLabel.setBounds(110, 170, 48, 15);
autoLoginLabel.addMouseListener(new MouseAdapter() {//优化点击效果
@Override
public void mouseReleased(MouseEvent e) {
autoLoginCheckBox.setSelected(!autoLoginCheckBox.isSelected());
}
});
return panel;
}
/**
* 用户头像Panel
* @return
*/
private JPanel createUserImagePanel() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(104, 84));//不设置大小和layout,JPanel默认为0
panel.setLayout(null);//不要布局管理器
panel.setOpaque(false);//设置透明
//用户头像图
JLabel userImage = new JLabel(new ImageIcon("images/Login/qqhead.png"));
panel.add(userImage);
userImage.setBounds(22, 107, 80, 79);
//用户头像背景图
JLabel userImageBack = new JLabel(new ImageIcon("images/Login/login_head_white.png"));
panel.add(userImageBack);
userImageBack.setBounds(21, 103, 84, 84);
return panel;
}
/**
* 创建顶部面板
* @return
*/
private JPanel createTopPanel() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.RIGHT));//设置右对齐
panel.setOpaque(false);//设置透明
//最小化按钮
JButton minBtn = JButtonUtil.getMinButton(this);
panel.add(minBtn);
minBtn.setBounds(315, -2, 28, 20);
//关闭按钮
JButton closeBtn = JButtonUtil.getClosedButton(this);
panel.add(closeBtn);
closeBtn.setBounds(342, -2, 39, 20);
return panel;
}
/**
* 创建底部面板
* @return
*/
private JPanel createBottomPanel() {
ImageJPanel panel = new ImageJPanel();
panel.setImage("images/Login/loginbutton_background.jpg");
panel.setPreferredSize(new Dimension(380, 50));
panel.setLayout(null);
//添加登陆按钮
JButton button = JButtonUtil.getImageButton("images/Login/button_blue_normal.png", "images/Login/button_blue_press.png", "images/Login/button_blue_hover.png");
button.setBounds(72, 5, 237, 48);
//添加登陆文字
JLabel label = new JLabel("登 陆");
label.setFont(FontUtil.getYaHei());
label.setForeground(Color.BLACK);
label.setBounds(163, 16, 100, 25);
panel.add(label);
panel.add(button);
return panel;
}
/**
* 初始化信息窗口系统
*/
private void initWindow() {
//设置大小
this.setSize(380, 292);
//设置位置
this.setLocation(WindowUtil.getScreenCenterPoint(this));
//图标啊图标
this.setIconImage(new ImageIcon("images/Login/qqTitle.png").getImage());
this.setAlwaysOnTop(true);
//仿QQ圆角
AWTUtilities.setWindowShape(this, new RoundRectangle2D.Double(0.0D, 0.0D, this.getWidth(), this.getHeight(), 10.0D, 10.0D));
//添加移动效果
WindowMoveListener listener = new WindowMoveListener(this);
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
}
}
2 个解决方案
#1
我试了一下,直接继承JWindow上放JTextField是没法输入的,我也是新手 不太了解机制 我换成JFrame就可以了
设置两个基础的东西就好了
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置两个基础的东西就好了
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
#2
+1
#1
我试了一下,直接继承JWindow上放JTextField是没法输入的,我也是新手 不太了解机制 我换成JFrame就可以了
设置两个基础的东西就好了
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置两个基础的东西就好了
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
#2
+1