根据点击次数交换两个JLabel

时间:2012-11-11 19:52:51

标签: jpanel jlabel swap

如何使用此代码单击JPanel中的一个JLabel,然后在单独的面板中再次单击另一个JLabel并让两个JLabel交换?我正在创建一个面板网格,每个面板都包含一个JLabel和一个图像Icon。我想点击一个图标,然后点击另一个面板,让内容交换。我做错了什么?

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();
         }
      });
   }
}

1 个答案:

答案 0 :(得分:0)

我制作了自己的代码,试图找出你想要做的事情......这是我想出来的,我希望它有所帮助......虽然我注意到这是在一个月前发布的...... < / p>

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添加一个鼠标侦听器。现在,当用户点击其中一个时,它将检查之前是否已经点击了另一个,如果没有,则获取该标签文本并将其添加到临时字符串。运行它,看看它是怎么回事