2013-10-18 36 views
0

我有一個JLabel比寬度尺寸更大的文本,所以文字顯示是這樣的:JLabel的文本完整

enter image description here

我想是該文本表現爲板缺這樣的:

elll

我不能改變德標籤的sizeWidth是necesary,我把它 這個尺寸線:

Cuadros[5].setText("Auto Incremet"); 
Cuadros[5].setSize(ImgA(Cuadros[5]), ImgL(Cuadros[5])); 

感謝您的建議, 我希望能understan我的想法:d

+0

讓[佈局](http://docs.oracle。 com/javase/tutorial/uiswing/layout/visual.html)爲你做這個。 – trashgod

回答

0

嘗試以下,

final JLabel label = ... 
... 
label.setText("prototype text to define size"); 
final Dimension size = label.getPreferredSize(); 
label.setMinimumSize(size); 
label.setPreferredSize(size); 
... 
label.setText(...); 
+0

我相信OP表示他不**要重新調整標籤的大小。他似乎在尋找一種處理文本截斷的自定義方式。 –

+0

嗯我試着這些代碼,但它不工作仍... – WearFox

0

這篇文章是不是封閉的,所以我希望張貼的答案是好的。無論如何,我相信一個解決方案可以將您的JLabel嵌套在它自己的JPanel中,只要它的佈局不需要標籤適合所有側面的硬邊界即可,如BorderLayoutGridLayout所示。

這是例如在問題:

enter image description here enter image description here

這裏是SSCCE演示例如:

import javax.swing.*; 
import java.awt.*; 

public class TruncationTest1 
{ 
    public static void main(String[] args) 
    { 
     JFrame f = new JFrame("Truncation Test"); 
     JPanel panel = new JPanel(); //Default layout, AKA FlowLayout 
     JLabel label = new JLabel("Try resizing the frame: This will not be truncated for some reason."); 
     JLabel label2 = new JLabel("However, this JLabel, on the other hand, will become truncated."); 
     f.setSize((int)label.getPreferredSize().getWidth() + 40,65); 
     f.setLayout(new GridLayout(2,1)); 
     f.setLocationRelativeTo(null); 
     f.setBackground(Color.BLACK); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(panel); 
      panel.setBackground(Color.WHITE); 
      panel.add(label); 
     f.add(label2); 
      label2.setHorizontalAlignment(JLabel.CENTER); 
      label2.setForeground(Color.WHITE); 
     f.setVisible(true); 
    } 
}