JavaSWT如何在UI和代码之间正确传递输入和输出?

JavaSWT如何在UI和代码之间正确传递输入和输出?,java,user-interface,swt,Java,User Interface,Swt,我第一次尝试在UI和代码之间传递信息。我已经发布了我到目前为止所做的事情,但是我很难找到正确的方法来传递信息,而不附加当前文本 我知道这是初学者的东西,也希望能够指导专门用于在UI和代码之间来回传递信息的教程 public class UserInterface { private Shell shell; public static void main(){ Display display = new Display(); new UserInt

我第一次尝试在UI和代码之间传递信息。我已经发布了我到目前为止所做的事情,但是我很难找到正确的方法来传递信息,而不附加当前文本

我知道这是初学者的东西,也希望能够指导专门用于在UI和代码之间来回传递信息的教程

public class UserInterface {
    private Shell shell;
    public static void main(){
        Display display = new Display();
        new UserInterface(display);
        display.dispose();      
    }

    public UserInterface(Display display){
        shell = new Shell(display);
        shell.setSize(400, 250);
        initUI();
        center(shell);
        shell.open();
        while(!shell.isDisposed()){
            if(!display.readAndDispatch()){
                display.sleep();
            }
        }
    }

    public void initUI(){
        final Text text = new Text(shell, SWT.SINGLE);
        text.setBounds(20, 20, 100, 18);

        Label lbl1 = new Label(shell, SWT.BEGINNING);
        lbl1.setBounds(20, 50, 500, 50);
        lbl1.setText("(1) Enter number.\n(2) Click next to enter another number\n(3) Click done to find LCM");

        final Text display = new Text(shell, SWT.SINGLE);
        display.setBounds(20, 110, 360, 100);

        Button button = new Button(shell, SWT.PUSH);
        button.setBounds(140, 16, 100, 35);
        button.setText("Button");

        button.addSelectionListener(new SelectionAdapter(){
            @Override
            public void widgetSelected(SelectionEvent e){
            //display.setText("");
            String output = text.getText();
            display.setText(output);
            }
        });
    }

    public void center(Shell shell){
        Rectangle bds = shell.getMonitor().getBounds();
        Point p = shell.getSize();
        int xPos = (bds.width - p.x) / 2;
        int yPos = (bds.height - p.y) / 2;
        shell.setBounds(xPos, yPos, p.x, p.y);      
    }
}

这段代码应该会给你一个想法。它有两个
文本
小部件。其中一个将充当控制台(追加文本),另一个将显示您添加到控制台的最后一个文本。只需单击按钮添加文本:

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

    final Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
    text.setEditable(false);
    final Text lastAppend = new Text(shell, SWT.BORDER);
    lastAppend.setEditable(false);

    Button button = new Button(shell,SWT.PUSH);
    button.setText("Add new line");

    button.addListener(SWT.Selection, new Listener()
    {
        private int counter = 0;
        @Override
        public void handleEvent(Event arg0)
        {
            String append = "TEST " + counter++ + "\n";
            text.append(append);
            lastAppend.setText(append);
        }
    });

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

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
看起来像这样:


如果你不想追加,那么不要使用
Text\append(String)
而是
Text\setText(String)
@Baz这就是你在思考之前发表文章时发生的情况。谢谢,巴兹。介意我给你一个答案吗?或者,可以随意删除您的问题。@Baz奇怪的是,使用setText(字符串)仍然“附加”文本框中原来的内容。我我已经通过使用display.setText(“”)解决了这个问题。但是从逻辑上讲,为什么我重新分配setText时,它会添加到已经存在的内容上,而不是完全替换它呢?我只是在这里测试了它,它没有附加。你能用你现在拥有的更新你的代码片段吗?