Java SWT:组对象拒绝从其父级(shell)继承其背景

时间:2015-02-28 19:59:38

标签: java swt

大约一年前,我写了一个小的Checkers游戏作为一些研究任务的一部分。从那时起,代码文件就在闪存驱动器上,最近我决定恢复它并使其更好,只是为了填补我的业余时间。
我用SWT写了它,我记得当时它完美无缺 奇怪的是,在重新设置了所有环境之后(这是一台新计算机,所以我不得不安装Eclipse,导入SWT jar文件等等),然后运行它,我很惊讶地发现了一些Group对象 - 拿着按钮 - 没有正确显示。

首先让我发布代码,然后我会解释我正在谈论的内容(它很长,所以我省略了不相关的部分)......

public class MainMenu {


    public static void main(String[] args) {
        MainMenu menu=new MainMenu();
        menu.run();
    }

    /*private class StartGameListener extends MouseAdapter {
         // omitted because it is not relevant, unless it is, and then I wouldn't mind posting it too...
    }*/


    final static String images_path="./images";

    final private Display display;
    final private Shell shell;  
    private Button start_game;
    private Button quit_game;
    private Button[] mode_options;
    private Button[] board_options;
    private Text player_one_name;
    private Text player_two_name;

    private BoardPanel board;
    private CustomBoard custom_board_menu;
    private IController game_controller;

    //constructor
    public MainMenu() {

        /******************
         * main shell setup
         ******************/
        display=new Display();
        shell=new Shell(display, SWT.DIALOG_TRIM);
        shell.setText("Checkers Game");
        Image menu_background=new Image(display, images_path+"/backgrounds/menu.jpg");
        shell.setBackgroundImage(menu_background);
        shell.setBounds(menu_background.getBounds());
        shell.setBackgroundMode(SWT.INHERIT_DEFAULT); 


        /******************************************
         * Start Game and Quit game button
         ******************************************/
        start_game=new Button(shell, SWT.PUSH);
        start_game.setText("Start Game");
        start_game.setLocation(650, 135);
        start_game.pack();
        start_game.addMouseListener(new StartGameListener());

        quit_game=new Button(shell, SWT.PUSH);
        quit_game.setText("Quit Game");
        quit_game.setLocation(650, 170);
        quit_game.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(MouseEvent e) {
                shell.dispose();
                display.dispose();
            }

        });
        quit_game.pack();


        /******************
         * game modes Group
         ******************/
        Group game_modes=new Group(shell,SWT.SHADOW_ETCHED_OUT);
        game_modes.setText("Game Mode");
        game_modes.setLocation(65, 120);
        game_modes.setForeground(display.getSystemColor(SWT.COLOR_WHITE));

        mode_options=new Button[5];
        mode_options[0]=new Button(game_modes, SWT.RADIO);
        mode_options[1]=new Button(game_modes, SWT.RADIO);
        mode_options[2]=new Button(game_modes, SWT.RADIO);
        mode_options[3]=new Button(game_modes, SWT.CHECK);
        mode_options[4]=new Button(game_modes, SWT.CHECK);

        mode_options[0].setText("Versus Mode");
        mode_options[0].setLocation(5, 20);
        mode_options[0].setSelection(true);
        mode_options[0].setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        mode_options[0].pack();

        mode_options[1].setText("PC Mode");
        mode_options[1].setLocation(5, 45);
        mode_options[1].setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        mode_options[1].pack();

        mode_options[2].setText("Demo Game");
        mode_options[2].setLocation(5, 70);
        mode_options[2].setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        mode_options[2].pack();

        mode_options[3].setText("Creatures");
        mode_options[3].setLocation(130, 70);
        mode_options[3].setEnabled(false);
        mode_options[3].setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        mode_options[3].pack();

        mode_options[4].setText("Custom Game");
        mode_options[4].setLocation(130, 20);
        mode_options[4].setSelection(false);
        mode_options[4].setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        mode_options[4].pack();


        /*********************************************
         * the following anonymous class will listen to any
         * change in the game modes selection and will
         * react accordingly
         *********************************************/
        MouseAdapter modes_listener=new MouseAdapter() {

            @Override
            public void mouseUp(MouseEvent e) {
                String selection=((Button)e.getSource()).getText();
                switch (selection) {
                    case "Versus Mode":
                        player_one_name.setEnabled(true);
                        player_two_name.setEnabled(true);
                        mode_options[3].setEnabled(false);
                        mode_options[4].setEnabled(true);
                        break;
                    case "PC Mode":
                        player_one_name.setEnabled(true);
                        player_two_name.setEnabled(false);
                        mode_options[3].setEnabled(false);
                        mode_options[4].setEnabled(false);
                        break;
                    case "Demo Game":
                        player_one_name.setEnabled(false);
                        player_two_name.setEnabled(false);
                        mode_options[3].setEnabled(true);
                        mode_options[4].setEnabled(false);
                        break;
                }
            }

        };

        for (int i=0; i<3; i++) {
            mode_options[i].addMouseListener(modes_listener);
        }


        /******************
         * board size Group
         ******************/
        Group board_size=new Group(shell, SWT.SHADOW_ETCHED_OUT);
        board_size.setText("Board Size");
        board_size.setLocation(65, 230);
        board_size.setForeground(display.getSystemColor(SWT.COLOR_WHITE));

        board_options=new Button[2];
        board_options[0]=new Button(board_size, SWT.RADIO);
        board_options[1]=new Button(board_size, SWT.RADIO);

        board_options[0].setText("Normal");
        board_options[0].setLocation(5, 20);
        board_options[0].setSelection(true);
        board_options[0].setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        board_options[0].pack();

        board_options[1].setText("Large");
        board_options[1].setLocation(5, 45);
        board_options[1].setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        board_options[1].pack();


        /********************
         * players names Group
         ********************/
        Group players_names=new Group(shell, SWT.NONE);
        players_names.setLayout(new GridLayout(2,true));
        players_names.setLocation(335, 130);
        players_names.setForeground(display.getSystemColor(SWT.COLOR_WHITE));

        Label player_one=new Label(players_names, SWT.LEFT);
        player_one.setText("Player 1 Name: ");
        player_one.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        player_one_name=new Text(players_names, SWT.BORDER);
        player_one_name.setText("Player 1");
        player_one_name.setLayoutData(new GridData(128, SWT.DEFAULT));

        Label player_two=new Label(players_names, SWT.LEFT);
        player_two.setText("Player 2 Name: ");
        player_two.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        player_two_name=new Text(players_names, SWT.BORDER);
        player_two_name.setText("Player 2");
        player_two_name.setLayoutData(new GridData(128, SWT.DEFAULT));

        game_modes.pack();
        board_size.pack();
        players_names.pack();
    }//end of MainMenu constructor


    //render the main menu
    public void run() {

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) 
                display.sleep();
        }
    }
}

上面的代码产生了这个:
enter image description here

正如你所看到的,小组小部件(以及开始游戏和退出游戏按钮)都没有透明的背景(如果他们从父母那里继承他们的背景 - 这就是我所期望的那样)。 再次,这很奇怪,因为我记得一年前这不是问题,但是,我试图改变:

shell.setBackgroundMode(SWT.INHERIT_DEFAULT);

要:

shell.setBackgroundMode(SWT.INHERIT_FORCE);

但这没有多大帮助:

enter image description here

那些敏锐的人必须注意到现在开始游戏和退出游戏按钮更整齐地显示,但仍然 - 三个组对象不同意从他们的父母继承他们的背景(虽然他们内部的小部件[单选按钮,文本标签等...])。

有谁知道这可能是什么问题?我在这里错过了什么吗?

0 个答案:

没有答案