gpt4 book ai didi

java - 从组件更改 JLabel ICON

转载 作者:行者123 更新时间:2023-12-01 15:56:14 25 4
gpt4 key购买 nike

所以我有一个 JLabels 网格,随机分配背景颜色和图标来匹配(Bubble Breaker 游戏的气泡),当我使用 findComponentAt(getX(), getY()) 来获取组件我点击....这是因为我想找出我被点击的标签并将其图标更改为不同的图标,但是组件API似乎没有 setIcon() 方法,关于如何操作的任何建议我可以更改图标吗?

下面给出了我的一个文件的代码,但很困惑,因为我显然仍在开发中,所以很抱歉。

代码粘贴得很奇怪......:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.Random;
import java.awt.Color.*;
import java.awt.Cursor.*;
import javax.swing.border.*;
import java.awt.event.*;

public class GridForGame extends JPanel {
JTextField myNumberArray[] = new JTextField[16];
MouseHandling handler = new MouseHandling();

JLabel statusbar = new JLabel("default");
JLabel tempLabel = new JLabel();

JPanel squareContainer = new JPanel();
Boolean handlerOn = false;
boolean colourChanged = false;

ImageIcon redBubble;
ImageIcon blueBubble;
ImageIcon yellowBubble;
ImageIcon orangeBubble;
ImageIcon greenBubble;
ImageIcon pinkBubble;
ImageIcon purpleBubble;


public void smallIcons(){
redBubble = new ImageIcon("images/redSmall.jpg");
blueBubble = new ImageIcon("images/blueSmall.jpg");
yellowBubble = new ImageIcon("images/yellowSmall.jpg");
orangeBubble = new ImageIcon("images/orangeSmall.jpg");
greenBubble = new ImageIcon("images/greenSmall.jpg");
pinkBubble = new ImageIcon("images/pinkSmall.jpg");
purpleBubble = new ImageIcon("images/purpleSmall.jpg");
}

public void mediumIcons(){
redBubble = new ImageIcon("images/redMedium.jpg");
blueBubble = new ImageIcon("images/blueMedium.jpg");
yellowBubble = new ImageIcon("images/yellowMedium.jpg");
orangeBubble = new ImageIcon("images/orangeMedium.jpg");
greenBubble = new ImageIcon("images/greenMedium.jpg");
pinkBubble = new ImageIcon("images/pinkMedium.jpg");
}

public void largeIcons(){
redBubble = new ImageIcon("images/redLarge.jpg");
blueBubble = new ImageIcon("images/blueLarge.jpg");
yellowBubble = new ImageIcon("images/yellowLarge.jpg");
orangeBubble = new ImageIcon("images/orangeLarge.jpg");
greenBubble = new ImageIcon("images/greenLarge.jpg");
pinkBubble = new ImageIcon("images/pinkLarge.jpg");
}

public GridForGame(){
setLayout(new BorderLayout());
setBackground(Color.gray);
setBorder(new EtchedBorder(4, Color.blue, Color.blue));
setPreferredSize(new Dimension(600,600));
}

public void small(){

smallIcons();
/*
* Below empties the main game view so that
* more can be 'painted' on it.
*/
squareContainer.removeAll();
revalidate();

// Set the grid for 8 x 8
squareContainer.setLayout(new GridLayout(8,8));
squareContainer.setPreferredSize(new Dimension(500,500));
squareContainer.setMaximumSize(new Dimension(500,500));

/*
* Checks if a Mouse handler has already been
* assigned to game view, if not, Add one!
*/
if(!handlerOn){
squareContainer.addMouseListener(handler);
handlerOn = true;
}

statusbar.setText("default");
add(statusbar, BorderLayout.NORTH);

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

for(int j=0; j<8; j++){
JLabel number = new JLabel();

number.setEnabled(true);
number.setOpaque(true);
number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));

for(int k=0; k<8; k++){
int n = new Random().nextInt(7);
switch(n){
case 0: number.setBackground(Color.red);
number.setIcon(redBubble); break;
case 1: number.setBackground(Color.cyan);
number.setIcon(orangeBubble); break;
case 2: number.setBackground(Color.blue);
number.setIcon(blueBubble); break;
case 3: number.setBackground(Color.green);
number.setIcon(greenBubble); break;
case 4: number.setBackground(Color.yellow);
number.setIcon(yellowBubble); break;
case 5: number.setBackground(Color.magenta);
number.setIcon(pinkBubble); break;
default:
}
}

squareContainer.add(number);
}

//squareContainer.add(squares);
}
add(squareContainer);
revalidate();
}

public void medium(){

mediumIcons();

/*
* Below empties the main game view so that
* more can be 'painted' on it.
*/
squareContainer.removeAll();
revalidate();

// Set the grid for 12 x 12
squareContainer.setLayout(new GridLayout(12,12));
squareContainer.setPreferredSize(new Dimension(500,500));
squareContainer.setMaximumSize(new Dimension(500,500));

/*
* Checks if a Mouse handler has already been
* assigned to game view, if not, Add one!
*/
if(!handlerOn){
squareContainer.addMouseListener(handler);
handlerOn = true;
}

statusbar.setText("default");
add(statusbar, BorderLayout.NORTH);

for(int i=0; i<12; i++){
JPanel squares = new JPanel();
squares.setLayout(new GridLayout(1,1));
for(int j=0; j<12; j++){
JLabel number = new JLabel();
number.setEnabled(true);
number.setOpaque(true);
number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));

squares.add(number);
for(int k=0; k<12; k++){
int n = new Random().nextInt(7);
switch(n){
case 0: number.setBackground(Color.red);
number.setIcon(redBubble); break;
case 1: number.setBackground(Color.cyan);
number.setIcon(orangeBubble); break;
case 2: number.setBackground(Color.blue);
number.setIcon(blueBubble); break;
case 3: number.setBackground(Color.green);
number.setIcon(greenBubble); break;
case 4: number.setBackground(Color.yellow);
number.setIcon(yellowBubble); break;
case 5: number.setBackground(Color.magenta);
number.setIcon(pinkBubble); break;
default:
}
}
}

squareContainer.add(squares);
}
add(squareContainer);
revalidate();
}

public void large(){

largeIcons();

/*
* Below empties the main game view so that
* more can be 'painted' on it.
*/
squareContainer.removeAll();
revalidate();

// Set the grid for 16 x 16
squareContainer.setLayout(new GridLayout(16,16));
squareContainer.setPreferredSize(new Dimension(500,500));
squareContainer.setMaximumSize(new Dimension(500,500));

/*
* Checks if a Mouse handler has already been
* assigned to game view, if not, Add one!
*/
if(!handlerOn){
squareContainer.addMouseListener(handler);
handlerOn = true;
}

statusbar.setText("default");
add(statusbar, BorderLayout.NORTH);

for(int i=0; i<16; i++){
JPanel squares = new JPanel();
squares.setLayout(new GridLayout(1,1));
for(int j=0; j<16; j++){
JLabel number = new JLabel();
number.setEnabled(true);
number.setOpaque(true);
number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));

squares.add(number);
for(int k=0; k<16; k++){
int n = new Random().nextInt(7);
switch(n){
case 0: number.setBackground(Color.red);
number.setIcon(redBubble); break;
case 1: number.setBackground(Color.cyan);
number.setIcon(orangeBubble); break;
case 2: number.setBackground(Color.blue);
number.setIcon(blueBubble); break;
case 3: number.setBackground(Color.green);
number.setIcon(greenBubble); break;
case 4: number.setBackground(Color.yellow);
number.setIcon(yellowBubble); break;
case 5: number.setBackground(Color.magenta);
number.setIcon(pinkBubble); break;
default:
}
}
}

squareContainer.add(squares);
}
add(squareContainer);
revalidate();
}

private class MouseHandling implements MouseListener{
public void mouseClicked(MouseEvent event){
statusbar.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
System.out.println(String.format("Component clicked was " + findComponentAt(event.getX(), event.getY()).getBackground()));

/*JLabel temp = new JLabel();

temp = findComponentAt(event.getX(), event.getY());*/

if(colourChanged == true){
findComponentAt(event.getX(), event.getY()).setBackground(Color.white);
System.out.println("selected label now set to white");
}

if(colourChanged == false){
findComponentAt(event.getX(), event.getY()).setBackground(Color.black);
System.out.println("selected label now set to black");
colourChanged = true;
}
}

public void mousePressed(MouseEvent event){
statusbar.setText("anything");
}

public void mouseReleased(MouseEvent event){
statusbar.setText(String.format("Released at %d,%d", event.getX(), event.getY()));
}

public void mouseEntered(MouseEvent event){
statusbar.setText("Entered");
}

public void mouseExited(MouseEvent event){
statusbar.setText("Exited");
}
}

}

最佳答案

通过instanceof运算符检查组件是否是JLabel,如果为假,则退出鼠标监听器方法,否则如果为真,则转换为JLabel并设置其图标。例如:

public void mouseClicked(MouseEvent event){
statusbar.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
System.out.println(String.format("Component clicked was " + findComponentAt(event.getX(), event.getY()).getBackground()));

Component comp = findComponentAt(event.getX(), event.getY());
if (!(comp instanceof JLabel)) {
return; // if it's not a JLabel get out of this method
}

// it is a JLabel so cast it.

JLabel temp = (JLabel) comp;

// now you can use the JLabel

关于java - 从组件更改 JLabel ICON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4987408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com