我有一个程序通过GridBagLayout布局管理器显示一个4x4的正方形网格。显示包含square.gif的16个JLabel。当点击其中一个矩形时,应该用包含图像的JLabel(例如帽子)替换它。因此,图像取代了单击的矩形。
然而,目前发生的事情是,点击的矩形有时会被替换。其他时候,矩形消失但图像不会取代它。其他时候,图像显示在之前单击过的矩形中,但仅在单击其他矩形后才显示。我在下面放置了最相关的代码。
public void displayGrid() {
c.gridx = 0;
c.gridy = 0;
try {
squareImage = ImageIO.read(this.getClass().getResource("stimulus(0).gif")); //line 37
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel squareLabel = new JLabel(new ImageIcon(squareImage));
for(int i = 0; i < 16; i++){
c.gridx = i % 4;
c.gridy = i / 4;
squareLabel = new JLabel(new ImageIcon(squareImage));
squareLabels[i] = squareLabel;
panel.add(squareLabels[i], c);
squareLabels[i].addMouseListener(this);
System.out.println(c.gridx + "" + c.gridy);
}
panel.validate();
}
public void mousePressed(MouseEvent e) {
for(int i = 0; i < squareLabels.length; i++){
if(e.getSource() == squareLabels[i]){
//JLabel removedLabel = squareLabels[i];
c.gridx = (i/4);
c.gridy = (i%4);
panel.remove(squareLabels[i]);
panel.revalidate();
panel.repaint();
panel.add(stimuliLabels[0], c);
panel.validate();
}
}
}
在mousePressed()方法中,我试图编写确定按下的JLabel的代码,获取该JLabel的GridBagConstraints,删除单击的JLabel,然后用新的JLabel替换该JLabel。给出了GridBagConstraints。但是,正如我已经说过的那样,该计划没有按计划运作,我不知道为什么。
感谢您抽出宝贵时间阅读本文。任何帮助将不胜感激。
答案 0 :(得分:5)
为什么要交换JLabels? JLabels用于保存图标,通常是ImageIcons,如果要交换图像,最好将JLabel保留在适当位置,只需交换显示的ImageIcon即可通过调用setIcon(...)
轻松完成。这比您尝试做的要容易得多,而且这适用于图书馆,而不是像您尝试做的那样。
public void mousePressed(MouseEvent e) {
// assuming that only JLabels are given this MouseListener:
JLabel label = (JLabel) e.getSource();
label.setIcon(desiredNewIcon);
}
例如,从我对similar question的回答:
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class RandomChessMen extends JPanel {
// for this example I get a sprite sheet that holds several sprite images in it
// the images can be found here: https://stackoverflow.com/questions/19209650
private static final String IMAGE_PATH = "http://i.stack.imgur.com/memI0.png";
private static final int LABEL_COUNT = 2;
private static final int ICON_COLUMNS = 6;
private Random random = new Random();
public RandomChessMen() throws IOException {
URL url = new URL(IMAGE_PATH);
BufferedImage largeImg = ImageIO.read(url);
setLayout(new GridLayout(1, 0));
// break down large image into its constituent sprites and place into ArrayList<Icon>
int w = largeImg.getWidth() / ICON_COLUMNS;
int h = largeImg.getHeight() / LABEL_COUNT;
for (int i = 0; i < LABEL_COUNT; i++) {
final List<Icon> iconList = new ArrayList<>();
int y = (i * largeImg.getHeight()) / LABEL_COUNT;
// get 6 icons out of large image
for (int j = 0; j < ICON_COLUMNS; j++) {
int x = (j * largeImg.getWidth()) / ICON_COLUMNS;
// get subImage
BufferedImage subImg = largeImg.getSubimage(x, y, w, h);
// create ImageIcon and add to list
iconList.add(new ImageIcon(subImg));
}
// create JLabel
final JLabel label = new JLabel("", SwingConstants.CENTER);
int eb = 40;
label.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
// get random index for iconList
int randomIndex = random.nextInt(iconList.size());
Icon icon = iconList.get(randomIndex); // use index to get random Icon
label.setIcon(icon); // set label's icon
label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Icon secondIcon = label.getIcon();
// so we don't repeat icons
while (label.getIcon() == secondIcon) {
int randomIndex = random.nextInt(iconList.size());
secondIcon = iconList.get(randomIndex);
}
label.setIcon(secondIcon);
}
});
// add to GUI
add(label);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("RandomImages");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
frame.getContentPane().add(new RandomChessMen());
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}