JTextField 上的圆角并使其通过不同的 PLAF 保持一致

发布于 2024-11-08 15:20:24 字数 1853 浏览 0 评论 0原文

你好,亲爱的 stackoverflow 用户,

我得到了一个简单的黑客,我在 JTextField 上得到了我一直想要的圆角。
我发现我可以子类化 JTextField 并覆盖 paintComponent(Graphics g)

在这方面,我可以编辑以下内容:

  • 将边框从标准边框更改为 BorderFactory.createEmptyBorder()
  • 将文本字段的外观从矩形更改为圆形矩形。
  • 更改文本的偏移量,使其不靠近圆形边框。 (覆盖getInsets()

现在我正在解决以下问题:

  • 更改选择大小
  • USER将plaf更改为例如Nimbus时,上的外观子类 JTextField 被毁了,我的意思是 Nimbus 绘画例程比我的更受欢迎。所以我混合了 Nimbus 和我的圆形边框画。

简而言之,你们中有人知道我如何用上面写的各种问题来剖析 JTextField 吗?

编写的是我的示例代码,用于在构造函数 setBorder(BorderFactory.createEmptyBorder())setOpaque(false); 内的自定义类 JTextField 中制作圆角边框:

@Override
public Insets getInsets() {
    Insets insets = super.getInsets();
    insets.left += 10;
    return insets;
}

@Override
public Insets getInsets(Insets insets) {
    return insets;
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = Graphics2D)g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, getWidth(), getHeight(), 10, 10);
    Paint backgroundBrush = new GradientPaint(0, 0, new Color(0x383838), 0, getHeight(), new Color(0xCECECE).darker());
    Shape oldClip = g2.getClip();
    g2.setPaint(backgroundBrush);
    g2.clip(r2d);
    g2.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    g2.setClip(oldClip);
    g2.setColor(Color.black);
    g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 10, 10);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2.dispose();
    super.paintComponent(g);
}

Hello dear fellow stackoverflow users,

I got a simple hack where I get my long wanted round corners on a JTextField.
I found that I could subclass JTextField and override paintComponent(Graphics g)

In that regard I could edit the following:

  • change the border from standard border to BorderFactory.createEmptyBorder().
  • change the look on the textfield from rectangular to roundrectangular.
  • change the offset for the text so it wasn't near the round border. (override getInsets())

Now I'm battling with the following issues:

  • Changing the selection size
  • When USER change the plaf to e.g. Nimbus then the look on the subclassed JTextField is ruined, by that I mean Nimbus painting routines is preferred over mine. So I get a mix of Nimbus and my round borderpainting.

So in very short, does any of you know how I dissect the JTextField with the various issues, written above?

Written is my sample code for making rounded borders in a custom class JTextField within the constructor setBorder(BorderFactory.createEmptyBorder()) and setOpaque(false);:

@Override
public Insets getInsets() {
    Insets insets = super.getInsets();
    insets.left += 10;
    return insets;
}

@Override
public Insets getInsets(Insets insets) {
    return insets;
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = Graphics2D)g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, getWidth(), getHeight(), 10, 10);
    Paint backgroundBrush = new GradientPaint(0, 0, new Color(0x383838), 0, getHeight(), new Color(0xCECECE).darker());
    Shape oldClip = g2.getClip();
    g2.setPaint(backgroundBrush);
    g2.clip(r2d);
    g2.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    g2.setClip(oldClip);
    g2.setColor(Color.black);
    g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 10, 10);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2.dispose();
    super.paintComponent(g);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-11-15 15:20:24

我认为你应该为此创建一个自定义边框。然后,您可以控制插图并在 Border 中进行绘制,而不是使用文本字段的 PaintComponent() 方法。

I would think you should be creating a custom Border for this. Then you can control the insets and do the painting in the Border, instead of the paintComponent() method of the text field.

痴者 2024-11-15 15:20:24

我遇到了同样的问题,发现调用

setBackground(new Color(0,0,0,0))

文本字段类可以解决问题。我认为即使您声明小部件不透明,它也不会使背景不透明。

I was having the same issue, and found that calling

setBackground(new Color(0,0,0,0))

on the text field class cleared it up. I think that it is not making the background non opaque even if you declare the widget non opaque.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文