如何停用特定menuItem的方法?

时间:2011-06-11 16:51:33

标签: java swing

我想检查选择哪个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声明为公共静态,我无法引用他。

2 个答案:

答案 0 :(得分:1)

查看actionListeners并查看此tutorial

答案 1 :(得分:1)

鉴于代码不完整(CreateMenuItem做什么?),我不确定我能否提出一个很好的解决方案。但我假设你有一堆JRadioButton s,在这种情况下你可以这样做:

if ( !menuGame8.isSelected() && !menuGame9.isSelected() ) {
   drawCageLabels();
}