17
回答
Swing第一集
【腾讯云】学生服务器套餐10元/月 >>>   

1.背景

本人热衷于(也只会)Java,很长时间前有个想法,什么时候能做套类似QQ的聊天软件,我不指望与疼讯抗衡,只为能满足一般的中小企业使用就行。目前市场上面IM产品多,但是觉得都不咋的,所以想自己写,当然我也知道这是一条很漫长的路,如果大家觉得兄弟写的不错,想继续看下去,请有钱的来捧个钱场,有人的来捧个人场。如果大家觉得这样写没什么意思,那就让砖头来得更猛烈些吧,这个就是你们看见的第一集与最后一集(兄弟说的是真的,不是吓唬你的)

2.准备工作

服务器端:Windows Server 2003+OpenFire3.7+MySQL

开发环境:Windows 7+Netbeans7.0.1+JDK1.6U26

3.开始

尽管我们使用的是Netbeans做IDE,都知道它做Swing相当强悍,控件直接往上拖就行,但是这里我们不打算叫大家怎么拖(地球人都会拖),代码全部手写,这一集我们主要讨论下JFrame的重绘,这里我们针对关键代码说明,完整的文件后面提供下载。

我们先看看Swing1.6默认的JFrame是什么样子的

我们再来看看我们手绘的Jframe,还支持颜色渐变哟

这个是带边框的

再来个不带边框的

颜色变变变

我再变

以下为Java代码,直接复制下来即可用

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javaea.ui;

import com.sun.awt.AWTUtilities;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;

/**
 *
 * @author SeamanMei
 */
public class _Login extends JFrame {
    
    int width = 500;
    int height = 500;
    Dimension dimension = new Dimension();
    private Image image;//头像图片
    private GraphicsConfiguration translucencyCapableGC;//图形环境
    final Paint p = new GradientPaint(0.0f, 0.0f, new Color(255, 0, 51, 80), 500, 600, new Color(255, 204, 253, 255), true);
    private JPanel panel;//主面板
    private Frame frame = this;
    private Button btnClose = new Button("Close");
    private Point origin = new Point(); //用于移动窗体 
    Point location = new Point();//保存窗体当前坐标,在窗体最大化和还原时用到
    private boolean isMax = false;
    
    public _Login() {
        setVisibleRegion();
        //获取系统图形环境
        dimension.setSize(width, height);
        translucencyCapableGC = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        setSize(width, height);
        setMinimumSize(dimension);
        setTitle("我是手工重绘的哟");
        getContentPane().setLayout(new BorderLayout());
        panel = new JPanel() {
            
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setPaint(p);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                g2d.setColor(new Color(0, 0, 0));
                Shape shape = null;
                shape = new RoundRectangle2D.Double(0, 0, frame.getWidth() - 1, frame.getHeight() - 1, 5.0D, 5.0D);//绘制窗体边框
                g2d.draw(shape);
                //    g2d.drawImage(image, 8, 35, 40, 40, null);
                //   g2d.drawString("EIM 2011", 5, 15);
            }
        };
        MouseInputListener listener = new MouseInputHandler();//添加窗体事件
        addMouseListener(listener);
        addMouseMotionListener(listener);
        this.setContentPane(panel);
        setUndecorated(true);
        panel.setLayout(null);
        btnClose.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                setExtendedState(JFrame.EXIT_ON_CLOSE);
                dispose();
            }
        });
        btnClose.setBounds(4, 3, 50, 20);
        panel.add(btnClose);
        init();
    }
    //设定可见区域

    public final void setVisibleRegion() {
        Shape shape = null;
        shape = new RoundRectangle2D.Double(0, 0, width, height, 5.5D, 5.5D);
        AWTUtilities.setWindowShape(frame, shape);
    }
    
    public final void setVisibleRegion(int x, int y) {
        Shape shape = null;
        shape = new RoundRectangle2D.Double(0, 0, x, y, 5.5D, 5.5D);
        AWTUtilities.setWindowShape(frame, shape);
    }
    
    private void init() {
      
        try {
            image = ImageIO.read(new File("e:/png/user.png"));
        } catch (Exception e) {
            System.out.println(e);
        }
        setIconImage(image);
        setLocationRelativeTo(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    }
    
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                new _Login().setVisible(true);
            }
        });
    }
    
    private class MouseInputHandler implements MouseInputListener {
        
        private Rectangle r;
        int chx = 0;
        int chy = 0;
        
        public void Max() {
            if (isMax) {
                // location=frame.getLocation();
                frame.setLocation(location);
                frame.setSize(width, height);//设置窗体大小  
                //重新设定可见区域  
                setVisibleRegion();
                frame.repaint();
                isMax = false;
            } else {
                location = frame.getLocation();
                final Rectangle rec = translucencyCapableGC.getBounds();//获取图形环境桌面大小  
                Insets d = Toolkit.getDefaultToolkit().getScreenInsets(translucencyCapableGC);//获取桌面工作区大小  
                frame.setSize(rec.width, rec.height - d.bottom);//设置窗体大小  
                //重新设定可见区域  
                setVisibleRegion(frame.getWidth(), frame.getHeight());
                frame.repaint();
                frame.setLocation(0, 0);
                isMax = true;
            }
        }
        
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                Max();
            }
        }
        
        @Override
        public void mouseEntered(MouseEvent e) {
        }
        
        @Override
        public void mouseExited(MouseEvent e) {
        }

        //鼠标按下
        @Override
        public void mousePressed(MouseEvent e) {
            //记录按下时鼠标的坐标位置
            origin.x = e.getX();
            origin.y = e.getY();
        }
        
        @Override
        public void mouseReleased(MouseEvent e) {
        }

        //鼠标拖拽
        @Override
        public void mouseDragged(MouseEvent e) {
            final Cursor cursor = frame.getCursor();//获取光标显示方式
            final Point point = e.getPoint();//获取鼠标当前坐标
            //获取界面显示区域
            r = frame.getBounds();
            //初始化偏移量
            chx = 0;
            chy = 0;
            //使用单独的线程控制界面变化
            SwingUtilities.invokeLater(new Runnable() {
                
                @Override
                public void run() {
                    //判断光标样式
                    switch (cursor.getType()) {
                        case Cursor.SE_RESIZE_CURSOR://左上角
                            //System.out.println("SE_RESIZE_CURSOR");
                            chx = point.x - origin.x;
                            chy = point.y - origin.y;
                            r.x += chx;
                            r.y += chy;
                            r.width -= chx;
                            r.height -= chy;
                            break;
                        case Cursor.SW_RESIZE_CURSOR://右上角
                            //System.out.println("SW_RESIZE_CURSOR");
                            chx = point.x - origin.x;
                            chy = point.y - origin.y;
                            origin.x = point.x;
                            origin.y = point.y;
                            r.y += chy;
                            r.width += chx;
                            r.height -= chy;
                            break;
                        case Cursor.NE_RESIZE_CURSOR://左下角
                            //System.out.println("NE_RESIZE_CURSOR");
                            chx = point.x - origin.x;
                            chy = point.y - origin.y;
                            origin.x = point.x;
                            origin.y = point.y;
                            r.x += chx;
                            r.width -= chx;
                            r.height += chy;
                            break;
                        case Cursor.NW_RESIZE_CURSOR://右下角
                            //System.out.println("NW_RESIZE_CURSOR");
                            chx = point.x - origin.x;
                            chy = point.y - origin.y;
                            origin.x = point.x;
                            origin.y = point.y;
                            if ((r.width + chx) > frame.getMinimumSize().getWidth()//下边界拉伸高度只能大于最小高度
                                    && (r.height + chy) > frame.getMinimumSize()//下边界拉伸高度只能大于最小高度
                                    .getHeight()) {
                                r.width += chx;
                                r.height += chy;
                            }
                            break;
                        case Cursor.E_RESIZE_CURSOR://左边界
                            //System.out.println("E_RESIZE_CURSOR");
                            chx = point.x - origin.x;
                            r.x += chx;
                            r.width -= chx;
                            break;
                        case Cursor.W_RESIZE_CURSOR://右边界
                            //System.out.println("W_RESIZE_CURSOR");
                            chx = point.x - origin.x;
                            origin.x = point.x;
                            if ((r.width + chx) > frame.getMinimumSize().getWidth())//下边界拉伸高度只能大于最小宽度
                            {
                                r.width += chx;
                            }
                            break;
                        case Cursor.N_RESIZE_CURSOR://上边界
                            //System.out.println("N_RESIZE_CURSOR");
                            chy = origin.y - point.y;
                            r.y -= chy;
                            r.width += chx;
                            r.height += chy;
                            break;
                        case Cursor.S_RESIZE_CURSOR://下边界
                            //System.out.println("S_RESIZE_CURSOR");
                            chy = point.y - origin.y;
                            origin.y = point.y;
                            if ((r.height + chy) > frame.getMinimumSize().getHeight())//下边界拉伸高度只能大于最小高度
                            {
                                r.height += chy;
                            }
                            break;
                        case Cursor.DEFAULT_CURSOR://默认光标样式
                            //System.out.println("DEFAULT_CURSOR");
                            r.x += point.x - origin.x;
                            r.y += point.y - origin.y;
                            break;
                    }
                    //重新设定可见区域
                    setVisibleRegion();
                    //重新设定界面显示区域
                    reshape(r.x, r.y, r.width, r.height);
                    repaint();
                }
            });
        }
        
        @Override
        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();//获取当前坐标
            Point framePoint = new Point(0, 0);//设定界面起点坐标
            Dimension dim = frame.getSize();//获取界面大小
            Rectangle serect = new Rectangle(0, 0, 3, 3);//设定左上角拖动区域
            Rectangle swrect = new Rectangle(dim.width - 3, 0, dim.width, 3);//设定右上角拖动区域
            Rectangle nerect = new Rectangle(0, dim.height - 3, 3, dim.height);//设定左下角拖动区域
            Rectangle nwrect = new Rectangle(dim.width - 3, dim.height - 3, dim.width, dim.height);//设定右下角拖动区域
            if (serect.contains(p)) {//判断光标位置是否在左上角拖动区域
                setCursor(Cursor.SE_RESIZE_CURSOR);
            } else if (swrect.contains(p)) {//判断光标位置是否在右上角拖动区域
                setCursor(Cursor.SW_RESIZE_CURSOR);
            } else if (nerect.contains(p)) {//判断光标位置是否在左下角拖动区域
                setCursor(Cursor.NE_RESIZE_CURSOR);
            } else if (nwrect.contains(p)) {//判断光标位置是否在右下角拖动区域
                setCursor(Cursor.NW_RESIZE_CURSOR);
            } else if (p.x == framePoint.x) {//判断光标位置是否在左边界
                setCursor(Cursor.E_RESIZE_CURSOR);
            } else if (p.x == (dim.width - 1)) {//判断光标位置是否在右边界
                setCursor(Cursor.W_RESIZE_CURSOR);
            } else if (p.y == framePoint.y) {//判断光标位置是否在上边界
                setCursor(Cursor.N_RESIZE_CURSOR);
            } else if (p.y == (dim.height - 1)) {//判断光标位置是否在下边界
                setCursor(Cursor.S_RESIZE_CURSOR);
            } else {//判断光标位置是否在窗口中
                setCursor(Cursor.DEFAULT_CURSOR);
            }
        }
    }
}

希望大家跟帖,高手请键下留情,少仍砖头啊

 

 

举报
顶部