2009-08-07 114 views
0

我需要为我的一个JButton中的超链接创建一个字体下划线和蓝色,但似乎字体类没有明显的方法来执行此操作。我不能使用属性文本,因为我不打算用Graphics类来显示它。无论如何,我可以做到这一点?我只需要我的JButton的标题是蓝色和下划线。更改字体的颜色并使其加下划线

回答

0

我最终解决了不能通过用.....标签包围我的字符串来强调文本的问题。

0
JButton button = new JButton("OK"); 
button.setBackground(Color.blue); 

Font buttonFont=new Font(button.getFont().getName(),Font.UNDERLINED+Font.BOLD,button.getFont().getSize()); 
button.setFont(buttonFont); 
+0

我觉得字体下划线不变,实际上可能是 “Font.UNDERLINE”。试试两个。 – RJFalconer 2009-08-07 00:43:15

+0

啊,看: http://stackoverflow.com/questions/325840/what-is-the-constant-value-of-the-underline-font-in-java – RJFalconer 2009-08-07 00:44:56

2

我来不及回复。但无论如何,我会在这里发布它。也许这对别人有帮助。

JButton button = new JButton("Label"); 
HashMap<TextAttribute, Object> textAttrMap = new HashMap<TextAttribute, Object>(); 
textAttrMap.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); 
textAttrMap.put(TextAttribute.FOREGROUND, Color.BLUE); 

button.setFont(button.getFont().deriveFont(textAttrMap)); 

编号:http://docs.oracle.com/javase/tutorial/2d/text/examples/AttributedText.java