gpt4 book ai didi

java - JRadioButton 及其操作

转载 作者:行者123 更新时间:2023-11-30 07:53:41 26 4
gpt4 key购买 nike

我编写这段代码是为了让用户在选择游戏模式时必须做出选择:

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 时,您是否取消选择了之前选择的那个?如何在按下“开始游戏”按钮时启动游戏并关闭菜单?

最佳答案

您可能面临的问题是,您通过 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();
}

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

希望这对您有所帮助:)

关于java - JRadioButton 及其操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44608009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com