gpt4 book ai didi

java - 刷新面板时如何更改 JComboBox 中选定的索引?

转载 作者:行者123 更新时间:2023-12-01 16:13:15 25 4
gpt4 key购买 nike

我正在制作一个简单的数独,当我想开始新游戏时,我会重新加载面板。我首先将其删除,然后将其添加到框架中。问题是我可以选择新游戏的难度,但它总是选择第一个“简单”难度,而不是选择。因此,如果我在 JComboBox 中将其更改为“中”,则重新加载页面时,它将使用“简单”而不是“中”加载游戏。我应该怎么做才能让刷新后的面板接受更改后的难度?

以下是我的程序中使用的方法:

JComboBox difficulty = new JComboBox();
DefaultComboBoxModel difficultyModel = new DefaultComboBoxModel();
difficultyModel.addElement("Easy");
difficultyModel.addElement("Medium");
difficultyModel.addElement("Hard");
difficulty.setModel(tezavnostModel);
difficulty.setSelectedIndex(0);
difficulty.setPreferredSize(new Dimension(100, 25));

newGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrame.reloadSudokuBoard();
sudokuBoard.pickDifficulty(getDifficulty()));
}
});

public String getDifficulty() {
return (String)difficulty.getSelectedItem();
}

public void board(int[][] numbers, int zeros) {
int numberZeros = setDifficulty(sudokuForm.getDifficulty());
int[][] boardNumbers = gameNumbers();

public void reloadSudokuBoard() {
String newDifficulty = (sudokuForm.getDifficulty());
remove(sudokuBoard);
sudokuBoard.board(sudokuBoard.gameNumbers(), sudokuBoard.setDifficulty(newDifficulty ));
add(sudokuBoard, BorderLayout.WEST);
SwingUtilities.updateComponentTreeUI(sudokuBoard);
}

最佳答案

希望这有帮助。

    public void reloadSudokuBoard() {
int index = difficulty.getSelectedIndex();
String newDifficulty = (sudokuForm.getDifficulty());
remove(sudokuBoard);
sudokuBoard.board(sudokuBoard.gameNumbers(), sudokuBoard.setDifficulty(newDifficulty ));
add(sudokuBoard, BorderLayout.WEST);
SwingUtilities.updateComponentTreeUI(sudokuBoard);
difficulty.setSelectedIndex(index);
}

在删除组件之前,您可以使用getSelectedIndex来获取所选的索引。添加元素后,setSelectedIndex 将修复它

关于java - 刷新面板时如何更改 JComboBox 中选定的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62476737/

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