2

我有这种奇怪的恼人情况。我有一个插入文本的 JTextPane。Alogn with text,也有链接作为 JLabels。下面是一张快照: 在此处输入图像描述

现在的问题是:为什么 JLabel(A HTML 链接)与同一行上的文本不平行?它略低于。文本为“Calibri”,字体为 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>

4

1 回答 1

2

看一下 JComponent#setAlignmentY(...): java - 如何将 JLabel 适当地添加到 JEditorPane?- 堆栈溢出

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);
  }
}
于 2012-11-05T11:48:56.857 回答