我正在尝试从文本文件、二维数组和 toString() 中播放 9x9 网格;出来很好,但我不知道为什么我的网格甚至没有弹出。我什至尝试从另一个类调用一个非常简单的事件按钮,看看它是否会起作用,但我仍然什么也没得到(我是从 main 方法调用它)。但是,当我在另一个类中运行这个简单的事件按钮时它工作正常,我不知道为什么它不起作用。
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import java.applet.Applet;
import java.awt.GridLayout;
public class SudokuBrdManager extends Applet implements SudokuBoardManager
{
private static SudokuBrdManager myBoard;
private static ButtonGrid button;
private int [][] Board= new int[9][9];
private String output;
public static void main(String[] args)
{
myBoard = new SudokuBrdManager();
try {
//myBoard.setBoard();
} catch (InputOutOfRangeException e) {
TODO Auto-generated catch block
//e.printStackTrace();
} catch (ValueNotValidException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
//System.out.println(myBoard.toString());
}
public void setBoard () throws InputOutOfRangeException, ValueNotValidException
{
JFileChooser chooser = new JFileChooser();
int status;
chooser.setDialogTitle("Select Sudoku Game File");
status = chooser.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION)
{
try
{
File inFile = chooser.getSelectedFile();
myBoard.newGame(inFile);
}
catch(InputMismatchException e)
{
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner scanner = new Scanner(myBoard.toString()).useDelimiter(",|\r\n");
for (int i=0; i < 9; i++)
{
for (int j=0; j < 9; j++)
{
myBoard.setValueAt(i, j, scanner.nextInt());
add(new JTextField(String.valueOf(Board[i][j])));
}
}
}
}
@Override
public void setValueAt(int r, int c, int v) throws InputOutOfRangeException, ValueNotValidException
{
Board[r][c] = v;
}
@Override
public int getValueAt(int r, int c) throws InputOutOfRangeException
{
return 0;
}
@Override
public int[] displayPossibleValues(int r, int c)throws InputOutOfRangeException
{
return null;
}
public String toString()
{
return output;
}
@Override
public void newGame(File gameFile)
{
{
try
{
output = new Scanner(gameFile).useDelimiter("\\Z").next();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}