JOptionPane 中的文本换行? [英] Text wrap in JOptionPane?

查看:30
本文介绍了JOptionPane 中的文本换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

更一般地,请参阅如何在 Swing 组件中使用 HTML,以及这个简单的 JLabel 中使用 HTML 的示例.

More generally, see How to Use HTML in Swing Components, as well as this simple example of using HTML in JLabel.

这篇关于JOptionPane 中的文本换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆