有没有办法在SWT中选择标签?

Bab*_*ber 7 java user-interface swt

有没有办法在SWT中选择标签?或者SWT中是否有其他可选择的小部件可用作标签?我正在建立一个日历.

Cha*_*G K 7

我想你要选择Label要剪贴板的文本.

您可以使用TextWidget作为标签,但是当用户点击它时会出现插入符号.如果您不想要插入符号,则使用StyledText并将插入符号设置为null.

例:

package testplugin;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTHelloWorld {

public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Label labelWidget = new Label(shell, SWT.NONE);
    labelWidget.setText("Hello");

    Text textWidget = new Text(shell, SWT.NONE);
    textWidget.setText("Hello");

    //Set background color same as its container
    textWidget.setBackground(shell.getBackground());
    //Make editable false
    textWidget.setEditable(false);

    StyledText styledTextWidget = new StyledText(shell, SWT.NONE);
    styledTextWidget.setText("Hello");  
    styledTextWidget.setBackground(shell.getBackground());
    styledTextWidget.setEditable(false);
    //Set caret null this will hide caret
    styledTextWidget.setCaret(null);

    shell.pack();
    shell.open ();
    shell.setSize(200, 300);
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}
}
Run Code Online (Sandbox Code Playgroud)

您可以在各种小部件上引用这些链接来监听鼠标,键盘,焦点等等.