日期:2014-05-20  浏览次数:20888 次

Swing做的QQ,(续一).............................
有兴趣的看下视频:
下载地址:
http://www.cnit2000.com/cnit2000/jqchat.swf
或在线演示地址:
http://www.cnit2000.com/cnit2000/jqchat.htm
Java code

1,看到大家支持的热情很高,我很高兴哦,之前我也发过一个GUI的软件界面图片,当时也是很多人建议我贴代码,因为之前的项目是公司项目,所以就没把代码贴出来,现在这个是我自己打算开发的一个BS+CS的聊天工具,BS打算用AJAX+Struts2.0+Spring+Hibernate来开发,CS软件正如我贴出来的差不多,界面和QQ2007几乎是一样的,(请大家注意一点的是我写的程序是用eclipse3.3+Designer做为开发工具,其中全部是用纯Swing来实现的(个个感觉SWT虽然也能做出漂亮的东西,不过性能始终不如Swing),里面没有用到任何大家所说的插件,皮肤之类的东东~~~)今天给大家看看GUI界面,如果大家的热情度比较高的话等这个软件功能完善后我会贴代码的哦,本来我是做了一个JAVA WEB Start的,但是我自己没有jsp的WEB服务器.所以也看不到演示程序.

下面看我演示:
这是一个登陆页面,可以注册,说明这两个功能是可以实现的,大家可以猜一猜这两个是用什么控件?JLabel or JTextField?其实都不是......其实就是JPanel...是不是很漂亮?
现在看到的就是主界面了,大家不要己为这些按钮都只是画的而没有功能哦,大家看我点状态按钮.说明是可以用的,这里的其它几个面板待开发中.....
我现在打开的是聊天窗口,这里面的功能也还不是很完善,大家知道我刚才点的这两个按钮功能是用什么控件完成的吗?不要己为是JSplitPane完成的哦,也是我自己定义的控件...JSplitPane虽然可以完成以上功能,但是做出来的东东太难看了.再看下面这一条,哈哈,和QQ的面板是不是非常"像"?
再看一个功能,看到没?自动隐藏功能.....

好了,演示就到这里,下面我介绍几个常用函数:

2,下面我给出几个我这个JQchat里面用到的几个函数.

*a******************************************************************************************
/**
这里我现在设置的是windows风格,风格调用代码是:SystemStyles.setWindowsStyle(frame);
 * 设置Windows风格界面
 * @param comp
 */
public static void setWindowsStyle(Component comp) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        initGlobalFontSetting(getFont());
        SwingUtilities.updateComponentTreeUI(comp);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

风格设置之后就得设置字体了.

/**    设置全局字体    
 *     eg:initGlobalFontSetting("宋体");
 */ 
@SuppressWarnings("unchecked")
public static void initGlobalFontSetting(Font fnt) {
    FontUIResource fontRes = new FontUIResource(fnt);
    for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof FontUIResource)
            UIManager.put(key, fontRes);
    }
}

以上的函数只是是做CS的项目基本上都会用到的, 是很基本的东西,第二个函数是我昨天写的,调整窗体大小的函数,如果需要像我一样自定义上面的标题栏,那就要用到这个函数了(调整窗体函数),看到没,我没有用自带的标题栏,而是自己定义的,如果需要自己定义标题栏,那么就需要把原来的标题栏隐藏掉,但是如果隐藏掉标题栏后就不可以调整窗体大小,所以我就写了一个函数.假设我现在需要将这个变大一点点是不是不可以,?因为我把标题栏隐藏掉了,但是如果我把另外一个代码加上去就可以了.
*b******************************************************************************************
以下是代码,大家可以收藏.如果仔细观察相信大家都看得懂..
再看我刚才的这个点击标题栏托动的功能?其实也是要自己写的.大家自己去练练手吧.演示教程就到这里,记住哦,如果大家有兴趣的话等这个软件功能完成后我会在网站上面公布源代码.
今天中秋,祝大家中秋快乐.~


/**
     * adjust the window's size
     * @param frame the Object of window
     * @param a        the length of top 
     * @param b        the length of bottom
     * @param c        the length of left
     * @param d        the length of right
     * @param minWidth    the window's minwidth
     * @param minHeight    the window's minheight
     */
    public static void setCanResize(final Window frame,final int a,final int b,final int c,final int d,final int minWidth,final int minHeight){
        frame.addMouseMotionListener(new MouseMotionAdapter() {
            int x1,y1,x2,y2;
            public void mouseMoved(final MouseEvent e) {
                Point mousePoint = e.getLocationOnScreen();
                Point framePoint = frame.getLocationOnScreen();
                if(mousePoint.x>=framePoint.x && mousePoint.x<=framePoint.x + c && mousePoint.y>=framePoint.y + a && mousePoint.y<=framePoint.y + frame.getHeight() -b){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
                }else if(mousePoint.x>=framePoint.x + frame.getWidth() - d && mousePoint.x <= framePoint.x + frame.getWidth() && mousePoint.y>=framePoint.y + a && mousePoint.y<=framePoint.y + frame.getHeight() -b){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
                }else if(mousePoint.x >=framePoint.x + c && mousePoint.x <=framePoint.x + frame.getWidth() -d && mousePoint.y >=framePoint.y && mousePoint.y <= framePoint.y + a){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
                }else if(mousePoint.x >= framePoint.x + c && mousePoint.x <=framePoint.x + frame.getWidth()-d && mousePoint.y >= framePoint.y + frame.getHeight() -b && mousePoint.y<= framePoint.y + frame.getHeight()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
                }else if(mousePoint.x >= framePoint.x && mousePoint.x < framePoint.x + c && mousePoint.y >= framePoint.y && mousePoint.y < framePoint.y + a){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
                }else if(mousePoint.x > framePoint.x + frame.getWidth() -d && mousePoint.x <= framePoint.x + frame.getWidth() && mousePoint.y >= framePoint.y && mousePoint.y < framePoint.y + a){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
                }else if(mousePoint.x >= framePoint.x && mousePoint.x < framePoint.x + c && mousePoint.y > framePoint.y + frame.getHeight() - b && mousePoint.y <= framePoint.y + frame.getHeight()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
                }else if(mousePoint.x > framePoint.x + frame.getWidth() -d && mousePoint.x <= framePoint.x + frame.getWidth() && mousePoint.y > framePoint.y + frame.getHeight() - b && mousePoint.y <= framePoint.y + frame.getHeight()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
                }else{
                    frame.setCursor(Cursor.getDefaultCursor());
                    x1 = -1;
                    return;
                }
                x1 = framePoint.x;
                y1 = framePoint.y;
                x2 = framePoint.x + frame.getWidth();
                y2 = framePoint.y + frame.getHeight();
            }
            public void mouseDragged(final MouseEvent e) {
                if(x1 < 0){
                    return;
                }
                int type = frame.getCursor().getType();
                Point p = e.getLocationOnScreen();
                switch (type){
                case Cursor.W_RESIZE_CURSOR:
                    frame.setBounds(p.x, y1, (x2 - p.x)>minWidth?(x2 - p.x):minWidth, frame.getHeight()>minHeight?frame.getHeight():minHeight);
                    break;
                case Cursor.E_RESIZE_CURSOR:
                    frame.setBounds(x1, y1, (p.x - x1)>minWidth?(p.x - x1):minWidth, frame.getHeight()>minHeight?frame.getHeight():minHeight);
                    break;
                case Cursor.N_RESIZE_CURSOR:
                    frame.setBounds(x1, p.y, frame.getWidth()>minWidth?(frame.getWidth()):minWidth, (y2 - p.y)>minHeight?(y2 - p.y):minHeight);
                    break;
                case Cursor.S_RESIZE_CURSOR:
                    frame.setBounds(x1, y1, frame.getWidth()>minWidth?(frame.getWidth()):minWidth, (p.y - y1)>minHeight?(p.y - y1):minHeight);
                    break;
                case Cursor.NW_RESIZE_CURSOR:
                    frame.setBounds(p.x, p.y, (x2 - p.x)>minWidth?(x2 - p.x):minWidth, (y2 - p.y)>minHeight?(y2 - p.y):minHeight);
                    break;
                case Cursor.NE_RESIZE_CURSOR:
                    frame.setBounds(x1, p.y, (p.x - x1)>minWidth?(p.x - x1):minWidth, (y2 - p.y)>minHeight?(y2 - p.y):minHeight);
                    break;
                case Cursor.SW_RESIZE_CURSOR:
                    frame.setBounds(p.x, y1, (x2 - p.x)>minWidth?(x2 - p.x):minWidth, (p.y - y1)>minHeight?(p.y - y1):minHeight);
                    break;
                case Cursor.SE_RESIZE_CURSOR:
                    frame.setBounds(x1, y1, (p.x - x1)>minWidth?(p.x - x1):minWidth, (p.y - y1)>minHeight?(p.y - y1):minHeight);
                    break;
                }
            }
        });
    }