我应该使用什么进口

时间:2018-09-02 20:56:35

标签: java swing

我正在看我前一段时间做的一个未完成的Sudoku旧项目。我正在尝试将其编译,并且遇到了一些问题。我认为我使用的某些实用程序已过期或导入类已更改。在代码中,我评论了我的问题。 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

    }
}

0 个答案:

没有答案