如何使用此代码单击 JPanel 中的一个 JLabel,然后再次单击单独面板中的另一个 JLabel 并交换两个 JLabel?我正在创建一个面板网格,每个面板都包含一个带有图像图标的 JLabel。我想单击一个图标,然后单击另一个面板并交换内容。我究竟做错了什么?
package testcomponentat;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class TestComponentAt extends JPanel {
private static final int ROW_COUNT = 8;
private static final int W = 60;
private static final int H = W;
private static final Dimension PREF_SIZE = new Dimension(W, H);
protected static final Color SELECTION_COLOR = Color.pink;
private JPanel selectedPanel = null;
private Color originalColor = null;
private static int r, c = 0;
private static final ImageIcon im1 = new ImageIcon("image1.jpg");
private static final ImageIcon im2 = new ImageIcon("image2.jpg");
private static boolean swap = false;
private static JPanel panel1, panel2;
public TestComponentAt() {
setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));
setBackground(Color.black);
for (int i = 0; i < ROW_COUNT * ROW_COUNT; i++) {
JPanel panel = new JPanel();
JLabel jlab = new JLabel();
String name = String.format("[%d, %d]", i / ROW_COUNT, i % ROW_COUNT);
panel.setName(name);
if (i == 0) {
originalColor = panel.getBackground();
}
r = i % ROW_COUNT;
c = i % ROW_COUNT;
if(c == 1){ jlab.setIcon(im1); }
if(c == 6){ jlab.setIcon(im2); }
panel.add(jlab);
panel.setPreferredSize(PREF_SIZE);
add(panel);
}
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(swap == false){
panel1 = (JPanel) getComponentAt(e.getPoint());
//panel1 = (JPanel) e.getComponent();
if (panel1 == null || panel1 == TestComponentAt.this) {
return;
}
if (selectedPanel != null) {
selectedPanel.setBackground(originalColor);
//selectedPanel.removeAll();
selectedPanel.revalidate();
selectedPanel.repaint();
}
selectedPanel = panel1;
selectedPanel.setBackground(SELECTION_COLOR);
selectedPanel.add(new JLabel(selectedPanel.getName()));
selectedPanel.revalidate();
selectedPanel.repaint();
swap = true;
}
else{
JPanel temp = new JPanel();
panel2 = (JPanel) getComponentAt(e.getPoint());
if (panel2 == null || panel2 == TestComponentAt.this) {
return;
}
if (selectedPanel != null) {
selectedPanel.setBackground(originalColor);
//selectedPanel.removeAll();
selectedPanel.revalidate();
selectedPanel.repaint();
}
selectedPanel = panel2;
selectedPanel.setBackground(SELECTION_COLOR);
selectedPanel.add(new JLabel(selectedPanel.getName()));
temp = panel1;
panel1 = panel2;
panel2 = panel1;
selectedPanel.revalidate();
selectedPanel.repaint();
swap = false;
}
}
});
}
private static void createAndShowGui() {
JFrame frame = new JFrame("TestComponentAt");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestComponentAt());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGui();
}
});
}
}