make a JLabel wrap it's text by setting a max width

后端 未结 9 2580
清酒与你
清酒与你 2020-11-29 08:09

I have a JLabel which has a lot of text on it. Is there a way to make the JLabel have a max width so that it will wrap the text to make it not exceed this width?

Tha

9条回答
  •  独厮守ぢ
    2020-11-29 08:23

    wrapping works but not in all cases. If the parent container uses FlowLayout then it'll not work. Therefore I set it to BoxLayout. Look at this code snippet:

    javax.swing.JPanel pRefundNote = new javax.swing.JPanel(); 
    javax.swing.JLabel lbNote = new javax.swing.JLabel();
    
    pRefundNote.setAlignmentX(0.0F); 
    pRefundNote.setMaximumSize(new java.awt.Dimension(32767, 33)); 
    pRefundNote.setLayout(new javax.swing.BoxLayout(pRefundNote, javax.swing.BoxLayout.X_AXIS)); 
    
    lbNote.setText("Select items using Shift or Ctrl and Up/Down keys or Mouse"); 
    lbNote.setVerticalAlignment(javax.swing.SwingConstants.TOP);
    lbNote.setVerticalTextPosition(javax.swing.SwingConstants.TOP); 
    pRefundNote.add(lbNote);
    

    Don't add
    because it'll break your text even if you enlarge your parent frame and pRefundNote container.

提交回复
热议问题