gpt4 book ai didi

java - 按键监听器不断监听更多按键,我不知道如何阻止它这样做

转载 作者:行者123 更新时间:2023-12-01 17:43:04 25 4
gpt4 key购买 nike

所以我制作了一个小数独游戏,我使用切换按钮来接受输入。基本上,当切换按钮被切换时,会激活一个按键监听器来监听按键 1-9。问题是,输入 key 后,我使用了buttonGroup.clearSelection(),它再次切换按钮,因此它被取消选择,但是文本上方有一个小虚线矩形,如果我按任何其他数字,它会更改前一位数字并写入新数字。我对 Java 和 GUI 很陌生,所以问题可能完全是其他问题。代码附在下面。请帮忙!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Test implements ActionListener, KeyListener {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JToggleButton[] cells = new JToggleButton[81];
JToggleButton selectedButton = new JToggleButton();
ButtonGroup group = new ButtonGroup();
int index;

int[][] board = {
{7, 2, 6, 4, 9, 3, 8, 0, 5},
{3, 1, 5, 0, 2, 8, 0, 4, 6},
{4, 0, 9, 6, 5, 1, 2, 3, 7},
{8, 5, 2, 1, 0, 7, 6, 0, 3},
{6, 7, 3, 9, 0, 5, 0, 2, 4},
{9, 4, 0, 3, 6, 2, 7, 5, 8},
{1, 9, 4, 8, 3, 6, 5, 0, 2},
{5, 0, 7, 2, 1, 0, 3, 8, 9},
{2, 3, 8, 5, 7, 9, 4, 6, 0}
};

public Test() {
for (int x=0; x<81; x++) {
cells[x] = new JToggleButton();
group.add(cells[x]);
cells[x].putClientProperty("index", x);
cells[x].addActionListener(this);
panel.add(cells[x]);
}

panel.setLayout(new GridLayout(9, 9, 2, 2));
panel.setBackground(Color.black);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

printBoard(board);

frame.setJMenuBar(menuBar);
frame.pack();
frame.add(panel);
frame.pack();
frame.setSize(600, 600);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
selectedButton = (JToggleButton)e.getSource();
index = (int) selectedButton.getClientProperty("index");
selectedButton.addKeyListener(this);
}

public void keyPressed(KeyEvent e) {
Integer digit;
Integer r, c;
r = index / 9;
c = index % 9;
digit = Integer.valueOf(e.getKeyCode() - 48);

if (digit > 0 && digit <10) {
board[r][c] = digit;
}

String s = digit.toString();

if (e.getKeyCode() >= 49 && e.getKeyCode() <= 57) {
selectedButton.setText(s);
group.clearSelection();
} else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE || e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_ESCAPE) {
board[r][c] = 0;
selectedButton.setText("");
group.clearSelection();
}
}

public void keyReleased(KeyEvent e) {
group.clearSelection();
}

public void keyTyped(KeyEvent e) {}

public void printBoard(int[][] board) {
for (int x=0; x<81; x++) {
cells[x].setEnabled(true);
}
Integer digit;
String buttonText;

int buttonIndex = 0;
for (int r=0; r<9; r++) {
for (int c=0; c<9; c++) {
digit = Integer.valueOf(board[r][c]);
if (digit == 0) {
cells[buttonIndex].setText("");
buttonIndex++;
continue;
}
else if (digit != 0) {
buttonText = digit.toString();
cells[buttonIndex].setText(s);
cells[buttonIndex].setEnabled(false);
buttonIndex++;
}
}
}
}

public static void main(String[] args) {
new Test();
}
}

这里的group是所有切换按钮的ButtonGroup。它确实取消选择按钮,但关键监听器仍然保持激活状态。请帮忙。

最佳答案

您所要做的就是在释放按键时删除按键监听器。

我必须注释掉 JMenu,因为它一定在其他地方,

这是 GUI。

Sudoku Test GUI

这是我使用的代码。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SudokuTest implements ActionListener, KeyListener {
JFrame frame = new JFrame("Sudoku Test");

JPanel panel = new JPanel();
JToggleButton[] cells = new JToggleButton[81];
JToggleButton selectedButton = new JToggleButton();
ButtonGroup group = new ButtonGroup();
int index;

int[][] board = { { 7, 2, 6, 4, 9, 3, 8, 0, 5 }, { 3, 1, 5, 0, 2, 8, 0, 4, 6 }, { 4, 0, 9, 6, 5, 1, 2, 3, 7 },
{ 8, 5, 2, 1, 0, 7, 6, 0, 3 }, { 6, 7, 3, 9, 0, 5, 0, 2, 4 }, { 9, 4, 0, 3, 6, 2, 7, 5, 8 },
{ 1, 9, 4, 8, 3, 6, 5, 0, 2 }, { 5, 0, 7, 2, 1, 0, 3, 8, 9 }, { 2, 3, 8, 5, 7, 9, 4, 6, 0 } };

public SudokuTest() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Font font = panel.getFont().deriveFont(32F).deriveFont(Font.BOLD);
for (int x = 0; x < 81; x++) {
cells[x] = new JToggleButton();
group.add(cells[x]);
cells[x].putClientProperty("index", x);
cells[x].addActionListener(this);
cells[x].setFont(font);
panel.add(cells[x]);
}

panel.setLayout(new GridLayout(9, 9, 2, 2));
panel.setBackground(Color.black);

printBoard(board);

// frame.setJMenuBar(menuBar);
frame.add(panel);
frame.pack();
frame.setSize(600, 600);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
selectedButton = (JToggleButton) e.getSource();
index = (int) selectedButton.getClientProperty("index");
selectedButton.addKeyListener(this);
}

public void keyPressed(KeyEvent e) {
Integer digit;
Integer r, c;
r = index / 9;
c = index % 9;
digit = Integer.valueOf(e.getKeyCode() - 48);

if (digit > 0 && digit < 10) {
board[r][c] = digit;
}

String s = digit.toString();

if (e.getKeyCode() >= 49 && e.getKeyCode() <= 57) {
selectedButton.setText(s);
group.clearSelection();
} else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE || e.getKeyCode() == KeyEvent.VK_DELETE
|| e.getKeyCode() == KeyEvent.VK_ESCAPE) {
board[r][c] = 0;
selectedButton.setText("");
group.clearSelection();
}
}

public void keyReleased(KeyEvent e) {
group.clearSelection();
selectedButton.removeKeyListener(this);
}

public void keyTyped(KeyEvent e) {
}

public void printBoard(int[][] board) {
for (int x = 0; x < 81; x++) {
cells[x].setEnabled(true);
}
Integer digit;
String buttonText;

int buttonIndex = 0;
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
digit = Integer.valueOf(board[r][c]);
if (digit == 0) {
cells[buttonIndex].setText("");
buttonIndex++;
} else {
buttonText = digit.toString();
cells[buttonIndex].setText(buttonText);
cells[buttonIndex].setEnabled(false);
buttonIndex++;
}
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SudokuTest();
}
});
}

}

关于java - 按键监听器不断监听更多按键,我不知道如何阻止它这样做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60913732/

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