默认
发表评论 73
想开发IM:买成品怕坑?租第3方怕贵?找开源自已撸?尽量别走弯路了... 找站长给点建议
JAVA Swing 仿微信PC界面实现(内含源码)
阅读(134945) | 评论(73 收藏11 淘帖 4
本帖最后由 lp563161210 于 2015-12-9 14:59 编辑

效果图如下


JAVA Swing 仿微信PC界面实现(内含源码)_QQ截图20151208174613.jpg JAVA Swing 仿微信PC界面实现(内含源码)_QQ截图20151208174621.jpg JAVA Swing 仿微信PC界面实现(内含源码)_QQ截图20151208174630.jpg JAVA Swing 仿微信PC界面实现(内含源码)_QQ截图20151208174607.jpg

即时通讯网 - 即时通讯开发者社区! 来源: - 即时通讯开发者社区!

标签:Java swing

jibu.rar

415.53 KB, 下载次数: 206

售价: 10 金币  [记录]  [如何获取金钱?]

上一篇:网站验证码破解实战研究(一)下一篇:[已解决] java swing 怎么实现QQ聊天气泡效果
推荐方案
评论 73
然而,此处无码。。。
本帖最后由 lp563161210 于 2015-12-8 18:05 编辑

HaoziFrame 自定义组件


package com.haozi.HZJAR.FRAME;

import java.awt.Color;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.haozi.HZJAR.BUTTON.HaoziButton;
import com.haozi.Utils.UIUtils;

public class HaoziFrame extends JFrame{

        private int borderWidth = 5;
        
        private JPanel CenterPanel;
        //边框
        private JPanel TopBorderPanel;
        private JPanel DownBorderPanel;
        private JPanel LeftBorderPanel;
        private JPanel RightBorderPanel;
        private JPanel LeftTopBorderPanel;
        private JPanel LeftDownBorderPanel;
        private JPanel RightTopBorderPanel;
        private JPanel RightDownBorderPanel;
        
        private Color BorderColor;
        
        /**
         * 标题面板
         */
        private JPanel TitlePanel;
        private JLabel TitleText;
        private int TitlePanelHeigth=30;
        private int TitlePanelStartX = 0;
        private int TitlePanelButtonWidth = 30;
        private HaoziButton Min;
        private HaoziButton Max;
        private HaoziButton Close;
        private Color TitleColor = new Color(250, 250, 250);
        /**
         * 0 居中
         * 1 左
         * 2 右
         */
        private byte TitlePanel_TitleLocation = 0;
        //内容
        private JPanel BodyPanel;
        private int bodyPanelStartX=0;
        
        private Point loc = null;
        private Point tmp = null;
        private boolean isDragged = false;
        
        private int newX;
        private int newY;
        
        private BufferedImage img;
        
        public HaoziFrame(int w,int h){
                super("Haozi Frame 1.0");
                setSize(w, h);
                setLocationRelativeTo(null);
                setUndecorated(true);
                init();
        }
        /**
         * 初始化面板信息
         */
        public void init(){
                initCenterPanel();
        }
        /**
         * 初始化中心面板
         */
        public void initCenterPanel(){
                CenterPanel = new JPanel();
                CenterPanel.setLayout(null);
                CenterPanel.setBackground(new Color(242, 242, 242));
                CenterPanel.add(TopBoderPanel());
                CenterPanel.add(DownBorderPanel());
                CenterPanel.add(LeftBorderPanel());
                CenterPanel.add(RightBorderPanel());
                CenterPanel.add(LeftTopBorderPanel());
                CenterPanel.add(LeftDownBorderPanel());
                CenterPanel.add(RightTopBorderPanel());
                CenterPanel.add(RightDownBorderPanel());
                CenterPanel.add(TitlePanel());
                CenterPanel.add(BodyPanel());
                add(CenterPanel);
        }
        
        public void resetComponent(){
                //重置上边框面板
                TopBorderPanel.setBounds(borderWidth, 0, getWidth()-(borderWidth*2), borderWidth);
                //重置下边框面板
                DownBorderPanel.setBounds(borderWidth, getHeight()-borderWidth, getWidth()-(borderWidth*2), borderWidth);
                //重置左边框面板
                LeftBorderPanel.setBounds(0, borderWidth, borderWidth, getHeight()-(borderWidth*2));
                //重置右边框面板
                RightBorderPanel.setBounds(getWidth()-borderWidth, borderWidth, borderWidth, getHeight()-(borderWidth*2));
                
                //重置左下边框
                LeftDownBorderPanel.setBounds(0, getHeight()-borderWidth, borderWidth, borderWidth);
                //重置右上边框
                RightTopBorderPanel.setBounds(getWidth()-borderWidth, 0, borderWidth, borderWidth);
                //重置右下边框
                RightDownBorderPanel.setBounds(getWidth()-borderWidth, getHeight()-borderWidth, borderWidth, borderWidth);
                //重置标题
                TitlePanel.setBounds(TitlePanelStartX, borderWidth, getWidth()-borderWidth-TitlePanelStartX, TitlePanelHeigth);
                //重置标题文本
                switch (TitlePanel_TitleLocation) {
                case 0:{
                        TitleText.setBounds(TitlePanel.getWidth()/2-getTitle().length()*8/2, TitlePanel.getHeight()/2-6, getTitle().length()*12, 12);
                        break;
                }
                case 1:{
                        TitleText.setBounds(12, TitlePanel.getHeight()/2-6, getTitle().length()*12, 12);
                        break;
                }
                case 2:{
                        TitleText.setBounds(TitlePanel.getWidth()-getTitle().length()*8-(TitlePanelButtonWidth*3), TitlePanel.getHeight()/2-6, getTitle().length()*12, 12);
                        break;
                }
                default:
                        break;
                }
                //重置最小化,最大化,关闭按钮
                Min.setBounds(TitlePanel.getWidth()-TitlePanelButtonWidth*3, 0, TitlePanelButtonWidth, TitlePanelHeigth);
                Max.setBounds(TitlePanel.getWidth()-TitlePanelButtonWidth*2, 0, TitlePanelButtonWidth, TitlePanelHeigth);
                Close.setBounds(TitlePanel.getWidth()-TitlePanelButtonWidth*1, 0, TitlePanelButtonWidth, TitlePanelHeigth);
                //重置内容
                BodyPanel.setBounds(bodyPanelStartX, TitlePanelHeigth+borderWidth, getWidth()-bodyPanelStartX-(borderWidth), getHeight()-(borderWidth*2)-TitlePanelHeigth);
                //重置其他内容
                resetOtherComponent();
        }
        /**
         * 该方法交由子类去重写
         */
        public void resetOtherComponent(){
                
        }
        
        /**
         * 初始化上边框面板
         */
        public JPanel TopBoderPanel(){
                TopBorderPanel = new JPanel();
                TopBorderPanel.setBounds(borderWidth, 0, getWidth()-(borderWidth*2), borderWidth);
                TopBorderPanel.setBackground(BorderColor);
                //注册监听
                TopBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setLocation(getX(), getY()+newY);
                                setSize(getWidth(), getHeight()-newY);
                                resetComponent();
                        }
                });
                return TopBorderPanel;
        }
        
        /**
         * 初始化下边框面板
         */
        public JPanel DownBorderPanel(){
                DownBorderPanel = new JPanel();
                DownBorderPanel.setBounds(borderWidth, getHeight()-borderWidth, getWidth()-(borderWidth*2), borderWidth);
                DownBorderPanel.setBackground(BorderColor);
                DownBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setSize(getWidth(), getHeight()+newY);
                                resetComponent();
                        }
                });
                return DownBorderPanel;
        }
        
        /**
         * 初始化左边框面板
         */
        public JPanel LeftBorderPanel(){
                LeftBorderPanel = new JPanel();
                LeftBorderPanel.setBounds(0, borderWidth, borderWidth, getHeight()-(borderWidth*2));
                LeftBorderPanel.setBackground(BorderColor);
                LeftBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setLocation(getX()+newX, getY());
                                setSize(getWidth()-newX, getHeight());
                                resetComponent();
                        }
                });
                return LeftBorderPanel;
        }
        /**
         * 初始化右边框面板
         */
        public JPanel RightBorderPanel(){
                RightBorderPanel = new JPanel();
                RightBorderPanel.setBounds(getWidth()-borderWidth, borderWidth, borderWidth, getHeight()-(borderWidth*2));
                RightBorderPanel.setBackground(BorderColor);
                RightBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setSize(getWidth()+newX, getHeight());
                                resetComponent();
                        }
                });
                return RightBorderPanel;
        }
        
        /**
         * 初始化左上角边框面板
         */
        public JPanel LeftTopBorderPanel(){
                LeftTopBorderPanel = new JPanel();
                LeftTopBorderPanel.setBounds(0, 0, borderWidth, borderWidth);
                LeftTopBorderPanel.setBackground(BorderColor);
                LeftTopBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setLocation(getX()+newX, getY()+newY);
                                setSize(getWidth()-newX, getHeight()-newY);
                                resetComponent();
                        }
                });
                return LeftTopBorderPanel;
        }
        
        /**
         * 初始化左下角边框面板
         */
        public JPanel LeftDownBorderPanel(){
                LeftDownBorderPanel = new JPanel();
                LeftDownBorderPanel.setBounds(0, getHeight()-borderWidth, borderWidth, borderWidth);
                LeftDownBorderPanel.setBackground(BorderColor);
                LeftDownBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setLocation(getX()+newX, getY());
                                setSize(getWidth()-newX, getHeight()+newY);
                                resetComponent();
                        }
                });
                return LeftDownBorderPanel;
        }
        
        /**
         * 初始化右上角边框面板
         */
        public JPanel RightTopBorderPanel(){
                RightTopBorderPanel = new JPanel();
                RightTopBorderPanel.setBounds(getWidth()-borderWidth, 0, borderWidth, borderWidth);
                RightTopBorderPanel.setBackground(BorderColor);
                RightTopBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setLocation(getX(), getY()+newY);
                                setSize(getWidth()+newX, getHeight()-newY);
                                resetComponent();
                        }
                });
                return RightTopBorderPanel;
        }
        
        /**
         * 初始化右下角边框面板
         */
        public JPanel RightDownBorderPanel(){
                RightDownBorderPanel = new JPanel();
                RightDownBorderPanel.setBounds(getWidth()-borderWidth, getHeight()-borderWidth, borderWidth, borderWidth);
                RightDownBorderPanel.setBackground(BorderColor);
                RightDownBorderPanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                newX = e.getX();
                                newY = e.getY();
                                setLocation(getX(), getY());
                                setSize(getWidth()+newX, getHeight()+newY);
                                resetComponent();
                        }
                });
                return RightDownBorderPanel;
        }
        
        /**
         * 初始化标题面板
         */
        public JPanel TitlePanel(){
                TitlePanel = new JPanel();
                TitlePanel.setLayout(null);
                TitlePanel.setBounds(TitlePanelStartX, borderWidth, getWidth()-borderWidth-TitlePanelStartX, TitlePanelHeigth);
                TitlePanel.setBackground(TitleColor);
                //设置标题文本
                TitleText = new JLabel(getTitle());
                switch (TitlePanel_TitleLocation) {
                case 0:{
                        TitleText.setBounds(TitlePanel.getWidth()/2-getTitle().length()*8/2, TitlePanel.getHeight()/2-6, getTitle().length()*12, 12);
                        break;
                }
                case 1:{
                        TitleText.setBounds(12, TitlePanel.getHeight()/2-6, getTitle().length()*12, 12);
                        break;
                }
                case 2:{
                        TitleText.setBounds(TitlePanel.getWidth()-getTitle().length()*8-(TitlePanelButtonWidth*3), TitlePanel.getHeight()/2-6, getTitle().length()*12, 12);
                        break;
                }
                default:
                        break;
                }
                //添加标题栏监听事件
                TitlePanel.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                                // TODO Auto-generated method stub
                                isDragged = false;
                        }
                        @Override
                        public void mousePressed(MouseEvent e) {
                                // TODO Auto-generated method stub
                                tmp = new Point(e.getX(), e.getY());
                                isDragged = true;
                        }
                });
                TitlePanel.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                // TODO Auto-generated method stub
                                if(isDragged){
                                        loc = new Point(getLocation().x+e.getX()-tmp.x, getLocation().y+e.getY()-tmp.y);
                                        setLocation(loc);
                                }
                        }
                });
                //添加三个按钮
                Min = new HaoziButton();
                Min.setFont_color(Color.black);
                Min.setBounds(TitlePanel.getWidth()-TitlePanelButtonWidth*3, 0, TitlePanelButtonWidth, TitlePanelHeigth);
                img = UIUtils.LoadImage("data/TitlePanel/min1.png");
                Min.setIcon(img);
                img = UIUtils.LoadImage("data/TitlePanel/min2.png");
                Min.setRolloverIcon(img);
                img = UIUtils.LoadImage("data/TitlePanel/min3.png");
                Min.setPressedIcon(img);
                Min.setActionListener(new ActionListener() {
                        
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                setExtendedState(JFrame.ICONIFIED);
                        }
                });
                
                Max = new HaoziButton();
                Max.setFont_color(Color.black);
                Max.setBounds(TitlePanel.getWidth()-TitlePanelButtonWidth*2, 0, TitlePanelButtonWidth, TitlePanelHeigth);
                img = UIUtils.LoadImage("data/TitlePanel/mix1.png");
                Max.setIcon(img);
                img = UIUtils.LoadImage("data/TitlePanel/mix2.png");
                Max.setRolloverIcon(img);
                img = UIUtils.LoadImage("data/TitlePanel/mix3.png");
                Max.setPressedIcon(img);
                Max.setActionListener(new ActionListener() {
                        
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                if(getExtendedState() != JFrame.MAXIMIZED_BOTH){
                                        setExtendedState(JFrame.MAXIMIZED_BOTH);
                                }else if(getExtendedState() == JFrame.MAXIMIZED_BOTH){
                                        setExtendedState(JFrame.NORMAL);
                                }
                                resetComponent();
                        }
                });
                
                Close = new HaoziButton();
                Close.setFont_color(Color.black);
                Close.setBounds(TitlePanel.getWidth()-TitlePanelButtonWidth*1, 0, TitlePanelButtonWidth, TitlePanelHeigth);
                img = UIUtils.LoadImage("data/TitlePanel/close1.png");
                Close.setIcon(img);
                img = UIUtils.LoadImage("data/TitlePanel/close2.png");
                Close.setRolloverIcon(img);
                img = UIUtils.LoadImage("data/TitlePanel/close3.png");
                Close.setPressedIcon(img);
                Close.setActionListener(new ActionListener() {
                        
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                System.exit(0);
                        }
                });
                TitlePanel.add(TitleText);
                TitlePanel.add(Min);
                TitlePanel.add(Max);
                TitlePanel.add(Close);
                return TitlePanel;
        }
        
        public JPanel BodyPanel(){
                BodyPanel = new JPanel();
                BodyPanel.setLayout(null);
                BodyPanel.setBounds(bodyPanelStartX, TitlePanelHeigth+borderWidth, getWidth()-bodyPanelStartX-(borderWidth*2), getHeight()-(borderWidth*2)-TitlePanelHeigth);
                BodyPanel.setBackground(Color.black);
                return BodyPanel;
        }
        
        public void addTO(Component com){
                BodyPanel.add(com);
        }
        
        public void addTO(Component comp, Object constraints) {
                // TODO Auto-generated method stub
                BodyPanel.add(comp, constraints);
        }
        
        public void setLayoutTo(LayoutManager manager) {
                // TODO Auto-generated method stub
                BodyPanel.setLayout(manager);
        }
        
        public int getBorderWidth() {
                return borderWidth;
        }
        public void setBorderWidth(int borderWidth) {
                this.borderWidth = borderWidth;
        }
        public JPanel getCenterPanel() {
                return CenterPanel;
        }
        public void setCenterPanel(JPanel centerPanel) {
                CenterPanel = centerPanel;
        }
        public JPanel getTopBorderPanel() {
                return TopBorderPanel;
        }
        public void setTopBorderPanel(JPanel topBorderPanel) {
                TopBorderPanel = topBorderPanel;
        }
        public JPanel getDownBorderPanel() {
                return DownBorderPanel;
        }
        public void setDownBorderPanel(JPanel downBorderPanel) {
                DownBorderPanel = downBorderPanel;
        }
        public JPanel getLeftBorderPanel() {
                return LeftBorderPanel;
        }
        public void setLeftBorderPanel(JPanel leftBorderPanel) {
                LeftBorderPanel = leftBorderPanel;
        }
        public JPanel getRightBorderPanel() {
                return RightBorderPanel;
        }
        public void setRightBorderPanel(JPanel rightBorderPanel) {
                RightBorderPanel = rightBorderPanel;
        }
        public JPanel getLeftTopBorderPanel() {
                return LeftTopBorderPanel;
        }
        public void setLeftTopBorderPanel(JPanel leftTopBorderPanel) {
                LeftTopBorderPanel = leftTopBorderPanel;
        }
        public JPanel getLeftDownBorderPanel() {
                return LeftDownBorderPanel;
        }
        public void setLeftDownBorderPanel(JPanel leftDownBorderPanel) {
                LeftDownBorderPanel = leftDownBorderPanel;
        }
        public JPanel getRightTopBorderPanel() {
                return RightTopBorderPanel;
        }
        public void setRightTopBorderPanel(JPanel rightTopBorderPanel) {
                RightTopBorderPanel = rightTopBorderPanel;
        }
        public JPanel getRightDownBorderPanel() {
                return RightDownBorderPanel;
        }
        public void setRightDownBorderPanel(JPanel rightDownBorderPanel) {
                RightDownBorderPanel = rightDownBorderPanel;
        }
        public Color getBorderColor() {
                return BorderColor;
        }
        public void setBorderColor(Color borderColor) {
                BorderColor = borderColor;
        }
        public JPanel getTitlePanel() {
                return TitlePanel;
        }
        public void setTitlePanel(JPanel titlePanel) {
                TitlePanel = titlePanel;
        }
        public JLabel getTitleText() {
                return TitleText;
        }
        public void setTitleText(JLabel titleText) {
                TitleText = titleText;
        }
        public int getTitlePanelHeigth() {
                return TitlePanelHeigth;
        }
        public void setTitlePanelHeigth(int titlePanelHeigth) {
                TitlePanelHeigth = titlePanelHeigth;
        }
        public int getTitlePanelStartX() {
                return TitlePanelStartX;
        }
        public void setTitlePanelStartX(int titlePanelStartX) {
                TitlePanelStartX = titlePanelStartX;
        }
        public int getTitlePanelButtonWidth() {
                return TitlePanelButtonWidth;
        }
        public void setTitlePanelButtonWidth(int titlePanelButtonWidth) {
                TitlePanelButtonWidth = titlePanelButtonWidth;
        }
        public HaoziButton getMin() {
                return Min;
        }
        public void setMin(HaoziButton min) {
                Min = min;
        }
        public HaoziButton getMax() {
                return Max;
        }
        public void setMax(HaoziButton max) {
                Max = max;
        }
        public HaoziButton getClose() {
                return Close;
        }
        public void setClose(HaoziButton close) {
                Close = close;
        }
        public Color getTitleColor() {
                return TitleColor;
        }
        public void setTitleColor(Color titleColor) {
                TitleColor = titleColor;
                TitlePanel.setBackground(titleColor);
        }
        public byte getTitlePanel_TitleLocation() {
                return TitlePanel_TitleLocation;
        }
        public void setTitlePanel_TitleLocation(byte titlePanel_TitleLocation) {
                TitlePanel_TitleLocation = titlePanel_TitleLocation;
        }
        public Point getLoc() {
                return loc;
        }
        public void setLoc(Point loc) {
                this.loc = loc;
        }
        public Point getTmp() {
                return tmp;
        }
        public void setTmp(Point tmp) {
                this.tmp = tmp;
        }
        public boolean isDragged() {
                return isDragged;
        }
        public void setDragged(boolean isDragged) {
                this.isDragged = isDragged;
        }
        public int getNewX() {
                return newX;
        }
        public void setNewX(int newX) {
                this.newX = newX;
        }
        public int getNewY() {
                return newY;
        }
        public void setNewY(int newY) {
                this.newY = newY;
        }
        public JPanel getBodyPanel() {
                return BodyPanel;
        }
        public void setBodyPanel(JPanel bodyPanel) {
                BodyPanel = bodyPanel;
        }
        public int getBodyPanelStartX() {
                return bodyPanelStartX;
        }
        public void setBodyPanelStartX(int bodyPanelStartX) {
                this.bodyPanelStartX = bodyPanelStartX;
                BodyPanel.setBounds(bodyPanelStartX, TitlePanelHeigth+borderWidth, getWidth()-bodyPanelStartX-(borderWidth*2), getHeight()-(borderWidth*2)-TitlePanelHeigth);
        }
        
        
        
}

点评

JackJiang  说:
现在有码了!  (9 年前)

HaoziButton  自定义组件

package com.haozi.HZJAR.BUTTON;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.ItemSelectable;
import java.awt.MenuContainer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.Serializable;

import javax.imageio.ImageIO;
import javax.print.attribute.TextSyntax;
import javax.swing.JComponent;
import javax.swing.SwingConstants;

public class HaoziButton extends JComponent implements MouseListener{

	//设置按钮文字的坐标位置
	private int TextX = 20;
	private int TextY = 18;
	//按下图片
	private BufferedImage PressedIcon = null;
	//激活图片
	private BufferedImage RolloverIcon = null;
	//静态图片
	private BufferedImage Icon = null;
	//按钮文字
	private String text="";
	//监听事件
	private ActionListener actionListener;
	//使用的字体
	private Font font_type;
	//字体的颜色
	private Color font_color;
	/**
	 * 按钮的状态
	 * 0-静态
	 * 1-激活
	 * 2-按下
	 */
	private byte state= 0;
	/**
	 * setPressedIcon
	 * setRolloverIcon
	 * setIcon
	 */
	public HaoziButton() {
		// TODO Auto-generated constructor stub
		addMouseListener(this);
		repaint();
	}
	
	public HaoziButton(BufferedImage icon){
		this.Icon = icon;
		addMouseListener(this);
		repaint();
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		switch (state) {
		case 0:{
			if(getIcon()==null){
				g.setColor(new Color(242, 242, 242));
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				break;
			}
			g.drawImage(getIcon(), 0, 0, this.getWidth(), this.getHeight(),null);
			break;
		}
		case 1:{
			if(getPressedIcon()==null){
				g.setColor(Color.GREEN);
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				break;
			}
			g.drawImage(getPressedIcon(), 0, 0, this.getWidth(), this.getHeight(),null);
			break;
		}
		case 2:{
			if(getRolloverIcon()==null){
				g.setColor(Color.yellow);
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				break;
			}
			g.drawImage(getRolloverIcon(), 0, 0, this.getWidth(), this.getHeight(),null);
			break;
		}
		default:
			break;
		}
		//计算文字坐标
		FontMetrics fm = g.getFontMetrics();
		Rectangle2D rec = fm.getStringBounds(text, g);
		TextX = (int) (this.getWidth()/2-rec.getWidth()/2);
		TextY = (int) (this.getHeight()/2+rec.getHeight()/4);
		g.setColor(font_color);
		g.drawString(text, TextX, TextY);
	}
	
	public int getTextX() {
		return TextX;
	}

	public void setTextX(int textX) {
		TextX = textX;
		repaint();
	}

	public int getTextY() {
		return TextY;
	}

	public void setTextY(int textY) {
		TextY = textY;
	}

	public BufferedImage getPressedIcon() {
		return PressedIcon;
	}

	public void setPressedIcon(BufferedImage pressedIcon) {
		PressedIcon = pressedIcon;
	}

	public BufferedImage getRolloverIcon() {
		return RolloverIcon;
	}

	public void setRolloverIcon(BufferedImage rolloverIcon) {
		RolloverIcon = rolloverIcon;
	}

	public BufferedImage getIcon() {
		return Icon;
	}

	public void setIcon(BufferedImage icon) {
		Icon = icon;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		if(actionListener!=null){
			actionListener.actionPerformed(null);
		}
		setState((byte) 0);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		setState((byte) 2);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		setState((byte) 1);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		setState((byte) 0);
	}

	public byte getState() {
		return state;
	}

	public void setState(byte state) {
		this.state = state;
		repaint();
	}

	public ActionListener getActionListener() {
		return actionListener;
	}

	public void setActionListener(ActionListener actionListener) {
		this.actionListener = actionListener;
	}

	public Font getFont_type() {
		return font_type;
	}

	public void setFont_type(Font font_type) {
		this.font_type = font_type;
	}

	public Color getFont_color() {
		return font_color;
	}

	public void setFont_color(Color font_color) {
		this.font_color = font_color;
	}

}


HaoziScrolBar 自定义组件


package com.haozi.HZJAR.SCROLBAR;


import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicScrollBarUI;

import com.haozi.Utils.UIUtils;

public class HaoziScrolBar extends BasicScrollBarUI{

	private BufferedImage track;
	private BufferedImage down;
	private BufferedImage up;
	
	public HaoziScrolBar(String model) {
		// TODO Auto-generated constructor stub
		if(model.equals("haozi")){
			track = UIUtils.LoadImage("Data/ScrolBar/track.png");
			down = UIUtils.LoadImage("Data/ScrolBar/down.png");
			up = UIUtils.LoadImage("Data/ScrolBar/up.png");
		}
	}
	
	@Override
	protected void configureScrollBarColors() {
		// TODO Auto-generated method stub
		//滑道
		trackColor = Color.black;
//		trackHighlightColor = Color.green;
	}
	// 重绘滑块的滑动区域背景 
	@Override
	protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
		// TODO Auto-generated method stub
	}
	//重绘滑条
	@Override
	protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
		// TODO Auto-generated method stub
//		// 把绘制区的x,y点坐标定义为坐标系的原点
		g.translate(thumbBounds.x, thumbBounds.y);
		g.drawImage(track,0,0,20,thumbBounds.height-1,null);
	}
	
	@Override
	protected JButton createIncreaseButton(int orientation) {
		// TODO Auto-generated method stub
		JButton haozi = new JButton(new ImageIcon(down));
		haozi.setBackground(new Color(247, 242, 239));
		haozi.setBorder(null);
		haozi.setFocusPainted(false);
		return haozi;
	}
	
	@Override
	protected JButton createDecreaseButton(int orientation) {
		// TODO Auto-generated method stub
		JButton haozi = new JButton(new ImageIcon(up));
		haozi.setBackground(new Color(247, 242, 239));
		haozi.setBorder(null);
		haozi.setFocusPainted(false);
		return haozi;
	}
	public BufferedImage getTrack() {
		return track;
	}
	public void setTrack(BufferedImage track) {
		this.track = track;
	}
	public BufferedImage getDown() {
		return down;
	}
	public void setDown(BufferedImage down) {
		this.down = down;
	}
	public BufferedImage getUp() {
		return up;
	}
	public void setUp(BufferedImage up) {
		this.up = up;
	}
	
}


很厉害很厉害!
代码个人风格明显,我猜是学C#开始的
引用:隔壁老王 发表于 2015-12-8 18:16
很厉害很厉害!
代码个人风格明显,我猜是学C#开始的

没学过C#,学java之前,学了点C
引用:lp563161210 发表于 2015-12-8 18:18
没学过C#,学java之前,学了点C

引用:apollo 发表于 2015-12-8 18:43
无奈,金钱不足,得回复10次

好东西必须顶,来 给  耗子 顶顶~
引用:RIOWalker 发表于 2015-12-8 20:08
好东西必须顶,来 给  耗子 顶顶~

要  积分  才能下载 还得刷积分
看起来还不错哎
引用:lp563161210 发表于 2015-12-8 18:04
[mw_shl_code=actionscript3,true]package com.haozi.HZJAR.BUTTON;

import java.awt.Color;

感谢耗子同学的分享!
怎样才能下一份呢,界面挺好看的
引用:alvin198761 发表于 2015-12-9 16:35
怎样才能下一份呢,界面挺好看的

有附件可以下载哦
不错,的确很帅
好东西,学习下!!!
谢谢作者的无私奉献
额  写的还不错啊  呵呵
引用:RIOWalker 发表于 2015-12-8 20:18
多次  回复   凑   积分  很是   无奈  啊

没分的感觉很痛苦
打赏楼主 ×
使用微信打赏! 使用支付宝打赏!

返回顶部