2011-09-21 13 views

回答

1

不幸的是,你是通過Windows操作系統的能力的限制。據我所知,這個功能不可用直到Windows Vista。該行爲非常OS特定你可能有這樣的SO鏈接發現:Is it possible to modify how Text swt widget wrap word?

你最好保證是添加修改監聽器上的文本控制,計算使用GC.textExtent() API上的控制寬度,使用計算可以容納的字符數(使用GC.getFontMetrics().getAverageCharWidth()並在適當的位置插入一個換行符,並且如果你選擇這種方法,那麼請記住,你還必須處理resizepaint事件。否則,在調整窗口大小時,邏輯可能會

如果您想在Text小部件中找到內容(如MS Office),那麼您應該查找SWT StyledText widget,例如:

enter image description here

>>Code

從這裏Java2s StyledText服用。

package test; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.StyledText; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class StyledTextIndentAlignmentJustify 
{ 
    public static void main(String[] args) 
    { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 
     StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); 

     String text = "The third paragraph is justified. Like alignment, justify only works " + 
     "when the StyledText is using word wrap. If the paragraph wraps to several lines, " + 
     "the justification is performed on all lines but the last one."; 

     styledText.setText(text); 
     styledText.setLineJustify(0, 1, true); 

     shell.setSize(300, 400); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 
}