简体   繁体   English

如何将JLabel添加到JEditorPane?

[英]How to add JLabel to JEditorPane?

I am trying to extend the StyledEditorKit in Swing to be able to include a JLabel inside the editor. 我正在尝试在Swing中扩展StyledEditorKit,以便能够在编辑器中包含JLabel。 I was able to do that and this is what I got so far. 我能够做到这一点,这就是我到目前为止所取得的成就。 In the image below, the highlighted text button is of type JLabel whereas the rest of the text is normal text. 在下图中,突出显示的文本按钮的类型为JLabel,而其余文本为普通文本。

在此处输入图片说明

As you can see the label renders a little below than the normal text. 如您所见,标签呈现的颜色比普通文本略低。 How do I align its top with top of the remaining text? 如何将其顶部与其余文本的顶部对齐?

Here is the code for the view that is used to create this label element: 这是用于创建此label元素的视图的代码:

class ComponentView(Element elem) {
      @Override
      protected Component createComponent() {
        JLabel lbl = new JLabel("");
        lbl.setOpaque(true);
        lbl.setBackground(Color.red);
        try {
               int start = getElement().getStartOffset();
               int end = getElement().getEndOffset();
               String text = getElement().getDocument().getText(start, end - start);
               lbl.setText(text);
         } catch (BadLocationException e) {}
         return lbl;
       }
}

Try adjusting Component.getAlignmentY that controls the positioning of component relative to the text baseline as suggested in ComponentView . 尝试调整Component.getAlignmentY控制相对于文本基线部件的定位如在建议的ComponentView

You could also try using JTextPane that provides easier support for embedded components. 您也可以尝试使用JTextPane ,它为嵌入式组件提供了更轻松的支持。 Components can be added using insertComponent() method. 可以使用insertComponent()方法添加组件。 Here is an example, it also demos setAlignmentY : 这是一个示例,它还演示了setAlignmentY

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class TextPaneDemo {
    private static void createAndShowGUI() {
        final JTextPane pane = new JTextPane();
        pane.setText("Some text");

        JButton buttonButton = new JButton("Insert label");
        buttonButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JLabel label = new JLabel("label");
                label.setAlignmentY(0.85f);
                pane.insertComponent(label);
            }
        });

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(buttonButton, BorderLayout.SOUTH);
        panel.add(pane, BorderLayout.CENTER);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setSize(400, 200);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM