2014-11-03 91 views
0

我有一個標籤,我最初放置一個靜態文本。在我的應用程序過程中,我將通過使用setText方法覆蓋此標籤,文字可能vary.How重新尺寸標籤的大小,而不是在開始時放置一個大小不變的根據SWT中的文本大小重新調整標籤大小

Label fontDisplay; 
fontDisplay = new Label(Composite, SWT.NONE); 
GridData gridData = new GridData(); 
    gridData.widthHint = 12; 
    fontDisplay.setLayoutData(gridData); 
.... 

fontDisplay(someText);//size of some text is greater than 12,how to re -size the label here? 

回答

1

您可以在Label的父母打電話Control#pack()

private static final String TEXT = "abcdefghijklmnopqrstuvwxyz"; 
public static void main(String[] args) 
{ 
    Display display = new Display(); 
    Shell shell = new Shell(); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new GridLayout(2, false)); 

    Button button = new Button(shell, SWT.PUSH); 
    button.setText("Randomize"); 

    final Label label = new Label(shell, SWT.NONE); 
    label.setText("abc"); 

    button.addListener(SWT.Selection, new Listener() 
    { 
     private Random random = new Random(); 

     @Override 
     public void handleEvent(Event arg0) 
     { 
      label.setText(TEXT.substring(0, random.nextInt(TEXT.length()))); 
      label.getParent().pack(); 
     } 
    }); 

    shell.pack(); 
    shell.open(); 

    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
     { 
      display.sleep(); 
     } 
    } 
    display.dispose(); 
} 
0

我發現answert的解決方案。我們只需要調用佈局o f的部件的父母。它的伎倆。

Composite.layout(true);