gpt4 book ai didi

java - JOptionPane 中的文本换行?

转载 作者:IT老高 更新时间:2023-10-28 21:07:02 29 4
gpt4 key购买 nike

我正在使用以下代码在我的 Swing 应用程序中显示错误消息

try {
...
} catch (Exception exp) {
JOptionPane.showMessageDialog(this, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}

错误对话框的宽度会变长,具体取决于消息。有什么方法可以包装错误信息吗?

最佳答案

默认情况下,JOptionPane 将使用 JLabel 来显示文本。标签将格式化 HTML。在 CSS 中设置最大宽度。

JOptionPane.showMessageDialog(
this,
"<html><body><p style='width: 200px;'>"+exp.getMessage()+"</p></body></html>",
"Error",
JOptionPane.ERROR_MESSAGE);

更一般地,参见 How to Use HTML in Swing Components ,以及这个简单的example of using HTML in JLabel .

关于java - JOptionPane 中的文本换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14011492/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com