我想在 Jlabel 文本中包含可点击的单词,并且我想为每个单词注册一个 MouseListener 或获取被点击的单词(否则)。下图展示了我想要的。这可能吗?!
最佳答案
不,您只能使整个 JLabel 可点击。但是您当然可以将几个 JLabel 并排放置,只使少数几个可点击(并且外观不同)。
您也可以使用 HTML,例如在 JEditorPane
中,并通过 HTML 使某些词可点击,如果这适合您的需要。我找到了关于那个的东西 here ,这就是您的样子:
JEditorPane jep = new JEditorPane("text/html", "Click <a href='pdf'>here</a> to display a PDF file. For more information click <a href='info'>here</a>.");
jep.setEditable(false);
jep.setOpaque(false);
jep.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent hle) {
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
if (hle.getDescription().equalsIgnoreCase("pdf")) {
// display PDF
} else if (hle.getDescription().equalsIgnoreCase("info")) {
// display information
}
}
}
});
关于java - JLabel 中的可点击词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804894/