gpt4 book ai didi

java - JFormattedTextField 未正确清除

转载 作者:行者123 更新时间:2023-12-02 05:14:28 25 4
gpt4 key购买 nike

我正在做这个作业,制作一个解决数独的程序。我有一个带有 SudokuTextBox 网格扩展 JFormattedTextField 的面板。我有一个 MaskFormatter,因此每个文本框只接受一个整数。然后在我的面板中,当释放按键时,我会看到此代码。

 public void keyReleased(KeyEvent e) {
SudokuTextBox tb = (SudokuTextBox) e.getSource();
int row = tb.getRow();
int col = tb.getCol();
int value = toInteger(tb.getText());
//System.out.println(value);
if(sudoku.isValid(row, col, value)) {
sudoku.set(row, col, value);
}
else {
sudoku.set(row, col, 0);
tb.setText(null);
}
tb.setCaretPosition(0);
sudoku.print();
}

问题是,如果我在文本框中输入有效值,然后我返回并输入无效值(根据数独规则),文本框将被清除。但是,当我向前移动时,前一个有效值将显示在文本框中。我的数独矩阵包含所有已输入的数字,它会像应有的那样清除该值,因此它仅位于相应的文本框中。

为了让事情变得更加困惑,当我将“SudokuTextBox extends JFormattedTextField”更改为“SudokuTextBox extends JTextField”时,它就像一个魅力。但我无法设置 JTextField 的大小,使其成为正方形,并且我无法强制每个文本框仅使用一个整数。

我是否遗漏了一些非常明显的东西?

最佳答案

这是一个可能适合此类游戏的可调整大小组件的示例。尽管它不包含游戏逻辑,但它可以很好地处理输入。单击鼠标或按空格键会弹出一个菜单,并且制表符和数字键按预期工作。特别是,Digit.EMPTY 是一个有效值。

alt text

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.EnumSet;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/4148336 */
public class CellTest extends JPanel {

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

public static void createGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 3));
for (Digit d : Digit.digits) {
frame.add(new CellTest(d));
}
frame.pack();
frame.setVisible(true);
}

CellTest(Digit digit) {
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
this.setBackground(new Color(0x00e0e0));

JLabel candidates = new JLabel("123456789");
candidates.setHorizontalAlignment(JLabel.CENTER);
this.add(candidates, BorderLayout.NORTH);

JDigit cellValue = new JDigit(digit);
add(cellValue, BorderLayout.CENTER);
}
}

class JDigit extends JButton {

private static final int SIZE = 128;
private static final int BASE = SIZE / 32;
private static final Font FONT = new Font("Serif", Font.BOLD, SIZE);
private JPopupMenu popup = new JPopupMenu();
private Digit digit;
private Image image;
private int width, height;

public JDigit(Digit digit) {
this.digit = digit;
this.image = getImage(digit);
this.setPreferredSize(new Dimension(64, 64));
this.setBackground(new Color(0xe0e000));
this.setForeground(Color.black);
this.setBorderPainted(false);
this.setAction(new ButtonAction());
this.addFocusListener(new FocusHandler());
for (Digit d : Digit.values()) {
Action select = new SelectAction(d);
JMenuItem item = new JMenuItem(select);
getInputMap().put(KeyStroke.getKeyStroke(
KeyEvent.VK_0 + d.value(), 0), d.toString());
getInputMap().put(KeyStroke.getKeyStroke(
KeyEvent.VK_NUMPAD0 + d.value(), 0), d.toString());
getActionMap().put(d.toString(), select);
popup.add(item);
}
}

public Digit getDigit() {
return digit;
}

public void setDigit(Digit digit) {
this.digit = digit;
this.image = getImage(digit);
this.repaint();
}

@Override
protected void paintComponent(Graphics g) {
int w = this.getWidth();
int h = this.getHeight();
g.setColor(this.getBackground());
int dx1 = w * width / height / 4;
int dx2 = w - dx1;
g.fillRect(dx1, 0, dx2 - dx1, h);
g.drawImage(image,
dx1, 0, dx2, h,
0, 0, width, height, null);
}

private Image getImage(Digit digit) {
BufferedImage bi = new BufferedImage(
SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(this.getForeground());
g2d.setFont(FONT);
FontMetrics fm = g2d.getFontMetrics();
width = fm.stringWidth(digit.toString());
height = fm.getAscent();
g2d.drawString(digit.toString(), 0, height - BASE);
g2d.dispose();
return bi;
}

private class ButtonAction extends AbstractAction {
//@Override
public void actionPerformed(ActionEvent e) {
popup.show(JDigit.this, getWidth() - width, getHeight() / 2);
}
}

private class SelectAction extends AbstractAction {

private Digit digit;

public SelectAction(Digit digit) {
this.digit = digit;
this.putValue(Action.NAME, digit.toString());
}

//@Override
public void actionPerformed(ActionEvent e) {
setDigit(digit);
}
}

private class FocusHandler implements FocusListener {

private Color background = getBackground();

//@Override
public void focusGained(FocusEvent e) {
setBackground(background.brighter());
}

//@Override
public void focusLost(FocusEvent e) {
setBackground(background);
}
}
}

enum Digit {

EMPTY(0, " "), ONE(1, "1"), TWO(2, "2"), THREE(3, "3"), FOUR(4, "4"),
FIVE(5, "5"), SIX(6, "6"), SEVEN(7, "7"), EIGHT(8, "8"), NINE(9, "9");
public static EnumSet<Digit> digits = EnumSet.range(Digit.ONE, Digit.NINE);
private int i;
private String s;

Digit(int i, String s) {
this.i = i;
this.s = s;
}

@Override
public String toString() {
return s;
}

public int value() {
return i;
}
}

关于java - JFormattedTextField 未正确清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4148336/

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