java怎么绘制文本,在Java中绘制文本,外观和感觉的问题

I've overridden the paintComponent method of an extended JToggleButton so that I can use a TexturePaint fill for the text when the button is toggled. The problem I'm running into is that I can't seem to draw the text using the same font as my look and feel is using as a default. I've tried g2d.setFont(this.getFont()); , where "this" is the button I'm working with. The font is close, but appears bolder than the default text when I draw it. Is there a better way to draw text such that it looks the same as the default, save for the color? Thanks in advance!

解决方案

If you are overriding the paintComponent() method then the Graphics object should already be configured to have the Font of the toggle button. The difference is probably because of anti aliasing which is not turned on by default.

I have found some code that works for me in very limited testing. Try the following in the paintComponent() method:

Graphics2D g2 = (Graphics2D)g.create();

Toolkit toolkit = Toolkit.getDefaultToolkit();

Map map = (Map)(toolkit.getDesktopProperty("awt.font.desktophints"));

if (map != null)

{

g2.addRenderingHints(map);

}

g2.drawString(...);

g2.dispose();

I was warned in this posting - How to set text above and below a JButton icon? - that this will not work on all platforms and LAF's. The comment also gives a suggested solution on how to paint the text.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值