如何将JLabel添加到JEditorPane? [英] How to add JLabel to JEditorPane?

查看:140
本文介绍了如何将JLabel添加到JEditorPane?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Swing中扩展StyledEditorKit以便能够在编辑器中包含JLabel.我能够做到这一点,这就是我到目前为止所取得的成就.在下图中,突出显示的文本 button 是JLabel类型,而其余文本是普通文本.

I am trying to extend the StyledEditorKit in Swing to be able to include a JLabel inside the editor. 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.

如您所见,标签呈现的颜色比普通文本略低.如何将其顶部与其余文本的顶部对齐?

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:

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;
       }
}

推荐答案

尝试调整Component.getAlignmentY,以控制Component.getAlignmentY相对于文本基线的定位,如ComponentView .

Try adjusting Component.getAlignmentY that controls the positioning of component relative to the text baseline as suggested in ComponentView.

您还可以尝试使用JTextPane,它为嵌入式组件提供更容易的支持.可以使用 insertComponent()添加组件方法.这是一个示例,它还演示了setAlignmentY:

You could also try using JTextPane that provides easier support for embedded components. Components can be added using insertComponent() method. Here is an example, it also demos 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();
            }
        });
    }
}

这篇关于如何将JLabel添加到JEditorPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆