Ho questo adesso:
L'unico problema è che se scrivo in una JTextField il numero compare anche in tutte le altre ...codice:public class SudokuPanel extends JPanel { private static final long serialVersionUID = 1L; private Sudoku sudoku; public SudokuPanel(Sudoku sudoku) { this.sudoku = sudoku; setLayout(new GridLayout(9,9)); setPreferredSize(new Dimension(600,600)); setBorder(BorderFactory.createLineBorder(Color.BLACK)); MyTextFieldDocument document = new MyTextFieldDocument(1); //Crea la griglia for(int i = 0; i < Sudoku.DIM; i++) { for(int j = 0; j < Sudoku.DIM; j++) { JTextField textField = new JTextField(); textField.setDocument(document); Font font = new Font("Serif", Font.BOLD, 45); textField.setFont(font); textField.setHorizontalAlignment(JTextField.CENTER); textField.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); if((i == 3 && j < 9) || (i == 6 && j < 9)) { textField.setBorder(BorderFactory.createMatteBorder(5, 1, 1, 1, Color.DARK_GRAY)); } if((j == 3 && i < 9) || (j == 6 && i < 9)) { textField.setBorder(BorderFactory.createMatteBorder(1, 5, 1, 1, Color.DARK_GRAY)); } if((i == 3 && j == 3) || (i == 3 && j == 6) || (i == 6 && j == 3) || (i == 6 && j == 6)) { textField.setBorder(BorderFactory.createMatteBorder(5, 5, 1, 1, Color.DARK_GRAY)); } add(textField); } } } } class MyTextFieldDocument extends PlainDocument { private static final long serialVersionUID = 1L; private int maxlength; MyTextFieldDocument(int maxlength) { this.maxlength = maxlength; } public void insertString(int offset, String str, javax.swing.text.AttributeSet a) throws BadLocationException { Integer integer = Integer.parseInt(str); if (integer > 0 && integer < 10 && (getLength() < maxlength)) { super.insertString(offset, str, a); } } }

Rispondi quotando