java - 在 Java 中的另一个 SWT 框架内单击按钮打开 SWT 模式

标签 java eclipse swt

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

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

Exception in thread "main" org.eclipse.swt.SWTException: Invalid thread access

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

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();
    }
}

最佳答案

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

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

    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

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

关于java - 在 Java 中的另一个 SWT 框架内单击按钮打开 SWT 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59594463/

相关文章:

java - 在线程上调用 interrupt() 是否会创建与被中断线程的 happens-before 关系

java - 使用 JPA EclipseLink 进行无连接远程 MySQL 连接

java.lang.ClassNotFoundException 用于 tomcat 中的 servlet 与 eclipse

Eclipse,将鼠标悬停在关键字上时更改弹出文本背景颜色

java - 拖放 SWT 容器组合

java - 如何从Java代码中找到真实的显示密度(DPI)?

java - 在Android中按距离和角度移动物体多远

eclipse - 无法从Eclipse Oxygen 4.7.0 Build 20170620-1800中的2.0.2.v20170420-0909版更新到Buildship 2.1.2v20170807-1324

java - 在自定义 Eclipse View 中绘制

java - 你怎么能在这里得到 "SWTException: Invalid thread access"?