1

如何使用此代码单击 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();
         }
      });
   }
}
4

1 回答 1

0

我编写了自己的代码来尝试弄清楚您要做什么...这是我想出的,希望对您有所帮助...尽管我只是注意到这是一个月前发布的...

Java 文件名“SplashScreen.java”:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

public class SplashScreen extends JFrame {

JPanel[] p1 = new JPanel[9];
JPanel mainP = new JPanel();
JLabel[] l1 = new JLabel[9];
boolean[] l1Clicked = new boolean[9];
boolean bufferFull = false;
String temp;

public SplashScreen() {
    createSplash();
}

public void createSplash() {
    mainP.setLayout(new FlowLayout());
    mouseHandler mh = new mouseHandler();
    for (int i = 0; i < p1.length; i++) {
        l1[i] = new JLabel("" + i);
        p1[i] = new JPanel();
        p1[i].add(l1[i]);
        p1[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        p1[i].addMouseListener(mh);
        l1Clicked[i] = false;
        mainP.add(p1[i]);
    }
    add(mainP);
    //add(p2);
}

public class mouseHandler extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == p1[0]) {
            if (bufferFull == false) {
                temp = l1[0].getText();
                bufferFull = true;
                l1Clicked[0] = true;
            } else {
                for (int i = 0; i < l1Clicked.length; i++) {
                    if (l1Clicked[i] == true) {
                        l1[i].setText(l1[0].getText());
                        l1Clicked[i] = false; 
                        bufferFull = false;
                    }

                }
                l1[0].setText(temp);

            }
        } else if (e.getSource() == p1[1]) {
            if (bufferFull == false) {
                temp = l1[1].getText();
                bufferFull = true;
                l1Clicked[1] = true;
            } else {
                for (int i = 0; i < l1Clicked.length; i++) {
                    if (l1Clicked[i] == true) {
                        l1[i].setText(l1[1].getText());
                        l1Clicked[i] = false;
                        bufferFull = false;
                    }
                }
                l1[1].setText(temp);

            }
        } else {
            System.out.println("You get the point");
        }



    }
}
}

Java 文件名“ImageTest.java”:

import javax.swing.JFrame;


public class ImageTest extends JFrame {

public static void main(String[] args) {
    SplashScreen ss = new SplashScreen();
    ss.setVisible(true);
    ss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ss.setSize(500,500);
}
}

工作正常!:) 简单地制作了两个数组。一个用于 JPanel,另一个用于 JLabels。您为每个 JPanel 添加一个鼠标侦听器。现在,当用户单击一个时,它将检查之前是否单击过另一个,如果没有,它会获取标签文本并将其添加到临时字符串中。运行它,看看它是怎么回事

于 2012-12-21T08:56:55.703 回答