【问题标题】:Open a SWT modal on button click inside another SWT Framework in Java在 Java 中的另一个 SWT 框架内单击按钮打开 SWT 模式
【发布时间】:2020-04-22 22:56:09
【问题描述】:

我编写了以下框架类。我想打开某种模式,它将在按钮单击时保存组件(即按钮、文本框等)。我尝试了一个 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();
    }
}

【问题讨论】:

  • 不要将 SWT 与 Swing/SWT 混合使用,这会导致无穷无尽的问题。

标签: java eclipse swt


【解决方案1】:

当按下按钮时,您的 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 2022-01-27
    • 1970-01-01
    • 2015-05-30
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多