簡體   English   中英

JLabel擴展但不包裝文字

[英]JLabel expanding but not word-wrapping

我有一個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);
  }

}

您可以嘗試在開源JIDE Common Layer中給StyledLabel。 它支持您希望JLabel可以執行的許多其他功能中的換行。

標簽需要有最大寬度才能包裝。 嘗試設置最大寬度或將文本包裝在帶有width屬性的div標簽中。

BorderLayoutpanelErrMsg一起panelErrMsg ,並將標簽放在NORTH位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM