我已经编写了以下框架类。我想打开某种模式,将在单击按钮时容纳组件(即按钮,文本框等)。我已经尝试过一个JFrame并可以正常工作,但是,我无法获得与父外壳匹配的一致设计。例如,JFrames上的按钮看起来与SWT外壳不同。
单击按钮时出现错误...
线程“ main” 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
-您不应该这样做。 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并提供了更易于使用的对话框。