如何将JTextPane的大小固定为其内容? [英] How to fix size of JTextPane to its content?

查看:83
本文介绍了如何将JTextPane的大小固定为其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JPanelBoxLayout.

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS){
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public Dimension minimumLayoutSize(Container target){
            return new Dimension(100, 25);
        }
        @Override
        public Dimension maximumLayoutSize(Container target){
            return new Dimension(100,25);
        }

});

我动态添加了一些JTextPaneJPanel. 我调用下面的方法,将新的JTextPane添加到我的jpanel中,并将文本传递给它.

I add some JTextPane and JPanel to it dynamically. I call the method below to add new JTextPane to my jpanel and pass the text to it.

public void display(final String text){
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    final Style style = sc.addStyle("MainStyle", defaultStyle);
    JTextPane pane = new JTextPane(){
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override // how should I write this:
        public Dimension getMinimumSize(){
            return new Dimension(text.length()*5, getContentHeight(text.length()*5,text));
        }
        @Override // how should I write this:
        public Dimension  getMaximumSize(){
            return  new Dimension(430, getContentHeight(text.length()*5,text));
        }
    };
    receive.setStyledDocument(doc);
    receive.setEditable(false);
    // how should I write this: 
    receive.setPreferredSize(new Dimension(text.length()*5, getContentHeight(text.length()*5,text)));

    try {
        doc.insertString(doc.getLength(),text,style);
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    panel.add(pane);

}

因此内容不会更改.

public int getContentHeight(int i, String content) {
        JEditorPane dummyEditorPane=new JEditorPane();
        dummyEditorPane.setSize(i,Short.MAX_VALUE);
        dummyEditorPane.setText(content);
        return dummyEditorPane.getPreferredSize().height;
}

问题出在getMinimumSize()getMaximumSize()setPreferredSize()方法中! 也是我为dummyEditorPane.setSize(i,Short.MAX_VALUE);设置的宽度 那么如何设置此方法来固定文本窗格的大小?

The issue is in getMinimumSize() and getMaximumSize() and setPreferredSize() method! Also in width that I set for dummyEditorPane.setSize(i,Short.MAX_VALUE); So How can I set this method to fix size for textpane?

推荐答案

getPreferredSize()已返回内容的大小.因此,解决方案是不使用setPreferredSize,而是使用

getPreferredSize() already returns the size of the content. So the solution is just to not use setPreferredSize, but use

jtp.setSize(jtp.getWidth(),jtp.getPreferredSize().height);//For fixed width

jtp.setSize(jtp.getPreferredSize());//for unlimited width and no wrapping

这将设置JTextPane的高度以匹配其内容.

This will set the height of the JTextPane to match its content.

要使用BoxLayout进行此操作,您可能想要

To do this with a BoxLayout, you might want to

  • 扩展JTextPane并覆盖getMinimumSizegetMaximumSize以使它们返回getPreferredSize:

        final JTextPane myJTP = new JTextPane() {

            @Override
            public Dimension getMinimumSize() {
                return getPreferredSize();
            }

            @Override
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }

        };

  • 或扩展BoxLayout以使layoutContainer方法为其子级设置正确的大小.

  • or extends BoxLayout to make the layoutContainer method set the correct size to its children.

    这篇关于如何将JTextPane的大小固定为其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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