NETBEANS를 사용 중이며 의사 결정/부울을 기반으로 특정 아이콘/이미지를 표시해야하는 여러 개의 JLabel이 있습니다. 모든 JLabel에 마우스 수신기를 추가 한 다음 각 JLabel에 대한 코드를 복사하여 붙여 넣지 않아도됩니다. 오히려 나는 x, y를 JLabel의 이름으로 사용하고 x, y를 기반으로 아이콘을 설정하는 것을 선호합니다. 나는 XY를 받고 아무런 문제가 없지만이 (xy.setIcon 같은 (새 이미지 아이콘을 (히트))하는 방법을 알아낼 수 없습니다. 여기 내 코드입니다 마우스 클릭의 x, y를 기반으로 동적으로 JLabel 아이콘을 변경하십시오.
/**
* Mathematical coordinates of player1Fleet.
* Used to realign ships from Ship Class
* for use on game board.
*/
public void mouseClicked(MouseEvent e) {
Launch1();
}
public void FleetP1() {
for (Ship s : player1Fleet) {
int size = s.getSize();
for (int i = 0; i < size; i++) {
player1Ships.add((((s.getXCoordinate(i) + 1) * 45) + 90) + "" + (((s.getYCoordinate(i) + 1) * 45) + 180));
}
}
// Verification of Math
System.out.println(player1Ships);
}
/**
* Determine Hit or Miss based on location of Cross-hairs
* for player 1/West on game board.
* @return
*/
//public boolean setStrike1(){
public boolean Launch1() {
w93.setIcon(null);
player1Ships.clear();
FleetP1();
boolean strike1 = false;
boolean Launch = false;
for (int i = 0; i < this.player1Ships.size(); i++) {
if (this.player1Ships.get(i).equals(LblCrossHairs.getX() + "" + LblCrossHairs.getY())) {
strike1 = true;
//break;
}
if (strike1) {
strike1 = true;
(LblCrossHairs.getX() + "" + LblCrossHairs.getY()).setIcon(new ImageIcon(Hit));
//Launch = theAttack.Strike1(strike1);
//w93.setIcon(new ImageIcon(Hit));
URL url = this.getClass().getResource("MissleHit.au");
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.println("HIT");
break;
} else {
strike1 = false;
(LblCrossHairs.getX() + "" + LblCrossHairs.getY()).setIcon(new ImageIcon(Hit));
//w93.setIcon(new ImageIcon(Miss));
URL url = this.getClass().getResource("MissileMiss.au");
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.println("MISS");
}
}
TxtClick.setText(LblCrossHairs.getX() + "" + LblCrossHairs.getY() + ".setIcon");
return strike1;
}
가 사전에 감사합니다 모든 도움이됩니다. 이것은 지난 2 일
* 어떤 질문입니까? * 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –