数独:不会出现选择图像

问题描述:

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

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

} 
} 

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

+0

你忘了问一个问题。你需要什么帮助?要尽可能具体。 –

+0

我编辑了它。谢谢 – Nuiofrd

+0

为什么不在'JPanel'上绘画? – Sybren

我发现的是,布局图层倒退到我期望的位置。您首先添加的标签将始终保持最佳状态。在原始文件打开后,JComponents添加的标签将不会被绘制。因此,基本上,在向JFrame添加组件的代码中,Component的优先级越高。

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

  2. 单步执行程序,查看它是否按预期行事。

  3. 将日志记录添加到您的程序中,查看它是否按预期行事。

如果您遇到任何具体问题,随便问一个具体问题。但是现在,你基本上在问“如何调试程序?”

+0

好吧,我会试试这个。 – Nuiofrd