本文介绍了如何在SWT StyledText中显示提示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SWT 文本具有一个名为 setMessage()的方法,可用于将消息显示为提示该用户指示该字段的目的。

SWT Text has a method called setMessage() which can be used to display a message as a hint for the user, indicating the purpose of the field.

似乎 StyledText 没有 setMessage()方法。可以使用 StyledText 做类似的事情吗?

It seems that StyledText does not have a setMessage() method. Can something similar be done with StyledText nonetheless?

推荐答案

最近,我需要在StyledText中显示一个提示。所以我在这里分享我的解决方案

Recently, I had the need to display a hint within a StyledText. Thus I share my solution here.

显示 StyledTextMessage setMessage()以下可用于在 StyledText 小部件上显示提示。该类添加一个绘画消息文本和几个监听器的绘图器,并显示过滤器,以便在无效时重新绘制消息。

The setMessage() method of the StyledTextMessage shown below can be used to display a hint on a StyledText widget. The class adds a paint listener to draw the message text and several listeners and display filters to redraw the message whenever it may be invalidated.

public class StyledTextMessage {

  public static void main( String[] args ) {
    Display display = new Display();
    Shell shell = new Shell( display );
    shell.setLayout( new FillLayout() );
    StyledText styledText2 = new StyledText( shell, SWT.BORDER );
    Button button = new Button( shell, SWT.PUSH );
    button.setText( "Push Button" );
    new StyledTextMessage( styledText2 ).setMessage( "Please enter something stylish..." );
    shell.setSize( 600, 600 );
    shell.open();
    while( !shell.isDisposed() ) {
      if( !display.readAndDispatch() )
        display.sleep();
    }
    display.dispose();
  }

  private static final int MARGIN = 2;
  private static int[] INVALIDATE_EVENTS = { SWT.Activate, SWT.Deactivate, SWT.Show, SWT.Hide };

  private final StyledText styledText;
  private final Listener invalidateListener;
  private final Color textColor;
  private String message;
  private int verticalOffsetLines;

  public StyledTextMessage( StyledText styledText ) {
    this.styledText = Objects.requireNonNull( styledText );
    this.invalidateListener = this::handleInvalidatedEvent;
    this.textColor = getTextColor();
    this.message = "";
    initialize();
  }

  public void setMessage( String message ) {
    this.message = Objects.requireNonNull( message );
    this.styledText.redraw();
  }

  public boolean isMessageShowing() {
    return !message.isEmpty() && styledText.getContent().getCharCount() == 0;
  }

  private void initialize() {
    styledText.addListener( SWT.Paint, this::handlePaintEvent );
    styledText.addListener( SWT.Resize, event -> styledText.redraw() );
    styledText.addListener( SWT.Dispose, this::handleDispose );
    for( int eventType : INVALIDATE_EVENTS ) {
      styledText.getDisplay().addFilter( eventType, invalidateListener );
    }
  }

  private Color getTextColor() {
    return styledText.getDisplay().getSystemColor( SWT.COLOR_GRAY );
  }

  private void handlePaintEvent( Event event ) {
    if( isMessageShowing() ) {
      drawHint( event.gc, event.x, event.y );
    }
  }

  private void handleDispose( Event event ) {
    for( int eventType : INVALIDATE_EVENTS ) {
      styledText.getDisplay().removeFilter( eventType, invalidateListener );
    }
  }

  private void handleInvalidatedEvent( Event event ) {
    styledText.redraw();
  }

  private void drawHint( GC gc, int x, int y ) {
    int verticalOffset = verticalOffsetLines * gc.getFontMetrics().getHeight();
    gc.setForeground( textColor );
    gc.drawText( message, x + MARGIN, y + MARGIN - verticalOffset, SWT.DRAW_DELIMITER | SWT.DRAW_TRANSPARENT );
  }

}

这篇关于如何在SWT StyledText中显示提示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:24