java JOptionPane.showMessageDialog 截断 JTextArea 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4330076/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:54:48  来源:igfitidea点击:

JOptionPane.showMessageDialog truncates JTextArea message

javaswingjtextareajoptionpane

提问by maerics

My Java GUI application needs to quickly show some text to the end-user, so the JOptionPaneutility 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.

我的 Java GUI 应用程序需要快速向最终用户显示一些文本,因此JOptionPane实用方法似乎很合适。此外,文本必须是可选的(用于复制和粘贴)并且它可能有点长(约 100 个字),因此它必须很好地适应窗口(屏幕外没有文本);理想情况下,它应该一次全部显示,以便用户无需交互即可阅读,因此滚动条是不可取的。

I thought putting the text into a JTextAreaand using that for the message in JOptionPane.showMessageDialogwould be easy but it appears to truncate the text!

我认为将文本放入 aJTextArea并将其用于消息中JOptionPane.showMessageDialog会很容易,但它似乎截断了文本!

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

Dialog with truncated text

带有截断文本的对话框

How can I get the text to fit entirely into the option pane without scrollbars and selectable for copy/paste?

如何让文本完全适合选项窗格而没有滚动条并且可选择复制/粘贴?

回答by camickr

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

回答by Yuri Joselson

If you need to display a string of an unknown length, you can set number of rows "on the fly":

如果需要显示未知长度的字符串,可以“即时”设置行数:

public static void showMessageDialogFormatted(String msg, String title, int messageType, int columnWidth) {
    JTextArea textArea = new JTextArea(msg);
    textArea.setColumns(columnWidth);
    textArea.setRows(msg.length() / columnWidth + 1);
    textArea.setLineWrap(true);
    textArea.setEditable(false);
    textArea.setWrapStyleWord(true);
    JOptionPane.showMessageDialog(null, textArea, title, messageType);
}

回答by Joshua McKinnon

You've got the right idea. Just adjust the rows of your textarea.

你的想法是对的。只需调整 textarea 的行。

textArea.setRows(10); // or value that seems acceptable to you...

This seemed to fix the issue for me, using 100 words of lorem ipsum.

这似乎为我解决了这个问题,使用了 100 个单词的 lorem ipsum。

回答by Huey

Try this:

试试这个:

JTextArea textArea = new JTextArea();
textArea.setText(getText());
textArea.setSize(limit, Short.MAX_VALUE); // limit = width in pixels, e.g. 500
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);