Swing


          天行健 君子以自強不息

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

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

          Posted on 2009-10-18 15:26 zht 閱讀(1239) 評論(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();
              }

          主站蜘蛛池模板: 滨海县| 遵义县| 安多县| 北川| 朝阳区| 罗甸县| 响水县| 防城港市| 左权县| 哈尔滨市| 宜都市| 定襄县| 万安县| 安化县| 怀集县| 浦城县| 和林格尔县| 大冶市| 张家港市| 辽阳县| 哈密市| 龙州县| 勃利县| 定州市| 万山特区| 慈溪市| 白朗县| 闽侯县| 合水县| 宜昌市| 买车| 通许县| 蒙自县| 天津市| 南昌县| 通江县| 日土县| 银川市| 离岛区| 大同市| 抚宁县|