JRadioButton 及其操作
JRadioButton and action on them
我编写这段代码是为了让用户在选择游戏模式时必须做出选择:
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");
前三个用于选择游戏难度,后两个用于选择模式。我只设置了select新手难度和矿井全随机modality.TheStart Game
按钮,从名字就可以理解是需要开始游戏
创建 JRadioButton
和 JButton
后,我将把它们添加到 GroupLayout
。
Controller.java
这个class我以为可以处理所有按钮事件。
@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);
}
}
}
当您 select 一个特定的 JRadioButton 时,您会删除select 之前 select 编辑的那个吗?如何在按下“开始游戏”按钮时启动游戏并关闭菜单?
您可能面临的问题是,您通过 ActionListener 事件获得的源在 source 中始终相同,难度[=39=]和选择。首先,你应该有一个组来存储单选按钮:
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
变量来安全地执行这些操作。
希望这对您有所帮助:)
我编写这段代码是为了让用户在选择游戏模式时必须做出选择:
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");
前三个用于选择游戏难度,后两个用于选择模式。我只设置了select新手难度和矿井全随机modality.TheStart Game
按钮,从名字就可以理解是需要开始游戏
创建 JRadioButton
和 JButton
后,我将把它们添加到 GroupLayout
。
Controller.java
这个class我以为可以处理所有按钮事件。
@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);
}
}
}
当您 select 一个特定的 JRadioButton 时,您会删除select 之前 select 编辑的那个吗?如何在按下“开始游戏”按钮时启动游戏并关闭菜单?
您可能面临的问题是,您通过 ActionListener 事件获得的源在 source 中始终相同,难度[=39=]和选择。首先,你应该有一个组来存储单选按钮:
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
变量来安全地执行这些操作。
希望这对您有所帮助:)