调整具有固定宽度的长句的对话框消息(JOptionPane) [英] Resize dialog message (JOptionPane) for long sentence with fixed width

查看:117
本文介绍了调整具有固定宽度的长句的对话框消息(JOptionPane)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用超链接调整长句的对话框高度( JOptionPane )。

I'm trying to resize the height of the dialog box (JOptionPane) for long sentence with hyperlink.

我的代码是..

public class DialogTest {
public static void main(String[] args) throws Exception {
    JTextPane jtp = new JTextPane();
    Document doc = jtp.getDocument();
    for (int i = 0; i < 50; i++) {
        doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
        if ((3 == i) || (7 == i) || (15 == i)) {
            doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
            SimpleAttributeSet attrs = new SimpleAttributeSet();
            StyleConstants.setUnderline(attrs, true);
            StyleConstants.setForeground(attrs, Color.BLUE);
            String text = "www.google.com";
            URL url = new URL("http://" + text);
            attrs.addAttribute(HTML.Attribute.HREF, url.toString());
            doc.insertString(doc.getLength(), text, attrs);
        }
    }
    JScrollPane jsp = new JScrollPane(jtp);
    jsp.setPreferredSize(new Dimension(480, 150));
    jsp.setBorder(null);

    JOptionPane.showMessageDialog(null, jsp, "Title", JOptionPane.INFORMATION_MESSAGE);
}}

如果我没有设置首选大小,那么该对话框将是真的很长,而且它不可读。所以,我想将宽度修正为480。

If I don't set the preferred size, that dialog is gonna be really long, and it's not readable. So, I want to fix the width to 480.

而且,我想调整高度取决于文本的长度。

And, I want to adjust height depends on the length of the text.

如果我运行此代码,我会看到垂直滚动条。但我不想显示滚动条并调整对话框的高度。

If I run this code, I see the vertical scrollbar. But I don't want to show that scrollbar and adjust the height of the dialog.

推荐答案

修复宽度并调整高度,我个人使用这个技巧:
你使用setSize修复任意高度和目标宽度,然后使用getPreferredSize()获得预期高度:

To fix the width and adapt the height, I personally use this trick : You fix an arbitrary height and the targeted width with setSize, and then get the expected height with getPreferredSize() :

jtp.setSize(new Dimension(480, 10));
jtp.setPreferredSize(new Dimension(480, jtp.getPreferredSize().height));

完整代码为:

public class DialogTest {
public static void main(String[] args) throws Exception {
    JTextPane jtp = new JTextPane();
    Document doc = jtp.getDocument();
    for (int i = 0; i < 50; i++) {
        doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
        if ((3 == i) || (7 == i) || (15 == i)) {
            doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
            SimpleAttributeSet attrs = new SimpleAttributeSet();
            StyleConstants.setUnderline(attrs, true);
            StyleConstants.setForeground(attrs, Color.BLUE);
            String text = "www.google.com";
            URL url = new URL("http://" + text);
            attrs.addAttribute(HTML.Attribute.HREF, url.toString());
            doc.insertString(doc.getLength(), text, attrs);
        }
    }
    //JScrollPane jsp = new JScrollPane(jtp);
    //jsp.setPreferredSize(new Dimension(480, 150));
    //jsp.setBorder(null);
    jtp.setSize(new Dimension(480, 10));
    jtp.setPreferredSize(new Dimension(480, jtp.getPreferredSize().height));

    //JOptionPane.showMessageDialog(null, jsp, "Title", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null, jtp, "Title", JOptionPane.INFORMATION_MESSAGE);
}}

这篇关于调整具有固定宽度的长句的对话框消息(JOptionPane)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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