【问题标题】:Rounded Border on one side only java一侧的圆形边框只有java
【发布时间】:2011-10-25 19:55:49
【问题描述】:

我只需要在组件的一侧创建圆角边框。

这段代码创建了一个圆形边框:

  new LineBorder(Color.RED, 3, true)

我见过this thread,它向您展示了如何创建只能在组件的一侧使用的哑光边框,但哑光边框不是圆角的。

是否可以只在一侧有圆形边框?

编辑:

我尝试过像这样使用复合边框:

    cell.setBorder(BorderFactory.createCompoundBorder(
        new LineBorder(borderColor, 3, true),
        BorderFactory.createMatteBorder(0, 3, 0, 0, Color.black)));

但它不起作用......

【问题讨论】:

    标签: java swing border


    【解决方案1】:

    您可以覆盖 LineBorder 的方法并在那里绘制您需要的所有内容 来自 LineBorder 的来源

        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            Color oldColor = g.getColor();
            int i;
    
        /// PENDING(klobad) How/should do we support Roundtangles?
            g.setColor(lineColor);
            for(i = 0; i < thickness; i++)  {
            if(!roundedCorners)
                    g.drawRect(x+i, y+i, width-i-i-1, height-i-i-1);
            else
    SET CLIP HERE TO DRAW ONLY NECESSARY PART
                    g.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, thickness, thickness);
            }
            g.setColor(oldColor);
        }
    

    【讨论】:

    • 替换g.drawRoundRect(...) 似乎并不简单,IMO。但这是一个开始 - 您必须分别绘制圆角和边缘。
    • @Thomas 说起来容易做起来难:P 用弧线画圆角是我无法做到的。我一直在尝试,但还没有成功。
    • 正如我所说,这不是微不足道的,意思是“没那么容易”:)
    • @David 对你的人没有任何好处,但正如你在上一篇文章中一样,我认为你没有只见树木不见森林 :-),java.awt.Border 默认有四个边,我看到了一些关于如何从位置(北/南/西/东)java2s.com/Tutorial/Java/0240__Swing/1530__AbstractBorder.htm 和另一个...中提取/更改/着色特定边框的主题......
    【解决方案2】:

    LineBorder 只支持圆角与否。复合边框(如 JavaDoc 所述)使用一个边框作为外部边框,一个边框作为内部边框,因此不区分左/右或上/下。

    恐怕您要么必须编写自己的 Border 实现,要么寻找其他人已经制作的实现(外部库)。

    【讨论】:

      【解决方案3】:

      这是一个关于如何使用Graphics2D#clipRect() 的示例。 此代码仅保留两个右圆角,并在左侧具有正常边框。如上所述,您必须在自定义 LineBorder 中使用此代码。

      Graphics2D g2d = (Graphics2D) g;
      
      g2d.clipRect(150, 10, 100, 100);
      g2d.draw(new RoundRectangle2D.Double(100, 10, 80, 30, 15, 15));
      
      g2d.setClip(null);
      g2d.clipRect(100, 10, 50, 100);
      g2d.draw(new Rectangle2D.Double(100, 10, 80, 30));
      

      【讨论】:

      • 当我使用它来覆盖paintBorder时,该代码不会绘制圆角边框。 :(
      【解决方案4】:
      public static final int TOP_LEFT = 1;
      public static final int TOP_RIGHT = 2;
      public static final int BOTTOM_LEFT = 4;
      public static final int BOTTOM_RIGHT = 8;
      public static final int ALL_CORNERS = TOP_LEFT + TOP_RIGHT + BOTTOM_LEFT + BOTTOM_RIGHT;
      
      public static void drawRoundRect(Graphics g, Color fillColor, Color borderColor, int x, int y, int width, int height, int radius, int cornerMask)
      {
          // // XXX Old code (without selectively disabled round corners):
          // if (fillColor != null)
          // {
          // og.setColor(fillColor);
          // og.fillRoundRect(x, y, width - 1, height - 1, radius, radius);
          // }
          // if (borderColor != null && !borderColor.equals(fillColor))
          // {
          // og.setColor(borderColor);
          // og.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
          // }
          radius += radius % 2; // so we don't have to deal with rounding issues for odd numbers
          int radiusHalf = radius / 2;
          width--;
          height--;
          if (fillColor != null)
          {
              g.setColor(fillColor);
              // og.fillRoundRect(x, y, width - 1, height - 1, radius, radius);
              if ((cornerMask & TOP_LEFT) > 0)
              {
                  g.fillArc(x, y, radius, radius, 90, 90);
              }
              else
              {
                  g.fillRect(x, y, radiusHalf, radiusHalf);
              }
              if ((cornerMask & TOP_RIGHT) > 0)
              {
                  g.fillArc(x + width - radius, y, radius, radius, 0, 90);
              }
              else
              {
                  g.fillRect(x + width - radiusHalf, y, radiusHalf, radiusHalf);
              }
              if ((cornerMask & BOTTOM_RIGHT) > 0)
              {
                  g.fillArc(x + width - radius, y + height - radius, radius, radius, 270, 90);
              }
              else
              {
                  g.fillRect(x + width - radiusHalf, y + height - radiusHalf, radiusHalf, radiusHalf);
              }
              if ((cornerMask & BOTTOM_LEFT) > 0)
              {
                  g.fillArc(x, y + height - radius, radius, radius, 180, 90);
              }
              else
              {
                  g.fillRect(x, y + height - radiusHalf, radiusHalf, radiusHalf);
              }
      
              g.fillRect(x + radiusHalf, y, width - radius, radiusHalf);
              g.fillRect(x + radiusHalf, y + height - radiusHalf, width - radius, radiusHalf);
              g.fillRect(x, y + radiusHalf, radiusHalf, height - radius);
              g.fillRect(x + width - radiusHalf, y + radiusHalf, radiusHalf, height - radius);
              g.fillRect(x + radiusHalf, y + radiusHalf, width - radius, height - radius);
          }
          if (borderColor != null && !borderColor.equals(fillColor))
          {
              g.setColor(borderColor);
      
              // XXX: there are problems with this when using semi-transparent colors + borderSize > 1
              // XXX: this could be changed to to use ONE draw action using drawShape with GeneralPath.curveTo()
              // XXX: this then could also be used to FILL the shape (see above)
              if ((cornerMask & TOP_LEFT) > 0)
              {
                  g.drawArc(x, y, radius, radius, 90, 90);
              }
              else
              {
                  g.drawLine(x, y, x + radiusHalf, y);
                  g.drawLine(x, y, x, y + radiusHalf);
              }
              if ((cornerMask & TOP_RIGHT) > 0)
              {
                  g.drawArc(x + width - radius, y, radius, radius, 0, 90);
              }
              else
              {
                  g.drawLine(x + width - radiusHalf, y, x + width, y);
                  g.drawLine(x + width, y, x + width, y + radiusHalf);
              }
              if ((cornerMask & BOTTOM_RIGHT) > 0)
              {
                  g.drawArc(x + width - radius, y + height - radius, radius, radius, 270, 90);
              }
              else
              {
                  g.drawLine(x + width - radiusHalf, y + height, x + width, y + height);
                  g.drawLine(x + width, y + height - radiusHalf, x + width, y + height);
              }
              if ((cornerMask & BOTTOM_LEFT) > 0)
              {
                  g.drawArc(x, y + height - radius, radius, radius, 180, 90);
              }
              else
              {
                  g.drawLine(x, y + height, x + radiusHalf, y + height);
                  g.drawLine(x, y + height - radiusHalf, x, y + height);
              }
      
              g.drawLine(x + radiusHalf, y, x + width - radiusHalf, y); // top
              g.drawLine(x + width, y + radiusHalf, x + width, y + height - radiusHalf); // right
              g.drawLine(x + radiusHalf, y + height, x + width - radiusHalf, y + height); // bottom
              g.drawLine(x, y + radiusHalf, x, y + height - radiusHalf); // left
          }
      }
      

      【讨论】:

        【解决方案5】:

        这是一个例子,

        JPanel content = new JPanel(); content.setBorder(BorderFactory.createEmptyBorder(1,30,1,1));

        【讨论】:

          猜你喜欢
          • 2016-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多