首页 > 解决方案 > 在 Java 中的另一个 SWT 框架内单击按钮打开 SWT 模式

问题描述

我编写了以下框架类。我想打开某种模式,它将在按钮单击时保存组件(即按钮、文本框等)。我尝试了一个 JFrame 并且它有效,但是,我无法获得与父 shell 匹配的一致设计。例如,JFrames 上的按钮看起来与 SWT shell 不同。

单击按钮时出现错误...

线程“主”org.eclipse.swt.SWTException 中的异常:线程访问无效

这可以做到吗?如果没有,任何人都可以给我任何其他建议吗?

package framework;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import framework.SWTResourceManager;

public class Frontend {

    /** Declare display, shell and data text objects */
    Display display = new Display();
    Shell shell = new Shell(display, SWT.CLOSE);

    private Button btnOpen;

    /** Initialise frame */
    public Frontend() {
        init();
        shell.pack();
        shell.open();
        shell.setSize(600, 600);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private void init() {
        // Set background of framework.
        shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));

        btnOpen = new Button(shell, SWT.NONE);
        btnOpen.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        btnOpen.setText("Open");
        btnOpen.setBounds(20, 20, 50, 50);

        btnOpen.addSelectionListener(new SelectionAdapter() {       
            @Override
            public void widgetSelected(SelectionEvent e) {      
                Display visual_display = new Display();
                Shell visual_shell = new Shell(visual_display, SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.ON_TOP);
                visual_shell.pack();

                // Set framework size.
                visual_shell.setSize(600,600);
                visual_shell.open();

                while (!visual_shell.isDisposed()) {
                    if (!visual_display.readAndDispatch()) {
                        visual_display.sleep();
                    }
                }
                visual_display.dispose();
            }
        });
    }

    // Run Frame.
    public static void main(String[] args) {
         new Frontend();
    }
}

标签: javaeclipseswt

解决方案


当按下按钮时,您的 SWT 代码正在创建一个新代码Display- 您不应该这样做。DisplaySWT 应用程序应该只对整个应用程序使用单个对象。

所以你的选择监听器应该是这样的:

    btnOpen.addSelectionListener(new SelectionAdapter() {       
        @Override
        public void widgetSelected(SelectionEvent e) {      

            Shell visual_shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.ON_TOP);
            visual_shell.pack();

            // Set framework size.
            visual_shell.setSize(1010,600);
            visual_shell.open();

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

这只是使用display而不是调用Display.dispose.

您可能还想查看添加到 SWT 并提供更易于使用的对话框的 Eclipse JFace。


推荐阅读