<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Swing


    天行健 君子以自強不息

    posts - 69, comments - 215, trackbacks - 0, articles - 16
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Swing DayDayUP之五:帶標題欄右鍵菜單

    Posted on 2009-10-18 15:26 zht 閱讀(1232) 評論(0)  編輯  收藏 所屬分類: Swing

    標題位置可以分別設置為上下左右,4個位置。如下圖所示:


    /**
    * Title PopupMenu
    *
    * @author zhangtao
    *
    * Msn & Mail: zht_dream@hotmail.com
    */
    public class ZHTPopupMenu extends JPopupMenu {

        public static final int POSITION_TOP = 1;
        public static final int POSITION_BOTTOM = 2;
        public static final int POSITION_LEFT = 3;
        public static final int POSITION_RIGHT = 4;

        private int titleSize = 30;
        private Color titleColor = Color.BLACK;
        private Color titleFillColor = Color.WHITE;
        private boolean titleGradient = true;
        private Color titleGradientColor = new Color(0, 50, 50);
        private Font titleFont = new Font("Dialog", Font.BOLD, 12);
        private int titlePosition = POSITION_LEFT;
        private int titleGap = 5;

        public ZHTPopupMenu() {
            super();
        }

        public ZHTPopupMenu(String label) {
            super(label);
        }

        @Override
        public Insets getInsets() {
            Insets insets = (Insets) super.getInsets().clone();
            switch (titlePosition) {
            case POSITION_TOP:
                insets.top += titleSize;
                break;
            case POSITION_BOTTOM:
                insets.bottom += titleSize;
                break;
            case POSITION_LEFT:
                insets.left += titleSize;
                break;
            case POSITION_RIGHT:
                insets.right += titleSize;
                break;
            }
            return insets;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            Dimension size = this.getSize();
            GradientPaint paint;
            g2d.setColor(titleFillColor);
            String text = this.getLabel();
            int len = 0;//text.length();
            if (text != null && text.trim().length() > 0) {
                len = text.length();
            }
            boolean hasEN = false;
            for (int i = 0; i < len; i++) {
                if (isEN(text.charAt(i))) {
                    hasEN = true;
                    break;
                }
            }
            JLabel label = new JLabel(text);
            label.setFont(titleFont);
            label.setForeground(titleColor);
            Dimension labelSize = label.getPreferredSize();
            switch (titlePosition) {
            case POSITION_TOP: {
                if (titleGradient) {
                    paint = new GradientPaint(0, 0, titleFillColor, size.width, titleSize, titleGradientColor);
                    g2d.setPaint(paint);
                }
                g2d.fillRect(0, 0, size.width, titleSize);
                if (len != 0) {
                    ZHTUtils.paintComponent(g2d, label, this, titleGap, titleGap, labelSize.width, labelSize.height);
                }
            }
                break;
            case POSITION_BOTTOM: {
                if (titleGradient) {
                    paint = new GradientPaint(0, size.height - titleSize, titleFillColor, size.width, size.height, titleGradientColor);
                    g2d.setPaint(paint);
                }
                g2d.fillRect(0, size.height - titleSize, size.width, titleSize);
                if (len != 0) {
                    ZHTUtils.paintComponent(g2d, label, this, titleGap, size.height - titleGap - labelSize.height, labelSize.width, labelSize.height);
                }
            }
                break;
            case POSITION_LEFT: {
                if (titleGradient) {
                    paint = new GradientPaint(0, 0, titleFillColor, titleSize, size.height, titleGradientColor);
                    g2d.setPaint(paint);
                }
                g2d.fillRect(0, 0, titleSize, size.height);
                if (len != 0) {
                    if (hasEN) {
                        g2d.rotate(90 * Math.PI / 180, titleGap, titleGap);
                        ZHTUtils.paintComponent(g2d, label, this, titleGap, titleGap - labelSize.height, labelSize.width, labelSize.height);
                    } else {
                        int sum = 0;
                        for (int i = 0; i < len; i++) {
                            label.setText(text.charAt(i) + "");
                            labelSize = label.getPreferredSize();
                            ZHTUtils.paintComponent(g2d, label, this, titleGap, sum, labelSize.width, labelSize.height);
                            sum += labelSize.height;
                        }
                    }
                }
            }
                break;
            case POSITION_RIGHT: {
                if (titleGradient) {
                    paint = new GradientPaint(size.width - titleSize, 0, titleFillColor, size.width, size.height, titleGradientColor);
                    g2d.setPaint(paint);
                }
                g2d.fillRect(size.width - titleSize, 0, titleSize, size.height);
                if (len != 0) {
                    if (hasEN) {
                        g2d.rotate(90 * Math.PI / 180, size.width - titleSize + titleGap, titleGap);
                        ZHTUtils.paintComponent(g2d, label, this, size.width - titleSize + titleGap, titleGap - labelSize.height, labelSize.width, labelSize.height);
                    } else {
                        int sum = 0;
                        for (int i = 0; i < len; i++) {
                            label.setText(text.charAt(i) + "");
                            labelSize = label.getPreferredSize();
                            ZHTUtils.paintComponent(g2d, label, this, size.width - titleGap - labelSize.width, sum, labelSize.width, labelSize.height);
                            sum += labelSize.height;
                        }
                    }
                }
            }
                break;
            }
        }

        public static boolean isEN(char cn) {
            byte[] bytes = (String.valueOf(cn)).getBytes();
            if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 錯誤 
                return false;
            }
            if (bytes.length == 1) { // 英文字符 
                return true;
            }
            if (bytes.length == 2) { // 中文字符 
                return false;
            }
            return false;
        }

        public int getTitleSize() {
            return titleSize;
        }

        public void setTitleSize(int titleSize) {
            this.titleSize = titleSize;
            this.repaint();
        }

        public Color getTitleColor() {
            return titleColor;
        }

        public void setTitleColor(Color titleColor) {
            this.titleColor = titleColor;
            this.repaint();
        }

        public Color getTitleFillColor() {
            return titleFillColor;
        }

        public void setTitleFillColor(Color titleFillColor) {
            this.titleFillColor = titleFillColor;
            this.repaint();
        }

        public boolean isTitleGradient() {
            return titleGradient;
        }

        public void setTitleGradient(boolean titleGradient) {
            this.titleGradient = titleGradient;
            this.repaint();
        }

        public Color getTitleGradientColor() {
            return titleGradientColor;
        }

        public void setTitleGradientColor(Color titleGradientColor) {
            this.titleGradientColor = titleGradientColor;
            this.repaint();
        }

        public Font getTitleFont() {
            return titleFont;
        }

        public void setTitleFont(Font titleFont) {
            this.titleFont = titleFont;
            this.repaint();
        }

        public int getTitlePosition() {
            return titlePosition;
        }

        public void setTitlePosition(int titlePosition) {
            this.titlePosition = titlePosition;
            this.repaint();
        }

        public int getTitleGap() {
            return titleGap;
        }

        public void setTitleGap(int titleGap) {
            this.titleGap = titleGap;
            this.repaint();
        }

    主站蜘蛛池模板: xxxx日本免费| 亚洲福利在线观看| 亚洲sss综合天堂久久久| 99精品国产成人a∨免费看| 亚洲欧洲国产精品香蕉网| 好吊色永久免费视频大全 | 亚洲AV无码专区在线播放中文| 一级做a爰片久久毛片免费陪 | 亚洲另类小说图片| 久久久高清免费视频| 久9热免费精品视频在线观看| 亚洲熟妇无码AV| 四虎影视大全免费入口| 亚洲人成综合网站7777香蕉| 大学生美女毛片免费视频| 亚洲aⅴ无码专区在线观看| 又粗又硬免费毛片| 成人av片无码免费天天看| 亚洲an天堂an在线观看| 999在线视频精品免费播放观看| 亚洲成年网站在线观看| 免费看一级做a爰片久久| 黄色短视频免费看| 久久精品国产亚洲精品2020| 69成人免费视频无码专区| 美女视频黄视大全视频免费的| 国产亚洲精品无码拍拍拍色欲| 性无码免费一区二区三区在线| ww亚洲ww在线观看国产| 四虎影视精品永久免费网站| a级毛片100部免费观看| 亚洲依依成人精品| 亚洲国产精品人人做人人爱| 国产白丝无码免费视频| 亚洲中文字幕无码中文| 国产亚洲精品不卡在线| 亚欧色视频在线观看免费| 美女视频黄免费亚洲| 亚洲人成网站18禁止一区| 18未年禁止免费观看| 亚洲av乱码一区二区三区按摩|