我有一个用于显示错误消息的 JLabel 对象,如以下代码所示。据我了解,如果消息嵌入在 html 标签中,则标签应该是自动换行。但是,在我的情况下,标签似乎水平扩展。有人可以告诉我我做错了什么吗?或者,有没有更好的方法来显示长错误消息?
这是代码:
public class MyPanel extends JPanel {
public MyPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// Several JPanel objects inside
// The last JPanel to show error messages
JPanel panelErrMsg = new JPanel(new FlowLayout(FlowLayout.LEFT));
this._lblError = new JLabel();
this._lblError.setBorder(BorderFactory.createEmptyBorder(20, 10, 10, 10));
this._lblError.setForeground(Color.RED);
this._lblError.setFont(new Font("Courier New", Font.BOLD, 12));
panelErrMsg.add(this._lblError);
this.add(panelErrMsg);
}
private void DisplayMessage(String msg) {
String newMessage = "<html><body>" + msg + "</body></html>";
this._lblError.setText(newMessage);
}
}