JRadioButton和他们的行动

时间:2017-06-17 18:26:09

标签: java swing model-view-controller jradiobutton

我编写了这段代码,以便用户在选择游戏模式时做出选择:

JButton btnNewButton = new JButton("Start Game");

JRadioButton beginner = new JRadioButton("Beginner");
JRadioButton intermedie = new JRadioButton("Intermedie");
JRadioButton expert = new JRadioButton("Expert");
JRadioButton custom = new JRadioButton("Custom");
JRadioButton mineFullRandom = new JRadioButton("Mine full random");
JRadioButton minePartialRandom = new JRadioButton("Mine partial random");

前三个用于选择游戏的难度,而最后两个用于选择模式。我设置刚刚选择了初学者难度和我的全随机模态。Start Game按钮,你可以从名称中了解到开始游戏的必要性

创建JRadioButtonJButton之后,我将在GroupLayout上添加它们。

Controller.java

这个课我想过处理所有按钮事件。

@Override
public void actionPerformed(ActionEvent e) {

    JButton source = (JButton)e.getSource();
    JRadioButton difficulty = (JRadioButton)e.getSource();
    JRadioButton choice = (JRadioButton)e.getSource();

    if(source.getText().equals("Start Game")){
        if(difficulty.getText().equals("Beginner") /*&& choice.getText().equals("Mine full random")*/){
            fullRandom = new FullRandomGrid(ROW_BEGINNER, COLUMN_BEGINNER, MINE_BEGINNER);
            View view = new View(fullRandom);
            //Minesweeper.game.container.add(view);
            /*Minesweeper.game.container.add(new View(fullRandom), BorderLayout.CENTER);
            Minesweeper.game.container.remove(Minesweeper.menu);
            Minesweeper.game.setVisible(true); */
            view.setVisible(true);
        }
        else if(difficulty.getText().equals("Beginner") && choice.getText().equals("Mine partial random")){
            partialRandom = new PartialRandomGrid(ROW_BEGINNER, COLUMN_BEGINNER, MINE_BEGINNER);
            Minesweeper.game.container.add(new View(partialRandom));
            Minesweeper.game.container.remove(Minesweeper.menu);
        }
        else if(difficulty.getText().equals("Intermedie") && choice.getText().equals("Mine full random")){
            fullRandom = new FullRandomGrid(ROW_INTERMEDIE, COLUMN_INTERMEDIE, MINE_INTERMEDIE);

        }
        else if(difficulty.getText().equals("Intermedie") && choice.getText().equals("Mine partial random")){
            partialRandom = new PartialRandomGrid(ROW_INTERMEDIE, COLUMN_INTERMEDIE, MINE_INTERMEDIE);

        }
    }
}

当您选择特定的JRadioButton时,如何取消选择之前选择的JRadioButton?当按下“开始游戏”按钮时,如何启动游戏并关闭菜单?

1 个答案:

答案 0 :(得分:3)

您可能遇到的问题是,您通过 ActionListener 事件获得的来源在来源难度中始终相同的选择即可。首先,您应该有一个组来存储单选按钮:

ButtonGroup difficultyGroup = new ButtonGroup();

JRadioButton添加到此论坛时,选择一个选项会自动取消选择所有其他选项:

difficultyGroup.add(beginner);
difficultyGroup.add(intermediate);
difficultyGroup.add(expert);
...

这确保一次只选择一个按钮。 此外,解决围绕收到的事件的问题 -

为每个单选按钮添加ActionCommand将允许您通过分配给每个按钮的字符串对按钮执行操作。例如,在将按钮添加到组之前,请分配以下值:

beginner.addActionCommand("Beginner");
intermediate.addActionCommand("Intermediate");

请注意,以下字符串仅供内部使用。这意味着您可以通过分配的String

来识别按钮
public void ActionPerfomed(ActionEvent e) {
    // To get the source (for the game start button)
    JButton source = (Jbutton) e.getSource();

    // Get the radio button source which was selected
    // Resultantly obtain the String which it represents
    String difficulty = difficultyGroup.getSelection().getActionCommand();
}

现在,您可以使用条件difficulty语句中的String if变量来安全地执行这些操作。

希望这有助于:)