Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我怎样才能拥有一个可滚动的禁用文本?_Java_Textbox_Scroll_Swt_Disabled Control - Fatal编程技术网

Java 我怎样才能拥有一个可滚动的禁用文本?

Java 我怎样才能拥有一个可滚动的禁用文本?,java,textbox,scroll,swt,disabled-control,Java,Textbox,Scroll,Swt,Disabled Control,我的文本声明为 Text text= new Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.WRAP); 在某些情况下,它应该被禁用。但当我这么做的时候 text.setEnabled(false);文本的滚动条也被禁用,我无法完全看到文本中的值。 我的文本字段不能是只读的。在某些情况下,它应该是可编辑的。 我知道文本中有setEditable()方法,但我希望具有与禁用文本时相同的行为,即背景颜色更改、光标不闪烁(插入符号)

我的文本声明为

Text text= new Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);
在某些情况下,它应该被禁用。但当我这么做的时候
text.setEnabled(false);文本的滚动条也被禁用,我无法完全看到文本中的值。

我的文本字段不能是只读的。在某些情况下,它应该是可编辑的。

我知道文本中有setEditable()方法,但我希望具有与禁用文本时相同的行为,即背景颜色更改、光标不闪烁(插入符号)、无法单击鼠标和文本不可选择等

我可以通过这样做来改变背景颜色

text.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
但我无法禁用光标、文本选择和鼠标单击


有没有办法使禁用文本的滚动条保持活动状态?

禁用后,您将无法使
文本控件显示滚动条。这只是本机控件的工作方式,即操作系统呈现控件的方式

但是,您可以将
文本
包装在
滚动组合中
。这样,
scrolled组合将滚动,而不是
文本

以下是一个例子:

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

    final ScrolledComposite composite = new ScrolledComposite(shell, SWT.V_SCROLL);
    composite.setLayout(new FillLayout());

    final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.WRAP);

    composite.setContent(text);
    composite.setExpandHorizontal(true);
    composite.setExpandVertical(true);
    composite.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add text and disable");
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            text.setText("lalala\nlalala\nlalala\nlalala\nlalala\nlalala\n");
            text.setEnabled(false);
            composite.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    });

    shell.pack();
    shell.setSize(300, 150);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
这就是它看起来的样子:


禁用后,您将无法使
文本
控件显示滚动条。这只是本机控件的工作方式,即操作系统呈现控件的方式

但是,您可以将
文本
包装在
滚动组合中
。这样,
scrolled组合将滚动,而不是
文本

以下是一个例子:

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

    final ScrolledComposite composite = new ScrolledComposite(shell, SWT.V_SCROLL);
    composite.setLayout(new FillLayout());

    final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.WRAP);

    composite.setContent(text);
    composite.setExpandHorizontal(true);
    composite.setExpandVertical(true);
    composite.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add text and disable");
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            text.setText("lalala\nlalala\nlalala\nlalala\nlalala\nlalala\n");
            text.setEnabled(false);
            composite.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    });

    shell.pack();
    shell.setSize(300, 150);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
这就是它看起来的样子: