所以我有这段代码,它打开一个 JOptionPane
,其中包含有关异常的信息:
String fileName=e.getStackTrace()[0].getFileName();
String methodName=e.getStackTrace()[0].getMethodName();
String line=String.valueOf(e.getStackTrace()[0].getLineNumber());
JOptionPane.showMessageDialog(null, new JLabel("There was an unexpected "+e+" at file: "+fileName+" in method: "+methodName+" in line: "+line));
我唯一不满意的是它的颜色都是相同的,所以我想为什么不将异常(exception)情况的字符串颜色更改为红色。所以我用谷歌搜索了一下,发现了几个类似的问题,并且还尝试了他们使用 html 标签等建议的内容,但我没有成功。有什么可行的方法来给文本着色吗?目前看起来像this带有 html 标签
最佳答案
整个字符串需要包含在 HTML 标记内。 For more information, see How to Use HTML in Swing Components.
关于java - 是否可以更改 JOptionPane.showMessageDialog() 中的字符串颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019924/