问题
I am trying to make a JLabel underlined. I searched everywhere, but I got nothing. Even in the properties, there is no option for underlining the JLabel. What can I do?
回答1:
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));
回答2:
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));
来源:https://stackoverflow.com/questions/15892844/underlined-jlabel