当前位置:编程学习 > JAVA >>

创建了2个窗口无法切换

表示新开始学java,然后跟着一本书学习如何使用GUI。
写了两个Shell的类。第一个是登陆窗口,第二个是主界面
但是无法从第一个shell 换到第二个Shell上面去。。。
想请问如何修改代码才行
public class loginWindow extends Shell {
private Text txtUser;
private Text txtKey;
private Shell myShell;
/**
 * Launch the application.
 * @param args
 */
public static void main(String args[]) {
try {
Display display = Display.getDefault();
loginWindow myShell = new loginWindow(display);
myShell.open();
myShell.layout();
while (!myShell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}

}


/**
 * Create the shell.
 * @param display
 */
public loginWindow(Display display) {
super(display, SWT.SHELL_TRIM);
setBackground(SWTResourceManager.getColor(255, 228, 225));
createContents();
new Label(this, SWT.NONE);

//小图标
Label lblIma = new Label(this, SWT.NONE);
lblIma.setImage(SWTResourceManager.getImage("/Users/pro/Pictures/May1901.png"));
lblIma.setBounds(26, 100, 31, 28);
lblIma.setText("Ima");

//登陆按钮设置
                //这个按钮是用来登陆,也就来换窗口的地方
Button btnLogin = new Button(this, SWT.NONE);
btnLogin.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
users user = new users(txtUser.getText().trim(),
txtKey.getText().trim(),comboState.getSelectionIndex());
Shell oldShell;
if(new usersSet().isValid(user))//这里判断了user的正确性(确认是对的)
{
                                       //问题从下面这一行开始的
oldShell = myShell;
System.out.println("sec");
Display display = Display.getDefault();
scoreMana tes = new scoreMana(user,display);
myShell = tes.getShell();
oldShell.dispose();
myShell.open();
}
else 
{
MessageBox msg = new MessageBox(getShell());
msg.setMessage("User's name or password Wrong!");
msg.open();
}
}
});
btnLogin.setBounds(22, 145, 94, 28);
btnLogin.setText("Login");


这里是第二个窗口

[code=java]import org.eclipse.swt.SWT;


public class scoreMana extends Shell{
private users user;
protected Shell myshell;



/**
 * @wbp.parser.entryPoint
 */
public scoreMana(users user,Display display) {
super(display, SWT.SHELL_TRIM);
System.out.println("fir po");
myshell = new Shell();

Button b1 = new Button(myshell, SWT.NONE);
b1.setBounds(74, 94, 94, 28);
b1.setText("New Button");
createContents();
this.user = user;
}

/**
 * Create contents of the window.
 */
protected void createContents() {
System.out.println("sec  po");
myshell.setBackground(SWTResourceManager.getColor(255, 245, 238));
myshell.setSize(450, 300);
myshell.setText("Welcome Window");

}
/**
 * Open the window.
 *
 */
public void open() {
Display display = Display.getDefault();
createContents();
myshell.open();
myshell.layout();
while (!myshell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

public Shell getShell() {
return myshell;
}
}


另外。。想问下为什么我设置的label无法显示图片
我用的是mac系统这个会不会有关系。
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,