2012-09-08 2 views
2

스윙에서 HTML을 사용하여 레이블의 단어 스타일을 지정할 수 있습니다. 예를 들어 레이블의 특정 단어가 굵게 표시되거나 다른 색상으로 표시되도록하려면 HTML로 표시 할 수 있습니다.SWT 라벨에서 특정 단어의 색상이 바뀌 었습니까?

SWT에 해당하는 기능이 있습니까? 라벨의 텍스트로 "빠른 여우가 게으른 개를 뛰어 넘었다"는 말을하고 "여우"의 색을 갈색으로 변경하고 싶다면 어떻게하면 좋을까요?

+0

: StyledText

public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Label label = new Label(shell, SWT.NONE); label.setText("Blue and not blue"); Color blue = display.getSystemColor(SWT.COLOR_BLUE); final TextLayout layout = new TextLayout(display); layout.setText("Blue and not blue"); final TextStyle style = new TextStyle(display.getSystemFont(), blue, null); label.addListener(SWT.Paint, new Listener() { @Override public void handleEvent(Event event) { layout.setStyle(style, 0, 3); layout.draw(event.gc, event.x, event.y); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } 

그것은 다음과 같이 보일 것입니다 .eclipse.org/helios/index.jsp? topic = % 2Forg.eclipse.platform.doc.isv % 2Freference % 2Fapi % 2Forg % 2Feclipse % 2Fswt % 2Fcustom % 2FStyledText.html)? –

답변

7

정말로 Label이 필요한 경우 아래 코드를 사용할 수 있습니다. 그렇지 않으면 내가 제안 (코멘트에서 언급 한 바와 같이)를 StyledText : // 도움 : 무엇 [`StyledText`] (HTTP 사용에 대한

public static void main(String[] args) { 
    final Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 

    StyledText text = new StyledText(shell, SWT.NONE); 
    text.setEditable(false); 
    text.setEnabled(false); 
    text.setText("Blue and not blue"); 

    Color blue = display.getSystemColor(SWT.COLOR_BLUE); 

    StyleRange range = new StyleRange(0, 4, blue, null); 

    text.setStyleRange(range); 

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

스타일이 지정된 텍스트를 통해 어떻게하면됩니까? –

+0

@ClickUpvote 수정 된 답변입니다. 봐. – Baz

+0

감사합니다. StyledText 당 설정할 수있는 스타일 수의 제한이 있습니까? 아니면 필요한만큼 추가 할 수 있습니까? –

관련 문제