Java 在JTextFields网格中的特定JTextField中放置特定数字

Java 在JTextFields网格中的特定JTextField中放置特定数字,java,jtextfield,Java,Jtextfield,我的数独gui有一个JTextFields网格,但我不知道如何在左上角的框中放置一个3,所以看起来像这样 用户是否无法编辑该数字 下面是我设置单元格的课程: import java.awt.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swi

我的数独gui有一个JTextFields网格,但我不知道如何在左上角的框中放置一个3,所以看起来像这样

用户是否无法编辑该数字

下面是我设置单元格的课程:

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;


public class Squares extends JPanel {

    public final int CELL_COUNT = 9;
    public Cell [] cells = new Cell[CELL_COUNT];

    public Squares(){
        this.setLayout(new GridLayout(3,3));
        this.setBorder(new LineBorder(Color.BLACK,2));
        for(int i = 0; i<CELL_COUNT; i++){
            cells[i] = new Cell();
            cells[i].setDocument(new NumericalDocument());
            this.add(cells[i]);
        }
    }

    public class Cell extends JTextField{

        private int number;
        public Cell(){

        }
        public void setNumber(int number){
            this.number = number;
            this.setText("5");
        }
        public int getNumber(){

            return number;
        }

    }

    public static class NumericalDocument extends PlainDocument{
        String numbers = "0123456789";
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            if (getLength() == 0 && str.length() == 1 && numbers.contains(str)) {
                super.insertString(offs, str, a);
            }
            else {
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }
}
这是一个将3x3单元格设置为3x3网格的类

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class SudokuPanel extends JFrame {

    public final int SQUARE_COUNT = 9;
    public Squares [] squares = new Squares[SQUARE_COUNT];

    public SudokuPanel(){

        super("Sudoku");
        setSize(600,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JPanel panel = new JPanel(new GridLayout(3,3));
        for(int i=0; i<SQUARE_COUNT; i++){
            squares[i] = new Squares();
            panel.add(squares[i]);
        }

        JPanel panel2 = new JPanel();
        JButton startTime = new JButton();
        JButton stop = new JButton();
        JButton submit = new JButton();
        MyTimer timer = new MyTimer();

        startTime = new JButton("Start Timer");
        stop = new JButton("Stop Timer");
        final JLabel timerLabel = new JLabel("   ...Waiting...   ");
        submit = new JButton("Done");

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu menu = new JMenu("Menu");
        menuBar.add(menu);

        JMenuItem newDifficulty = new JMenuItem("Select New Difficulty");
        menu.add(newDifficulty);

        JMenuItem reset = new JMenuItem("Reset Puzzle");
        menu.add(reset);

        JMenuItem exit = new JMenuItem("Exit");
        menu.add(exit);

        exitaction e = new exitaction();
        newDifficultyaction d = new newDifficultyaction();
        resetaction f = new resetaction();

        reset.addActionListener(f);
        exit.addActionListener(e);
        newDifficulty.addActionListener(d);

        panel2.add(timer);

        add(panel, BorderLayout.CENTER);
        add(panel2, BorderLayout.PAGE_END);

        setVisible(true);
        setLocationRelativeTo(null);

    }

    public class exitaction implements ActionListener{
        public void actionPerformed (ActionEvent e){
            System.exit(0);
        }
    }

    public class newDifficultyaction implements ActionListener{
        public void actionPerformed (ActionEvent e){
            dispose();
            Level select = new Level(); 
        }
    }

    public class resetaction implements ActionListener{
        public void actionPerformed (ActionEvent e){
            dispose();
            SudokuPanel Squares = new SudokuPanel();
        }
    }

}

JTextField可以使用设置为不可编辑。您也可以使用。

我将如何实现这一点和数字?@user2880644您能更具体一些吗?我想将某些文本字段设置为不可编辑,并在其中放置数字。整个网格是一个空白的文本字段,我只想知道如何在一个框中放置一个3,而不让整个网格在每个文本中显示三field@user2880644你在跟踪这些文本字段吗?不,上面的代码是我所有与网格相关的东西