【问题标题】:Adding a MouseListener or MouseMotionListener to my JLabel that is inside of a Jpanel in my checkers-like board将 MouseListener 或 MouseMotionListener 添加到我的 JLabel 中,该 JLabel 位于类似棋盘的 Jpanel 内
【发布时间】:2012-12-02 12:00:09
【问题描述】:

我真的不知道该怎么办。我首先创建了一个实现 MouseMotionListener 和 MouseListener 的新类,并接受其构造函数 JLabel:

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;

public class motion implements MouseMotionListener, MouseListener {

    motion(JLabel im) {
        label1 = im;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == label1) {
            drag = true;
        } 
    } 

    @Override
    public void mouseReleased(MouseEvent e) {
        drag = false;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (drag == true) {
            mouseX = e.getX();
            mouseY = e.getY();
            label1.setBounds(mouseX, mouseY, 
                    label1.getWidth(), label1.getHeight());
        }
    }

    public void mouseMoved(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}  
    private JLabel label1;
    private int mouseX;
    private int mouseY;
    private boolean drag = false;

}

这是我的板的代码。目前这些碎片无法移动。我在每个 if 语句中都有一行来添加 MouseMotionListener--picLabel.addMouseMotionListener(new motion(picLabel))--但这不起作用。

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

   public class Checkerboard {
       public static void main (String args[]) throws IOException {
         JFrame checkerBoard = new JFrame();
         checkerBoard.setSize(700,700);
         checkerBoard.setTitle("Lines of Action");
         checkerBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         int row = 8;
         int col = 8;
         Container pane = checkerBoard.getContentPane();
         pane.setLayout(new GridLayout(row,col));
         Color checker;   
         for (int x = 1; x <= (row * col); x++) {
            int altr = 0;
            altr = (x-1) % col;
            altr += (x-1) / col;

            if (altr % 2 == 0) {
               checker = Color.darkGray;
            } else {
               checker = Color.lightGray;
            }

            JPanel panel = new JPanel();
            panel.setPreferredSize(new Dimension(400/row, 400/col));
            panel.setBackground(checker);
            if ((x < 8 && x > 1) || (x < 64 && x > 57)) {
                BufferedImage myPicture = 
            ImageIO.read(new  File("C:\\Users\\srjames90\\Downloads\\BlackPiece.png"));
                JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
                picLabel.addMouseMotionListener(new motion(picLabel));
                panel.add(picLabel);
            } else if(check(x)) {
                BufferedImage myPicture = 
            ImageIO.read(new File("C:\\Users\\srjames90\\Downloads\\WhitePiece.png"));
                JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
                panel.add(picLabel);
            }
            pane.add(panel);
         }
         checkerBoard.setVisible(true);
      }

      static boolean check(int y) {
           return y == 9 || y == 16 || y == 17 || y == 24 || y == 25
                   || y == 32 || y == 33 || y == 40 || y == 41 || y == 48
                       || y == 49 || y == 56;
       }
   }

【问题讨论】:

    标签: java jframe jpanel mouseevent jlabel


    【解决方案1】:

    要修复此问题,请将侦听器添加到 Motion 类 JLabel

    Motion(JLabel im) {
        label1 = im;
        label1.addMouseListener(this);
    }
    

    也请阅读Java Programming Style Guidelines

    【讨论】:

      【解决方案2】:

      MouseListenerMouseMotionListener 定义了两组不同的事件。为了让您的听众同时获得这两个集合,您必须同时注册这两个集合......使用类似这样的东西:

      Motion m = new Motion(picLabel);
      picLabel.addMouseMotionListener(m); //will cause m to get mouseMoved and mouseDragged calls
      picLabel.addMouseListener(m); //will cause m to get mousePressed, mouseReleased, mouseEntered, mouseExited, etc.
      

      还值得注意的是,在您发布的代码 sn-p 中,您根本没有向您的 WhitePiece.png 片段添加任何侦听器。

      【讨论】:

      • 这行得通,但现在我无法将我的芯片跨板移动到另一个面板。它只能在它所在的面板内移动。如果我试图将一个芯片从它的面板移动到另一个面板,它看起来就像芯片在另一个面板下面。
      • 这是正确的 - GridLayout 将您添加到其中的项目限制在单个单元格中。如果您希望能够将图像拖放到网格顶部,则必须使用更复杂的解决方案。 Swing 有一个内置的拖放框架,但使用起来可能很复杂;您的另一个不错的选择是尝试涉及位于 GridLayout 上的处理拖动的图层。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多