JLabel 与 J 中的文本不符 [英] JLabel not in line with text in J
问题描述
我有这种奇怪的刺激性情况.我有一个插入了文本的 JTextPane.除了文本,还有链接作为 JLabels.下面是一个快照:
I have this wierd irritating situation. I have a JTextPane with text inserted. Alogn with text, there are also Links as JLabels. Below is a snapshot:
现在的问题是:为什么 JLabel(A HTML 链接)与同一行上的文本不平行?它略低于.文字为Calibri",字体为 14.JLabel:
Now the issue is: Why is the JLabel (A HTML Link) not parallel with the text on the same line? It is slightly below. Text is "Calibri" and Font 14. JLabel:
label.setForeground(Color.blue);
label.setFont(new Font("Check",Font.BOLD,14));
makeLinkable(label, new LinkMouseListener());
请帮忙.JLabel 后面的链接是:<html><a href="file:///C:/Users/Jatin/Documents/NetBeansProjects/ImageRename/log/2012-10-5%20Time%2013-52-1.csv">file:///C:/Users/Jatin/Documents/NetBeansProjects/ImageRename/log/2012-10-5%20Time%2013-52-1.csv</a></html>
>
Please help. The link behind JLabel is: <html><a href="file:///C:/Users/Jatin/Documents/NetBeansProjects/ImageRename/log/2012-10-5%20Time%2013-52-1.csv">
file:///C:/Users/Jatin/Documents/NetBeansProjects/ImageRename/log/2012-10-5%20Time%2013-52-1.csv</a></html>
推荐答案
看看 JComponent#setAlignmentY(...): java - 如何将 JLabel 适当地添加到 JEditorPane?- 堆栈溢出
Take a look at JComponent#setAlignmentY(...): java - How to appropriately adding JLabel to JEditorPane? - Stack Overflow
import java.awt.*;
import javax.swing.*;
public class BaselineTest {
public JComponent makeUI() {
JLabel label1 = makeLabel();
JLabel label2 = makeLabel();
String s = "For detailed infomation refer: ";
JTextPane textPane = new JTextPane();
textPane.replaceSelection(s);
textPane.insertComponent(label1);
Dimension d = label2.getPreferredSize();
int baseline = label2.getBaseline(d.width, d.height);
label2.setAlignmentY(baseline/(float)d.height);
textPane.replaceSelection("\n\n"+s);
textPane.insertComponent(label2);
return new JScrollPane(textPane);
}
private static JLabel makeLabel() {
JLabel label = new JLabel("2012-10-05");
label.setForeground(Color.BLUE);
label.setOpaque(false);
return label;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new BaselineTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
这篇关于JLabel 与 J 中的文本不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!