我想以某种方式检查选择了哪个MenuItem,以便对于某些菜单按钮以不同的方式绘制网格。

我在包视图中的MainUI类中创建了菜单:

我也忘了提到创建菜单的代码在一个名为showContainers的方法中

        // Create the game menu
    menuGame = new JMenu( "Game" );
    menuGame.setMnemonic( 'G' );
    menuBar.add( menuGame );
    // Create the game menu
    // Build a game menu items
            //...Create 3x3 Game
    menuGame3 = CreateMenuItem( menuGame, ITEM_RADIO,
                            "Create 3x3 Game", null, '3', null );
    //...Create 4x4 Game
    menuGame4 = CreateMenuItem( menuGame, ITEM_RADIO,
                            "Create 4x4 Game", null, '4', null );
            //...Create 5x5 Game
    menuGame5 = CreateMenuItem( menuGame, ITEM_RADIO,
                            "Create 5x5 Game", null, '5', null );
    //...Create 6x6 Game
    menuGame6 = CreateMenuItem( menuGame, ITEM_RADIO,
                            "Create 6x6 Game", null, '6', null );
            //...Create 7x7 Game
    menuGame7 = CreateMenuItem( menuGame, ITEM_RADIO,
                            "Create 7x7 Game", null, '7', null );
            //...Create 8x8 Game
    menuGame8 = CreateMenuItem( menuGame, ITEM_RADIO,
                            "Create 8x8 Game", null, '8', null );
            //...Create 9x9 Game
    menuGame9 = CreateMenuItem( menuGame, ITEM_RADIO,
                            "Create 9x9 Game", null, '9', null );
            ButtonGroup group = new ButtonGroup();
            group.add(menuGame3);
            group.add(menuGame4);
            group.add(menuGame5);
            group.add(menuGame6);
            group.add(menuGame7);
            group.add(menuGame8);
            group.add(menuGame9);

        public void addCreateGame3Listener(ActionListener createGameListener){
    menuGame3.addActionListener(createGameListener);
}

public void addCreateGame4Listener(ActionListener createGameListener){
    menuGame4.addActionListener(createGameListener);
}

    public void addCreateGame5Listener(ActionListener createGameListener){
    menuGame5.addActionListener(createGameListener);
}

public void addCreateGame6Listener(ActionListener createGameListener){
    menuGame6.addActionListener(createGameListener);
}

    public void addCreateGame7Listener(ActionListener createGameListener){
    menuGame7.addActionListener(createGameListener);
}

    public void addCreateGame8Listener(ActionListener createGameListener){
    menuGame8.addActionListener(createGameListener);
}

    public void addCreateGame9Listener(ActionListener createGameListener){
    menuGame9.addActionListener(createGameListener);
}


这是我所做的修改的GameUI中的painComponent:

@Override
public void paintComponent(Graphics g){

    highLightCage =false ;
    super.paintComponent(g); //clear the off-screen bitmap
    drawCells(g);
    drawCages(g);
            if( MainUI.menuGame3.isSelected()){
                    drawCageLabels(g);
            }
    drawLabels(g);
    drawGameHint(g);
}


问题是我没有在MainUI类中将menuGame3声明为public static,并且无法引用他。

最佳答案

调查动作听众,并看到此

关于java - 如何停用特定menuItem的方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6317231/

10-13 07:24