какой импорт я должен использовать - PullRequest
0 голосов
/ 02 сентября 2018

Я смотрю на старый незаконченный проект Судоку, который я делал некоторое время назад. Я пытаюсь скомпилировать его и сталкиваюсь с некоторыми проблемами. Я думаю, что некоторые утилиты, которые я использовал, устарели или класс импорта изменился. В коде я прокомментировал мои вопросы. SetVisible не работает. Compiler says: Cannot resolve it. То же самое с setDefaultCloseOperation, getcontentpane и setBounds. Я не знаю почему. Эти функции вышли из моды в новой версии Java?

import java.awt.BorderLayout;//in the compiler this was greyed out
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;//this too
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;//this too
import java.awt.GridLayout;
import java.awt.event.ActionEvent;//this too
import java.awt.event.ActionListener;//this too
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;//this too
import java.util.Scanner;
public class Sudoku extends JFrame {
    public JTextField[][] getTextcells() {
        return textcells;
    }//the compiler recommended JtextField be deprecated? Why?
    public void setTextcells(JTextField[][] textcells) {
        this.textcells = textcells;
    }
    public static final int SIZE_OF_GRID = 9;
    public static final int INNER_GRID = 3;
    private JTextField[][] textcells = new JTextField[SIZE_OF_GRID][SIZE_OF_GRID];

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Sudoku frame = new Sudoku();
                frame.setVisible(true);//does not work
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public Sudoku() throws FileNotFoundException  {

    int puzzle[][];
    String Name = "Sudoku.txt";
    File file = new File(Name);
    Scanner inputStream = new Scanner(file);
    puzzle = new int[9][9];
    try{
        while(inputStream.hasNext())
        {
            String line = inputStream.next();
            inputStream = new Scanner(file);
            for(int i = 0; i<9; i++)
                for (int j = 0; j < 9; j++) {
                    line = inputStream.next();
                    puzzle[i][j] = Integer.parseInt(line);
                }
        }
    }
    catch(FileNotFoundException e){
        System.out.println("error");
    }
    inputStream.close();
    Container c = getContentPane();//getcontentpane does not work
    c.setLayout(new GridLayout(SIZE_OF_GRID,SIZE_OF_GRID));
    int row;
    int col;
    boolean[][] clean =
            {{true, true, true, true, true, false, true, false, true},
                    {false, false, true, true, false, true, false, true, true},
                    {true, false, true, false, false, true, true, true, false},
                    {false, false, false, true, false, false, true, true, true},
                    {false, false, false, true, true, true, false, false, false},
                    {true, true, true, false, false, true, false, false, false},
                    {false, true, true, true, false, false, true, false, true},
                    {true, true, false, true, false, true, true, false, false},
                    {true, false, false, true, true, true, true, true, true}};

    for(row = 0; row<SIZE_OF_GRID; ++row){
        for(col = 0; col <SIZE_OF_GRID; ++col){
            textcells[row][col] = new JTextField();
            c.add(textcells[row][col]);
            textcells[row][col].setText(puzzle[row][col] + "");

            if(clean[row][col])
            {
                textcells[row][col].setText(" ");
                textcells[row][col].setEditable(true);
            }
            else{
                textcells[row][col].setText(puzzle[row][col] + " ");
                textcells[row][col].setEditable(false);
            }

        }
    }

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 454, 451);//setbounds and setdefault.... dont work

    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...