2014-04-26 2 views
0

스도쿠 GUI에 대한 JTextFields 그리드가 있지만 맨 위 왼쪽 상자에 예를 들어 3을 배치하는 방법과 혼동하는 경우가 있습니다 그 상자의 그래서이JTextField의 그리드에있는 특정 JTextField에 특정 숫자 배치

http://tinypic.com/r/t7io28/8

같은 것을보고 그 숫자는 사용자에게 편집되지 않은이됩니까?

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 = ""; 
     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(); 
      } 
     } 
    } 
} 

을하고 여기에 3 × 3 격자 JTextFieldsetEditable(false)를 사용하여 편집 할 설정할 수 있습니다

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(); 
     } 
    } 

} 

답변

0

에 3 × 3 세포를 설정하는 클래스의 :

여기에 세포를 설정하는 내 수업입니다. JLabels을 사용할 수도 있습니다.

+0

어떻게 구현합니까? – user2880644

+0

@ user2880644 좀 더 구체적으로 기재 할 수 있습니까? – fdsa

+0

특정 텍스트 필드를 편집 할 수 없도록 설정하고 번호를 입력하고 싶습니다. 전체 그리드는 빈 텍스트 필드입니다. 모든 텍스트 필드에 3을 표시하는 전체 격자가없는 상자에 3을 배치하는 방법을 알고 싶습니다. – user2880644