正如标题所示,我正在尝试将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);