Java 为固定宽度的长句调整对话框消息的大小 (JOptionPane)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22072975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resize dialog message (JOptionPane) for long sentence with fixed width
提问by swordartist
I'm trying to resize the height of the dialog box (JOptionPane
) for long sentence with hyperlink.
我正在尝试JOptionPane
使用超链接调整长句对话框 ( )的高度。
My code is ..
我的代码是..
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);
}}
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.
如果我不设置首选大小,该对话框会非常长,而且不可读。所以,我想将宽度固定为 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.
如果我运行此代码,我会看到垂直滚动条。但我不想显示滚动条并调整对话框的高度。
采纳答案by Sharcoux
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() :
为了固定宽度和调整高度,我个人使用这个技巧:你用 setSize 固定任意高度和目标宽度,然后用 getPreferredSize() 获得预期高度:
jtp.setSize(new Dimension(480, 10));
jtp.setPreferredSize(new Dimension(480, jtp.getPreferredSize().height));
The full code would be :
完整的代码是:
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);
}}
回答by Abhishek K
To resize your JOptionPane please use the overridden class displayed in below URL:-
要调整 JOptionPane 的大小,请使用以下 URL 中显示的重写类:-