JOptionPane.showMessageDialog截断JTextArea消息 [英] JOptionPane.showMessageDialog truncates JTextArea message
问题描述
我的Java GUI应用程序需要快速向最终用户显示一些文本,因此JOptionPane
实用程序方法似乎很合适.而且,文本必须是可选择的(用于复制和粘贴),并且可能会有些长(〜100个单词),因此它必须很好地适合窗口(屏幕外没有文本);理想情况下,所有这些都应该一次显示,以便用户无需交互即可阅读它,因此滚动条是不可取的.
My Java GUI application needs to quickly show some text to the end-user, so the JOptionPane
utility methods seem like a good fit. Moreover, the text must be selectable (for copy-and-paste) and it could be somewhat long (~100 words) so it must fit nicely into the window (no text off screen); ideally it should all be displayed at once so the user can read it without needing to interact, so scrollbars are undesirable.
我认为将文本放入JTextArea
并将其用于JOptionPane.showMessageDialog
中的消息会很容易,但似乎会截断文本!
I thought putting the text into a JTextArea
and using that for the message in JOptionPane.showMessageDialog
would be easy but it appears to truncate the text!
public static void main(String[] args) {
JTextArea textArea = new JTextArea();
textArea.setText(getText()); // A string of ~100 words "Lorem ipsum...\nFin."
textArea.setColumns(50);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JOptionPane.showMessageDialog(null, textArea, "Truncated!", JOptionPane.WARNING_MESSAGE);
}
如何使文本完全适合选项窗格而没有滚动条,并且可以选择复制/粘贴?
How can I get the text to fit entirely into the option pane without scrollbars and selectable for copy/paste?
推荐答案
import java.awt.*;
import javax.swing.*;
public class TextAreaPreferredHeight2
{
public static void main(String[] args)
{
String text = "one two three four five six seven eight nine ten ";
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.append(text);
textArea.append(text);
textArea.append(text);
textArea.append(text);
textArea.append(text);
textArea.setSize(textArea.getPreferredSize().width, 1);
JOptionPane.showMessageDialog(
null, textArea, "Not Truncated!", JOptionPane.WARNING_MESSAGE);
}
}
这篇关于JOptionPane.showMessageDialog截断JTextArea消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!