Java SWT on按钮单击关闭当前外壳打开新外壳
我是Java新手。我正在尝试一个有登录表单的SWT桌面应用程序。当单击OK按钮时,在验证之后,我想在关闭登录shell后打开一个主窗口。请在下面找到相关代码段 在Login.java中Java SWT on按钮单击关闭当前外壳打开新外壳,java,shell,swt,Java,Shell,Swt,我是Java新手。我正在尝试一个有登录表单的SWT桌面应用程序。当单击OK按钮时,在验证之后,我想在关闭登录shell后打开一个主窗口。请在下面找到相关代码段 在Login.java中 btnOK.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent event) { if(LoginAuthentication.isValidLogin(txtU
btnOK.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
if(LoginAuthentication.isValidLogin(txtUname.getText(), txtPwd.getText())){
//shell.dispose();
MainWin win = new MainWin();
}
一切正常,但登录shell挂起在后台,一旦MainWin shell关闭,就可以看到。如果我取消注释包含shell.dispose()的行;在Login.java中,MainWin shell会立即打开和关闭
有谁能在这个问题上帮助我吗?我们将非常感谢您的建议
感谢鲁迪格为解决这个问题所做的努力。打开登录shell的代码如下所示。在LoginWin.java中
public static void main(String[] args){
LoginWin window = new LoginWin();
window.open();
}
此外,在LoginWIn.java中,我有以下内容:
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
shell = new Shell( SWT.CLOSE | SWT.APPLICATION_MODAL);
shell.setSize(450, 300);
shell.setText("tMailer");
shell.setLayout(new GridLayout(2, true));
Label lblHeading = new Label(shell, SWT.SHADOW_OUT);
lblHeading.setText("Login window");
lblHeading.setLayoutData( new GridData(SWT.CENTER, GridData.FILL_VERTICAL, true, false, 2, 1));
Group loginGr = new Group(shell, SWT.NONE);
GridData gd_loginGr = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
1);
loginGr.setLayoutData(gd_loginGr);
loginGr.setText("Login data");
loginGr.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2,
1));
loginGr.setLayout(new GridLayout(4, true));
Label lblUname = new Label(loginGr, SWT.NONE);
lblUname.setText("Username");
lblUname.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true,
false));
Text txtUname = new Text(loginGr, SWT.FLAT);
txtUname.setText("u");
txtUname.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
3,1));
Label lblPwd = new Label(loginGr, SWT.NONE);
lblPwd.setText("Password");
lblPwd.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
Text txtPass = new Text(loginGr, SWT.PASSWORD);
txtPwd.setText("p");
txtPwd.setEchoChar('*');
Label lblCol1 = new Label(loginGr, SWT.NONE); // filler for balnk grid-column
lblCol1.setText("Column-1");
lblCol1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
lblCol1.setVisible(false);
Button btnCancel = new Button(loginGr, SWT.FLAT);
btnCancel.setText("Cancel");
btnCancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
btnCancel.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
shell.dispose();
}
});
Label lblCol2 = new Label(loginGr, SWT.NONE); // filler for gridcolumn 3
lblCol2.setText("Column-3");
lblCol2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
lblCol2.setVisible(false);
Button btnOK = new Button(loginGr, SWT.FLAT);
btnOK.setText("Login");
btnOK.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
shell.setDefaultButton(btnOK);
btnOK.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
if(LoginAuthentication.isValidLogin(txtUname.getText(), txtPass.getText())){
SplashWin sw = new SplashWin();
}
});
shell.pack();
}
public class SplashWin {
private final Shell splash;
private Display display;
private int counter;
public SplashWin()
{
display = Display.getDefault();
splash = new Shell(SWT.ON_TOP | SWT.APPLICATION_MODAL | SWT.NONE);
final Label lbl_img = new Label(splash, SWT.NONE);
final Image img = new Image("test.png");
lbl_img.setImage(img);
splash.open();
display.asyncExec(new Runnable()
{
public void run()
{
for(counter=0; counter<5; counter++
{
try {
Thread.sleep(40);
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
splash.close();
img.dispose();
splash.dispose();
// Create and call open() of an ApplicationWindow object
MainWin mainWin = new MainWin();
mainWin.open();
}
});
while(counter != 5)
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
public static void main(String[] args){
new SplashWin();
}
}
我的SplashWin.java文件包含以下内容:
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
shell = new Shell( SWT.CLOSE | SWT.APPLICATION_MODAL);
shell.setSize(450, 300);
shell.setText("tMailer");
shell.setLayout(new GridLayout(2, true));
Label lblHeading = new Label(shell, SWT.SHADOW_OUT);
lblHeading.setText("Login window");
lblHeading.setLayoutData( new GridData(SWT.CENTER, GridData.FILL_VERTICAL, true, false, 2, 1));
Group loginGr = new Group(shell, SWT.NONE);
GridData gd_loginGr = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
1);
loginGr.setLayoutData(gd_loginGr);
loginGr.setText("Login data");
loginGr.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2,
1));
loginGr.setLayout(new GridLayout(4, true));
Label lblUname = new Label(loginGr, SWT.NONE);
lblUname.setText("Username");
lblUname.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true,
false));
Text txtUname = new Text(loginGr, SWT.FLAT);
txtUname.setText("u");
txtUname.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
3,1));
Label lblPwd = new Label(loginGr, SWT.NONE);
lblPwd.setText("Password");
lblPwd.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
Text txtPass = new Text(loginGr, SWT.PASSWORD);
txtPwd.setText("p");
txtPwd.setEchoChar('*');
Label lblCol1 = new Label(loginGr, SWT.NONE); // filler for balnk grid-column
lblCol1.setText("Column-1");
lblCol1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
lblCol1.setVisible(false);
Button btnCancel = new Button(loginGr, SWT.FLAT);
btnCancel.setText("Cancel");
btnCancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
btnCancel.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
shell.dispose();
}
});
Label lblCol2 = new Label(loginGr, SWT.NONE); // filler for gridcolumn 3
lblCol2.setText("Column-3");
lblCol2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
lblCol2.setVisible(false);
Button btnOK = new Button(loginGr, SWT.FLAT);
btnOK.setText("Login");
btnOK.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
shell.setDefaultButton(btnOK);
btnOK.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
if(LoginAuthentication.isValidLogin(txtUname.getText(), txtPass.getText())){
SplashWin sw = new SplashWin();
}
});
shell.pack();
}
public class SplashWin {
private final Shell splash;
private Display display;
private int counter;
public SplashWin()
{
display = Display.getDefault();
splash = new Shell(SWT.ON_TOP | SWT.APPLICATION_MODAL | SWT.NONE);
final Label lbl_img = new Label(splash, SWT.NONE);
final Image img = new Image("test.png");
lbl_img.setImage(img);
splash.open();
display.asyncExec(new Runnable()
{
public void run()
{
for(counter=0; counter<5; counter++
{
try {
Thread.sleep(40);
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
splash.close();
img.dispose();
splash.dispose();
// Create and call open() of an ApplicationWindow object
MainWin mainWin = new MainWin();
mainWin.open();
}
});
while(counter != 5)
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
public static void main(String[] args){
new SplashWin();
}
}
公共类SplashWin{
私人最终炮弹飞溅;
私人显示器;
专用int计数器;
公众参与
{
display=display.getDefault();
飞溅=新外壳(SWT.ON|u TOP | SWT.APPLICATION|u model | SWT.NONE);
最终标签lbl_img=新标签(飞溅,SWT.NONE);
最终图像img=新图像(“test.png”);
lbl_img.setImage(img);
splash.open();
display.asyncExec(新的Runnable()
{
公开募捐
{
对于(计数器=0;计数器显示打开登录外壳的代码,循环调用线程。sleep
在循环期间阻塞UI线程(将其包装在asyncExec
中没有任何区别)。如果要在延迟后运行代码,请使用Display.timerExec
。