数独:选择图像不出现

Sudoku: Selection image does not appear

我正在构建数独游戏。到目前为止我已经绘制了一个网格并编程了一个字段的选择,但是我选择的用于选择的图片没有出现。我的 Class 选择器是:

package com.brendenbunker;

import javax.swing.*;

public class Selection {

public JLabel boxSelected;
public ImageIcon selected;
int x, y;

public Selection(){

    x = 0;
    y = 0;
    selected = new ImageIcon(getClass().getResource("/Selected.png"));

    boxSelected = new JLabel("");
    boxSelected.setIcon(selected);
    boxSelected.setBounds((x * (selected.getIconWidth() + 4) + (x / 3) * 4) + 4, (y * (selected.getIconWidth() + 4) + (y / 3) * 4) + 4, selected.getIconWidth(), selected.getIconHeight());
}

public Selection(int x, int y){

    this.x = x;
    this.y = y;
    selected = new ImageIcon(getClass().getResource("/Selected.png"));

    boxSelected = new JLabel("");
    boxSelected.setIcon(selected);
    boxSelected.setBounds((x * (selected.getIconWidth() + 4) + (x / 3) * 4) + 4, (y * (selected.getIconWidth() + 4) + (y / 3) * 4) + 4, selected.getIconWidth(), selected.getIconHeight());
}

public void setNewSelection(int x, int y) {
    this.x = x;
    this.y = y;
    boxSelected.setBounds((x * (selected.getIconWidth() + 4) + (x / 3) * 4) + 4, (y * (selected.getIconWidth() + 4) + (y / 3) * 4) + 4, selected.getIconWidth(), selected.getIconHeight());
}
}

显示所有内容的代码是:

package com.brendenbunker;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class ScreenGenerator extends JFrame{

//Intro Components
//JLabel temp;
JLabel[] gridLabel, numbLabel, numbBackLabel;
JLabel[][] numbDisp;
ImageIcon gridPic, numbPic, numbBackPic;
Rectangle[][] boxArea;
Selection selection;
Random random;
//intro Vars


public ScreenGenerator() {

    setLayout(null);

    random = new Random();
    selection = new Selection();
    gridPic = new ImageIcon(getClass().getResource("/Grid_Unified.png"));
    numbBackPic = new ImageIcon(getClass().getResource("/Square.png"));
    gridLabel = new JLabel[9];
    numbLabel = new JLabel[9];
    numbBackLabel = new JLabel[9];
    boxArea = new Rectangle[9][9];
    numbDisp = new JLabel[9][9];

    for (int i=0; i<9; i++) {
        gridLabel[i] = new JLabel("");
            gridLabel[i].setBounds(((i+1)%3)*gridPic.getIconWidth(),Math.round(i/3)*gridPic.getIconHeight(),gridPic.getIconWidth(),gridPic.getIconHeight());
        numbBackLabel[i] = new JLabel("");
        numbBackLabel[i].setBounds(i*numbBackPic.getIconWidth()+1,gridPic.getIconHeight()*3,numbBackPic.getIconWidth(),numbBackPic.getIconHeight());
        numbLabel[i] = new JLabel("");
        numbLabel[i].setBounds(i*numbBackPic.getIconWidth(),gridPic.getIconHeight()*3,numbBackPic.getIconWidth(),numbBackPic.getIconHeight());
        for (int j=0; j<9; j++) {
            numbDisp[i][j] = new JLabel("");
            numbDisp[i][j].setBounds((j * (selection.selected.getIconWidth() + 4) + (j / 3) * 4) + 4, (i * (selection.selected.getIconWidth() + 4) + (i / 3) * 4) + 4, selection.selected.getIconWidth(), selection.selected.getIconHeight());
            boxArea[j][i] = new Rectangle((j*(selection.selected.getIconWidth()+4)+(j/3)*4)+4,(i*(selection.selected.getIconWidth()+4)+(i/3)*4)+4,selection.selected.getIconWidth(),selection.selected.getIconHeight());

            add(numbDisp[i][j]);
        }
    }

    for (int i=0; i<9; i++) {

        numbPic = new ImageIcon(getClass().getResource("/numb_" + (i+1) + ".png"));
        numbLabel[i].setIcon(numbPic);
        gridLabel[i].setIcon(gridPic);
        numbBackLabel[i].setIcon(numbBackPic);

        add(selection.boxSelected);
        add(gridLabel[i]);
        add(numbLabel[i]);
        add(numbBackLabel[i]);

    }

    setBoxNumb(random.nextInt(9)+1,random.nextInt(9)+1,random.nextInt(9)+1);
    selection.setNewSelection(1,2);

}

public void setBoxNumb(int x, int y, int numb){

    numbPic = new ImageIcon(getClass().getResource("/numb_" + numb + ".png"));
    numbDisp[x - 1][y - 1].setIcon(numbPic);

}
}

所以我想问的是,如果选择了一个字段,为什么我想要显示的图像没有出现?有谁知道如何解决这个问题?

  1. 尝试将您的程序简化为能够重现问题的最简单程序。您可能会在此过程中发现问题,但如果没有,您将有一个清晰易懂的示例。

  2. 单步执行您的程序以查看其行为是否符合您的预期。

  3. 向您的程序添加日志记录以查看其行为是否符合您的预期。

如果您遇到任何具体问题,请随时提出具体问题。但是现在,你基本上是在问 "how do I debug a program?"

我发现布局层与我预期的相反。您首先添加的标签将始终位于顶部。在原始标签打开后,该标签不会被 JComponents add 覆盖。所以基本上,在代码中越早将组件添加到 JFrame,组件的优先级就越高。