加下划线的JLabel

时间:2013-04-09 03:36:52

标签: java swing netbeans jlabel

我正在尝试使JLabel加下划线。我到处搜索,但我一无所获。即使在属性中,也没有选项来强调JLabel。我该怎么办?

2 个答案:

答案 0 :(得分:39)

JLabel label = new JLabel("<HTML><U>YOUR TEXT HERE</U></HTML>");
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

OR

JLabel label = new JLabel("Underlined Label");
Font font = label.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
label.setFont(font.deriveFont(attributes));

答案 1 :(得分:35)

JLabel label = new JLabel("Underlined Label");
Font font = label.getFont();
Map<TextAttribute, Object> attributes = new HashMap<>(font.getAttributes());
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
label.setFont(font.deriveFont(attributes));