0

テキスト ファイル、2D 配列、および toString(); から 9x9 グリッドを表示しようとしています。うまくいきますが、グリッドがポップアップしない理由がわかりません。別のクラスから非常に単純なイベントボタンを呼び出して、それが機能するかどうかを確認しようとしましたが、まだ何も得られません(メインメソッドから呼び出しています)。ただし、この単純なイベント ボタンを他のクラスで実行すると正常に動作しますが、なぜ動作しないのかわかりません。

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

1 に答える 1

2

static void main()アプレットでは機能しないため、標準のアプリケーションとはエントリ ポイントとライフ サイクルが異なります。初期化ロジックを に移動しますvoid init()

http://docs.oracle.com/javase/6/docs/api/java/applet/Applet.htmlおよびhttps://www.google.com/search?q=java+applet+lifecycleを参照してください。

また、ここでは AWT と Swing を混在させています。Applet の代わりに JApplet を使うべきだと思います。

于 2012-10-09T17:47:00.417 に答える