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