JTextField 上的圆角并使其通过不同的 PLAF 保持一致
你好,亲爱的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你应该为此创建一个自定义边框。然后,您可以控制插图并在 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.
我遇到了同样的问题,发现调用
文本字段类可以解决问题。我认为即使您声明小部件不透明,它也不会使背景不透明。
I was having the same issue, and found that calling
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.