Java SWT复合-重绘问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1024732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:22:30  来源:igfitidea点击:

SWT composite - redraw problem

javauser-interfaceswt

提问by Amit

I have a composite element, that initially has a Label. Now I call dispose on the it (the label) and create another label in the same container (composite elm), but I don't see the new text. It brings me to question how do I enable redraw on the composite, so that the new label (or any other component I might create) will render in place of the old one. Here is the code I have (separated into a unit test for redraw a composite)

我有一个复合元素,它最初有一个标签。现在我在它(标签)上调用 dispose 并在同一个容器(复合榆树)中创建另一个标签,但我没有看到新文本。它让我质疑如何在复合上启用重绘,以便新标签(或我可能创建的任何其他组件)将代替旧标签呈现。这是我的代码(分离为重绘复合的单元测试)

private Label createLabel( Composite parent) {
    Label label = new Label(parent, SWT.NONE);
    label.setAlignment(SWT.CENTER);
    label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true) );
    return label;
}

private void changeText() {
    assert testCell != null : "Please initialize test cell";
    testCell.getChildren()[0].dispose();
    Label l = createLabel(testCell);
    l.setText("New TexT");
    testCell.redraw();
}

private void draw() {
    Display display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(2,false));

    testCell = new Composite(shell,SWT.BORDER);
    testCell.setLayout(new GridLayout());
    Label l = createLabel(testCell);
    l.setText("Old Text");
    Composite btnCell = new Composite(shell,SWT.NONE);
    btnCell.setLayout(new GridLayout());
    Button b = new Button(btnCell, SWT.PUSH);
    b.setText("Change");
    b.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            changeText();
        }
    });

As you can see, I am calling redraw on the composite after I add a new element. Also, I have verified that after the call to dispose, testCell.getChildren().length returns 0, as expected, and when I create a new label, I get the same expression to return 1, verifying that the new element is indeed getting added to its parent composite container Am I missing something here ?

如您所见,我在添加新元素后对复合调用重绘。另外,我已经验证在调用 dispose 之后,testCell.getChildren().length 返回 0,正如预期的那样,并且当我创建一个新标签时,我得到相同的表达式以返回 1,验证新元素确实得到添加到其父复合容器中 我在这里遗漏了什么吗?

采纳答案by Csaba_H

In the changeText()function, the

changeText()函数中,

testCell.redraw();

testCell.redraw();

line should be replaced by

行应替换为

testCell.layout();

testCell.layout();

Or, if you want to resize it correctly you should use

或者,如果你想正确调整它的大小,你应该使用

shell.layout();.

shell.layout();.

回答by anonymous

I would say add a selectionListener on the label.

我会说在标签上添加一个 selectionListener 。

.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(final SelectionEvent e) {
        //Change text by Label.setText();
    }
}