java - 在 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();
}
}
解决方案
当按下按钮时,您的 SWT 代码正在创建一个新代码Display
- 您不应该这样做。Display
SWT 应用程序应该只对整个应用程序使用单个对象。
所以你的选择监听器应该是这样的:
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。
推荐阅读
- python - 在对 YouTube 频道进行网页抓取期间,循环未完成,无法提取频道中所有视频的数据
- python - 如何在 DRF 中使用 api 在 python 中进行身份验证
- python - 与断言在同一行上的 Pytest 条件语句不起作用
- reactjs - 何时将回调传递给 useState 设置器实际上会有所作为?
- angular - 离子:在模板中的对象上运行循环的生产构建失败
- flutter - 元素类型 Color 不能分配给列表类型 Color
- javascript - firebase:emulators:Start :术语“firebase:emulators:Start”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称
- html - 无法删除我网站上的多余空白
- python - `df.corr()` 中出现的 NaN 值
- typescript - 如何解构打字稿中的类型?