首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >styledtext的SWT问题

styledtext的SWT问题
EN

Stack Overflow用户
提问于 2014-01-12 03:44:07
回答 1查看 227关注 0票数 0

我正在使用SWT创建一个应用程序的菜单。

在类"B“中,方法createContents()负责创建表单的内容:

代码语言:javascript
复制
protected void createContents() {
    shell = new Shell();
    shell.setSize(500, 315);
    shell.setText("SWT Application");

    Label lblNewLabel = new Label(shell, SWT.NONE);
    lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 14, SWT.NORMAL));
    lblNewLabel.setBounds(188, 0, 108, 25);

    Button btnLogs = new Button(shell, SWT.NONE);
    btnLogs.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Window3 window = new Window3();
            window.open();
        }
    });
    btnLogs.setBounds(386, 242, 75, 25);

    StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
    styledText.setBounds(10, 64, 357, 203);

    Label lblRealTimeLogs = new Label(shell, SWT.NONE);
    lblRealTimeLogs.setBounds(10, 35, 108, 15);

    Button btnFilters = new Button(shell, SWT.NONE);
    btnFilters.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
                Window2 window = new Window2();
                window.open();

        }
    });
    btnFilters.setBounds(386, 204, 75, 25);
    btnFilters.setText("FILTRES");

    Label lblConfiguration = new Label(shell, SWT.NONE);
    lblConfiguration.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
    lblConfiguration.setText("Configuraci\u00F3");
    lblConfiguration.setBounds(386, 128, 88, 15);

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Window4 window = new Window4();
            window.open();
        }
    });
    btnNewButton.setBounds(386, 165, 75, 25);


}   

然后我执行应用程序,显示"B“类中的表单。问题是我需要向来自另一个类的styledtext添加新的文本行。我需要做的是,在类"A“中,它检查文件的内容,如果内容发生了更改,它会在createContents()方法内的类"B”中创建的样式文本中添加新的行。这看起来很简单,但我无法引用类"B“中创建的样式文本从类"A”中添加这些新行……

对不起,如果它不是很清楚,我的英语不是很好…

任何帮助都将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-01-12 18:02:26

好了,我已经整理了一个简单的例子,应该可以向你展示如何做到这一点。它基本上创建了两个类,AB,其中A包含一个从B修改而来的Text

代码语言:javascript
复制
public static void main(final String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    /* Create your gui class */
    A.getInstance(shell);

    shell.pack();
    shell.open();

    /* Wait one second, then simulate generation of other class that calls gui class */
    Display.getDefault().timerExec(1000, new Runnable()
    {
        @Override
        public void run()
        {
            new B();
        }
    });

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

private static class A
{
    private static A    INSTANCE;

    /* This is the field of A we want to modify */
    private Text        text;

    public static A getInstance(Shell shell)
    {
        if (INSTANCE == null && shell != null)
            INSTANCE = new A(shell);

        return INSTANCE;
    }

    private A(Shell shell)
    {
        text = new Text(shell, SWT.BORDER);
    }

    /* This is the method that allows editing the field of A from the outside */
    public void setText(String text)
    {
        if (this.text != null)
            this.text.setText(text);
    }
}

private static class B
{
    public B()
    {
        /* Set the text within A */
        A.getInstance(null).setText("Hello");
    }
}

或者,您可以让Text static忘记实例内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21066989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档