Swing


          天行健 君子以自強(qiáng)不息

          posts - 69, comments - 215, trackbacks - 0, articles - 16
             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Swing DayDayUP之五:帶標(biāo)題欄右鍵菜單

          Posted on 2009-10-18 15:26 zht 閱讀(1233) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Swing

          標(biāo)題位置可以分別設(shè)置為上下左右,4個(gè)位置。如下圖所示:


          /**
          * 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) { // 錯(cuò)誤 
                      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();
              }

          主站蜘蛛池模板: 华安县| 亚东县| 象州县| 家居| 湘阴县| 绵竹市| 克拉玛依市| 康马县| 北票市| 辽阳市| 麦盖提县| 大石桥市| 健康| 西安市| 崇仁县| 木里| 石台县| 建昌县| 双流县| 高碑店市| 胶南市| 白银市| 乐清市| 綦江县| 玉屏| 连南| 溧阳市| 政和县| 临武县| 农安县| 左云县| 新河县| 宁波市| 太仆寺旗| 介休市| 安新县| 美姑县| 溆浦县| 沙洋县| 赣州市| 南昌县|