我目前正在开发一个数独项目,而且我已经接近完成但我的部分代码存在问题(抱歉变量名称是法语),其中二维数组的行和列索引已经以某种方式进行了交换,我似乎无法找到它的来源。这是数组声明:
int[][]sdkGrid = {
{5,3,0,0,7,0,0,0,0},
{6,0,0,1,9,5,0,0,0},
{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},
{4,0,0,8,0,3,0,0,1},
{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},
{0,0,0,4,1,9,0,0,5},
{0,0,0,0,8,0,0,7,9}};
以下是以某种方式交换两个索引的代码: 问题是我不知道两个索引的交换位置。
public class FenetreGrille extends JFrame implements ActionListener{
public final int [][] NOMBRES_DEBUT;
public int [][] GRILLE_MODIF = new int[9][9];
public int [][] GRILLE_FINALE = new int[9][9];
public final int TAILLE = 9; //Mettre maj sur taille etc car ce sont des constantes
public final int TAILLECASE;
public final int COTEGRILLE ;
public Grille1 Sdk;
public JPanel Container1;
public JPanel Container2;
public JPanel containermain;
public JTextField [][] cases= new JTextField [9][9];
public JLabel FOND_ECRAN;
public JButton test = new JButton("test");
public FenetreGrille(int [][] NOMBRES_DEBUT){
this.TAILLECASE = 60;
this.NOMBRES_DEBUT = NOMBRES_DEBUT;
this.GRILLE_MODIF = NOMBRES_DEBUT;
Sdk = new Grille1(NOMBRES_DEBUT);
COTEGRILLE = TAILLE * TAILLECASE;
//this.setUndecorated(true); //Remove Title Bar AND EXIT BUTTON SO ADD ONE!!!!!
this.setAlwaysOnTop(true); //This is always on top
this.setResizable(false);
Toolkit tk = Toolkit.getDefaultToolkit();
int xsize = (int) tk.getScreenSize().getWidth();
int ysize = (int) tk.getScreenSize().getHeight();
this.setSize(xsize, ysize);
Container1 = new JPanel(); //Initialisation de la grille de Sudoku
Container1.setLayout(null);
Container1.setBounds((this.getWidth()/2)-(9*TAILLECASE/2),(this.getHeight()/2)-(9*TAILLECASE/2),COTEGRILLE,COTEGRILLE);
Container1.setBackground(Color.white);
for(int li = 0; li<TAILLE; li++){ // remplissage de la grille
for (int col = 0; col<TAILLE; col++){
cases[li][col] = new JTextField(Integer.toString(NOMBRES_DEBUT[li][col]));
cases[li][col].setBounds(li*TAILLECASE,col*TAILLECASE,TAILLECASE,TAILLECASE);
cases[li][col].setHorizontalAlignment(JTextField.CENTER);
cases[li][col].getDocument().addDocumentListener(new Ecouteur());
cases[li][col].getDocument().putProperty ("owner", cases[li][col]);
Container1.add(cases[li][col]);
if( li == 2 || li == 5){
if(col == 2 || col == 5){
Border border = BorderFactory.createMatteBorder(1,1,4,4,new Color(0, 0, 0));
cases[li][col].setBorder(border);
}else{
Border border = BorderFactory.createMatteBorder(1,1,1,4,new Color(0, 0, 0));
cases[li][col].setBorder(border);
}
}else if (col == 2 || col == 5){
Border border = BorderFactory.createMatteBorder(1,1,4,1,new Color(0, 0, 0));
cases[li][col].setBorder(border);
}else{
Border border = BorderFactory.createMatteBorder(1,1,1,1,new Color(0, 0, 0));
cases[li][col].setBorder(border);
}
if(NOMBRES_DEBUT[li][col] == 0){
cases[li][col].setText("");
cases[li][col].setBackground(new Color(204,204,204));
}else{
cases[li][col].setEditable( false);
}
}
}
int xButton = 200;
int yButton = (int) ((xButton*3)/4);
ImageIcon woodTemp = new ImageIcon("C:\\Users\\Maxime\\Documents\\Perso\\Post-Bac\\INSA\\Projet Info\\unfinished-bamboo-flooring-ufc214.jpg");
Image newImg = woodTemp.getImage();
ImageIcon woodButton = new ImageIcon(newImg.getScaledInstance(xButton, yButton, Image.SCALE_SMOOTH));
/*
test.setIcon(woodButton);
test.setBorder(null);
test.setMaximumSize(new Dimension(xButton, yButton));
test.setOpaque(false);
test.setContentAreaFilled(false);
test.setBorderPainted(false);
*/
test.setBounds((xsize/4)-100, (ysize/4)-75, xButton, yButton);
test.addActionListener(this);
containermain = new JPanel();
containermain.setBounds(0,0, xsize, ysize);
containermain.setOpaque(false);
containermain.setLayout(null);
FOND_ECRAN = new JLabel ();
FOND_ECRAN.setBounds(0, 0, 1920, 1080);
containermain.add(FOND_ECRAN);
containermain.add(test);
int x = FOND_ECRAN.getWidth();
ImageIcon temp = new ImageIcon("C:\\Users\\Maxime\\Documents\\Perso\\Post-Bac\\INSA\\Projet Info\\nature-1520704451453-7117.jpg");
Image img = temp.getImage();
Image dimg = img.getScaledInstance(FOND_ECRAN.getWidth(), FOND_ECRAN.getHeight(), Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(dimg);
FOND_ECRAN.setIcon(imageIcon);
setContentPane(containermain);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
JMenuItem item = new JMenuItem("Exit");
item.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(item);
this.setJMenuBar(menuBar);
containermain.add(Container1);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class Ecouteur implements DocumentListener{
public void changedUpdate(DocumentEvent e){
action(e);
}
public void insertUpdate(DocumentEvent e) {
action(e);
}
public void removeUpdate(DocumentEvent e){
//action(e);
}
public void action (DocumentEvent e){
for(int i = 0; i<TAILLE; i++){
for (int j = 0; j<TAILLE; j++){
if(e.getDocument().getProperty("owner") == cases[i][j]){
String s = cases[i][j].getText(); //Enlever tous les i,j, n et leur donner un nom
int n = Integer.parseInt(s); // Remplace la string s en un nombre
if(n < 1 || n > 9){
JOptionPane.showMessageDialog(containermain, " entrez un nombre entre 1 et 9");
}else{
cases[i][j].setBackground(Color.white);
GRILLE_MODIF[i][j] = n;
affichage(GRILLE_MODIF);
}
}
}
}
//Verification if the grid is complete
}
}
public void append ( String s){}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b) {
this.dispose();
}else if(e.getSource() == test) {
SudokuBackTrack sbt = new SudokuBackTrack(NOMBRES_DEBUT);
GRILLE_FINALE = sbt.grille;
for(int i = 0; i < TAILLE; i++) {
for(int j = 0; j < TAILLE; j++) {
cases[i][j].setText(Integer.toString(GRILLE_FINALE[i][j]));
}
}
}
}
public void affichage(int[][] t) {
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
System.out.print(t[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}
答案 0 :(得分:0)
我认为你的违规行是:
cases[li][col].setBounds(li*TAILLECASE,col*TAILLECASE,TAILLECASE,TAILLECASE);
要修复,请将其更改为:
cases[li][col].setBounds(col*TAILLECASE,li*TAILLECASE,TAILLECASE,TAILLECASE);
setBounds参数是setBounds(x,y,width,height)。 li(垂直线索引)应对应于y,col应对应于x坐标。
我在GUI中管理这样的数组时使用的一种编码实践是明确地命名我的迭代变量x和y,这样我就不会感到困惑。