SWT重绘shell

时间:2015-05-29 15:18:03

标签: java swt redraw

问题非常明显,所以我希望在按下按钮(或我的情况下的标签)后,布局会更改并更新。我尝试了几个解决方案,但没有一个适合我。更改位于“登录”鼠标轨道侦听器内的createLoginLayout()中。我知道代码看起来过于复杂,这是因为我使用WindowBuilder

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));
        }
    });



}

1 个答案:

答案 0 :(得分:0)

createRegisterLayout中,您创建了一个新的Shell,但永远不会打开此Shell。由于字段mainShell已重新分配给此隐藏shell,因此对mainShell的所有后续调用(例如layout()update()等)都没有任何可见效果