数独:不出现选择图像

时间:2015-05-22 05:18:08

标签: java imageicon sudoku

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

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);

}
}

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

2 个答案:

答案 0 :(得分:0)

  1. 尝试将程序简化为最简单的程序,以重现问题。你可能会在这个过程中发现问题,但如果没有,你会有一个很容易理解的明显例子。

  2. 单步执行程序,看看它是否符合您的预期。

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

  4. 如果您遇到任何特定的问题,请随时提出具体问题。但是现在,你基本上都在问“如何调试程序?”

答案 1 :(得分:0)

我发现布局图层是我所期望的。您首先添加的标签将始终位于顶部。在原始版本开启后,JComponents添加了该标签。基本上,在代码中较早的时候,您将组件添加到JFrame中,Component的优先级越高。