【发布时间】: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 混合使用,这会导致无穷无尽的问题。