2015-05-29 170 views
0

问题是非常明显的,所以我想在按下按钮(或在我的情况下的标签)后,布局将更改和更新。我尝试了几个解决方案,但没有为我工作。该更改位于“登录”鼠标轨道侦听器中的createLoginLayout()中。我知道代码看起来过于复杂,这是因为我用的WindowBuilderSWT重绘外壳

protected Shell mainShell; 
private Text hangmanPic; 
private Text clueField; 
private Composite alphabetComposite; 
private Display display; 
private Label currGuess; 
private GameManager gameManager; 
private Label clueTitle; 
private Text usrField; 
private Label passTitle; 
private Text passField; 
private Label login; 
private Label register; 

public static void main(String[] args) { 
    try { 
     Main window = new Main(); 
     window.open(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

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

/** 
* Create contents of the window. 
* @wbp.parser.entryPoint 
*/ 
protected void createContents() { 

    //Init new game 
    gameManager=new GameManager(); 

    //createGameMainLayout(); 
    createLoginLayout(); 
    //createRegisterLayout(); 

} 

/** 
* Creates the layout of the login page 
*/ 
private void createLoginLayout(){ 
    mainShell = new Shell(display,SWT.CLOSE | SWT.TITLE | SWT.MIN); 
    mainShell.setSize(685, 481); 
    mainShell.setText("Location Hangman"); 
    mainShell.setLayout(new FormLayout()); 
    Image image = new Image(display, System.getProperty("user.dir")+"/res/back.jpg"); 
    mainShell.setBackgroundImage(image); 
    mainShell.setBackgroundMode(SWT.INHERIT_FORCE); 

    Label gameTitle = new Label(mainShell, SWT.NONE); 
    gameTitle.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 40, SWT.NONE)); 
    gameTitle.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    gameTitle.setAlignment(SWT.CENTER); 
    gameTitle.setText("Location Hangman ^"); 
    FormData fd_gameTitle = new FormData(); 
    fd_gameTitle.top = new FormAttachment(0, 94); 
    fd_gameTitle.left = new FormAttachment(0, 10); 
    fd_gameTitle.right = new FormAttachment(100, -10); 
    fd_gameTitle.bottom = new FormAttachment(0, 163); 
    gameTitle.setLayoutData(fd_gameTitle); 

    Label usrTitle = new Label(mainShell, SWT.NONE); 
    usrTitle.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    usrTitle.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    usrTitle.setAlignment(SWT.RIGHT); 
    FormData fd_usrTitle = new FormData(); 
    fd_usrTitle.top = new FormAttachment(gameTitle, 81); 
    fd_usrTitle.left = new FormAttachment(0, 10); 
    usrTitle.setLayoutData(fd_usrTitle); 
    usrTitle.setText("Username:"); 

    usrField = new Text(mainShell, SWT.NONE); 
    fd_usrTitle.right = new FormAttachment(usrField, -6); 
    usrField.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    usrField.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    FormData fd_usrField = new FormData(); 
    fd_usrField.left = new FormAttachment(0, 331); 
    fd_usrField.right = new FormAttachment(100, -148); 
    fd_usrField.top = new FormAttachment(gameTitle, 81); 
    usrField.setLayoutData(fd_usrField); 

    passTitle = new Label(mainShell, SWT.NONE); 
    passTitle.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    passTitle.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    passTitle.setAlignment(SWT.RIGHT); 
    FormData fd_passTitle = new FormData(); 
    fd_passTitle.top = new FormAttachment(usrTitle, 22); 
    fd_passTitle.right = new FormAttachment(usrTitle, 0, SWT.RIGHT); 
    fd_passTitle.left = new FormAttachment(0, 10); 
    passTitle.setLayoutData(fd_passTitle); 
    passTitle.setText("Password:"); 

    passField = new Text(mainShell, SWT.PASSWORD | SWT.NONE); 
    passField.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    passField.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    FormData fd_passField = new FormData(); 
    fd_passField.top = new FormAttachment(passTitle, 0, SWT.TOP); 
    fd_passField.left = new FormAttachment(usrField, 0, SWT.LEFT); 
    fd_passField.right = new FormAttachment(100, -148); 
    passField.setLayoutData(fd_passField); 

    login = new Label(mainShell, SWT.NONE); 
    fd_passTitle.bottom = new FormAttachment(login, -43); 
    login.setAlignment(SWT.CENTER); 
    login.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    login.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    login.setText("Login"); 
    FormData fd_login = new FormData(); 
    fd_login.right = new FormAttachment(usrTitle, 0, SWT.RIGHT); 
    fd_login.bottom = new FormAttachment(100, -27); 
    fd_login.top = new FormAttachment(100, -68); 
    fd_login.left = new FormAttachment(0, 202); 
    login.setLayoutData(fd_login); 
    login.addMouseTrackListener(new MouseTrackAdapter() { 
     @Override 
     public void mouseEnter(MouseEvent arg0) { 
      login.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY)); 
     } 
     @Override 
     public void mouseExit(MouseEvent arg0) { 
      login.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
     } 
    }); 


    register = new Label(mainShell, SWT.NONE); 
    register.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseDown(MouseEvent arg0) { 

      createRegisterLayout(); //change of layout 

      mainShell.layout(true, true); 
      mainShell.redraw(); 
      mainShell.update(); 

     } 
    }); 
    register.setAlignment(SWT.CENTER); 
    register.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    register.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    register.setText("Register"); 
    FormData fd_register = new FormData(); 
    fd_register.top = new FormAttachment(login, 0, SWT.TOP); 
    fd_register.left = new FormAttachment(usrField, 0, SWT.LEFT); 
    fd_register.right = new FormAttachment(100, -225); 
    register.setLayoutData(fd_register); 
    register.addMouseTrackListener(new MouseTrackAdapter() { 
     @Override 
     public void mouseEnter(MouseEvent arg0) { 
      register.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY)); 
     } 
     @Override 
     public void mouseExit(MouseEvent arg0) { 
      register.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
     } 
    }); 

} 

/** 
* Creates the layout of the register page 
*/ 
private void createRegisterLayout(){ 
    mainShell = new Shell(display,SWT.CLOSE | SWT.TITLE | SWT.MIN); 
    mainShell.setSize(685, 481); 
    mainShell.setText("Location Hangman"); 
    mainShell.setLayout(new FormLayout()); 
    Image image = new Image(display, System.getProperty("user.dir")+"/res/back.jpg"); 
    mainShell.setBackgroundImage(image); 
    mainShell.setBackgroundMode(SWT.INHERIT_FORCE); 

    Label gameTitle = new Label(mainShell, SWT.NONE); 
    gameTitle.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 40, SWT.NONE)); 
    gameTitle.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    gameTitle.setAlignment(SWT.CENTER); 
    gameTitle.setText("Register"); 
    FormData fd_gameTitle = new FormData(); 
    fd_gameTitle.top = new FormAttachment(0, 94); 
    fd_gameTitle.left = new FormAttachment(0, 10); 
    fd_gameTitle.right = new FormAttachment(100, -10); 
    fd_gameTitle.bottom = new FormAttachment(0, 163); 
    gameTitle.setLayoutData(fd_gameTitle); 

    Label usrTitle = new Label(mainShell, SWT.NONE); 
    usrTitle.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    usrTitle.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    usrTitle.setAlignment(SWT.RIGHT); 
    FormData fd_usrTitle = new FormData(); 
    fd_usrTitle.top = new FormAttachment(gameTitle, 81); 
    fd_usrTitle.left = new FormAttachment(0, 10); 
    usrTitle.setLayoutData(fd_usrTitle); 
    usrTitle.setText("Enter Username:"); 

    usrField = new Text(mainShell, SWT.NONE); 
    fd_usrTitle.right = new FormAttachment(usrField, -6); 
    usrField.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    usrField.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    FormData fd_usrField = new FormData(); 
    fd_usrField.left = new FormAttachment(0, 331); 
    fd_usrField.right = new FormAttachment(100, -148); 
    fd_usrField.top = new FormAttachment(gameTitle, 81); 
    usrField.setLayoutData(fd_usrField); 

    passTitle = new Label(mainShell, SWT.NONE); 
    passTitle.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    passTitle.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    passTitle.setAlignment(SWT.RIGHT); 
    FormData fd_passTitle = new FormData(); 
    fd_passTitle.top = new FormAttachment(usrTitle, 22); 
    fd_passTitle.right = new FormAttachment(usrTitle, 0, SWT.RIGHT); 
    fd_passTitle.left = new FormAttachment(0, 10); 
    passTitle.setLayoutData(fd_passTitle); 
    passTitle.setText("Enter Password:"); 

    passField = new Text(mainShell, SWT.PASSWORD | SWT.NONE); 
    passField.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    passField.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    FormData fd_passField = new FormData(); 
    fd_passField.top = new FormAttachment(passTitle, 0, SWT.TOP); 
    fd_passField.left = new FormAttachment(usrField, 0, SWT.LEFT); 
    fd_passField.right = new FormAttachment(100, -148); 
    passField.setLayoutData(fd_passField); 


    register = new Label(mainShell, SWT.NONE); 
    register.setAlignment(SWT.CENTER); 
    register.setFont(new Font(Display.getDefault(),"DJB CHALK IT UP", 20, SWT.NONE)); 
    register.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
    register.setText("OK"); 
    FormData fd_register = new FormData(); 
    fd_register.bottom = new FormAttachment(100, -40); 
    fd_register.left = new FormAttachment(0, 276); 
    fd_register.right = new FormAttachment(100, -280); 
    register.setLayoutData(fd_register); 
    register.addMouseTrackListener(new MouseTrackAdapter() { 
     @Override 
     public void mouseEnter(MouseEvent arg0) { 
      register.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY)); 
     } 
     @Override 
     public void mouseExit(MouseEvent arg0) { 
      register.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 
     } 
    }); 



} 
+0

预期的行为是什么?会发生什么呢?你试过什么了? – Baz

+0

你可以看到我到目前为止尝试过的:),我几乎尝试了布局/重绘/更新的各种组合。我甚至试图把它们放在一个线程中,它仍然没有工作。有趣的是,当我使用mainShell.open()时,一个新的更新的shell已打开,但我希望更新将在同一个shell中执行 – eli

+0

_“我尝试了几个解决方案,但没有人为我工作”_哪种解决方案? – Baz

回答

0

createRegisterLayout,创建一个新的外壳,但壳牌从未打开过。由于字段mainShell被重新分配给这个不可见的shell,因此所有后续调用mainShell(如layout()update()等)都没有任何可见的效果。