我正在使用以下代码在我的 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/61445061/