JOptionPane中的文本换行? [英] Text wrap in JOptionPane?
本文介绍了JOptionPane中的文本换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
try {
...
} catch(Exception exp){
JOptionPane.showMessageDialog(this,exp.getMessage(),Error,JOptionPane.ERROR_MESSAGE);
}
根据消息,错误对话框的宽度会变长。有没有办法包装错误信息?
解决方案
A JOptionPane
将使用 JLabel
默认显示文本。标签将格式化HTML。设置CSS中的最大宽度。
JOptionPane.showMessageDialog(
this,
< html> < body>< p style ='width:200px;'>+ exp.getMessage()+< / p>< / body>< / html>,
,
JOptionPane.ERROR_MESSAGE);
更一般来说,请参阅如何在Swing组件中使用HTML ,以及这个简单的在 JLabel
中使用HTML的示例。
I'm using following code to display error message in my swing application
try {
...
} catch (Exception exp) {
JOptionPane.showMessageDialog(this, exp.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
The width of the error dialog goes lengthy depending on the message. Is there any way to wrap the error message?
解决方案
A JOptionPane
will use a JLabel
to display text by default. A label will format HTML. Set the maximum width in CSS.
JOptionPane.showMessageDialog(
this,
"<html><body><p style='width: 200px;'>"+exp.getMessage()+"</p></body></html>",
"Error",
JOptionPane.ERROR_MESSAGE);
More generally, see How to Use HTML in Swing Components, as well as this simple example of using HTML in JLabel
.
这篇关于JOptionPane中的文本换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文