A typical SWT sample code looks li开发者_StackOverflowke the following code:
final Display display = Display.getDefault();
final Shell shell = createMyShell(display);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
What is the difference between Display
and Shell
?
You can have multiple shells with a single Display and a single while loop handing event dispatch. Create the display, create the Shell(s) from the Display, and then start the single UI event dispatcher loop. See http://www.chrisnewland.com/av/111/swt-best-practice-single-display-multiple-shells
Yes, if you have to show multiple windows (shells), you must have a loop for each one.
But there is only one Display
object in an application that you need to create.
ChrisWhoCodes is quite correct. The following code implements his model.
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MultipleShells {
public MultipleShells(Display display) {
Shell[] shells;
MainWindows mainWindow;
mainWindow = new MainWindows(display, "Shell 1");
mainWindow = new MainWindows(display, "Shell 2");
while (!display.isDisposed()) {
if (!display.readAndDispatch()) {
shells = display.getShells();
if (shells.length == 0)
break;
display.sleep();
}
}
}
public static void main(String[] args) {
Display display = new Display();
MultipleShells mainApp = new MultipleShells(display);
display.dispose();
}
}
class MainWindows {
protected MainWindows(Display parent, String title) {
Shell mainShell;
mainShell = new Shell(parent);
mainShell.setText(title);
mainShell.open();
}
}
精彩评论