如何将MouseMotionListenter添加到扩展JLabel的类?

时间:2014-04-10 23:41:30

标签: java swing jlabel rmi mouselistener

正如标题所示,我正在尝试将MouseMotionListener添加到我创建的扩展Magnet的类(JLabel)中。该程序使用RMI在客户端和服务器之间进行通信。扩展JLabel的类是正在传输的数据。我的问题是,当我在客户端添加MouseMotionListener时,程序不会注册移动。正如我所说的,我下面的代码即使它应该也不会打印任何内容。我没有收到任何警告或错误。我真的迷路了。

这是Magnet

private static final long serialVersionUID = 1L;
private Font FONT = new Font("comic sans ms", Font.BOLD, 20);

private char letter;
private int x;
private int y;
private int id;

public Magnet(int x, int y, char letter, int id) {
    super();
    this.x = x;
    this.y = y;
    this.id = id;
    this.letter = Character.toUpperCase(letter);
    setText(Character.toString(this.letter));
    setFont(FONT);
    setBounds(x, y, 25, 25);
}

public char getLetter() {
    return letter;
}

public int getID() {
    return id;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public void relocate(int x, int y) {
    this.x = x;
    this.y = y;
    setBounds(x, y, 25, 25);
}

client

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 400, 400);
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    try {
        magnets = (MagnetInterface) Naming
                .lookup("//localhost:6667/MagnetServer");
        System.out.println("client: connected!");
        for (int i = 0; i < magnets.getSize(); i++) {
            magnets.getMagnet(0).addMouseMotionListener(
                            new MouseMotionListener() {

                @Override
                public void mouseMoved(MouseEvent arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("test");
                }

                @Override
                public void mouseDragged(MouseEvent arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("test");
                }
            });
            contentPane.add(tmp);
        }
    } catch (MalformedURLException | RemoteException | NotBoundException e) {
        System.out.println(e.getMessage());
        // e.printStackTrace();
    }
    setContentPane(contentPane);

0 个答案:

没有答案