Java SWT重绘外壳

Java SWT重绘外壳,java,swt,redraw,Java,Swt,Redraw,问题很明显,所以我希望在按下按钮(或者我的例子中的标签)后,布局会改变并更新。我尝试了几种建议的解决方案,但没有一种对我有效。更改位于“登录”鼠标轨迹侦听器内的createLoginLayout()中。我知道代码看起来过于复杂,这是因为我使用WindowBuilder protected Shell mainShell; private Text hangmanPic; private Text clueField; private Composite alphabetComposite; pr

问题很明显,所以我希望在按下按钮(或者我的例子中的标签)后,布局会改变并更新。我尝试了几种建议的解决方案,但没有一种对我有效。更改位于“登录”鼠标轨迹侦听器内的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));
        }
    });



}

createRegisterLayout
中,您创建了一个新的Shell,但该Shell从未打开。由于字段
mainShell
被重新分配给这个不可见的shell,所有对
mainShell
的后续调用,例如
layout()
update()
等,都没有任何可见的效果。

预期的行为是什么?会发生什么?到目前为止你尝试了什么?你可以看到我到目前为止尝试了什么:),我几乎尝试了布局/重画/更新的所有组合。我甚至试着把它们放在一根线里,但还是不行。有趣的是,当我使用mainShell.open()时,一个新更新的shell是打开的,但我希望更新将在同一个shell中执行“我尝试了几个建议的解决方案,但没有一个对我有效”哪个解决方案?您有两个
new shell()
调用,这意味着您创建了两个shell。您将它们分配给同一个变量,但仍然有两个(实际上,有1+#多次单击)