Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将MouseMotionListener添加到扩展JLabel的类中?_Java_Swing_Jlabel_Rmi_Mouselistener - Fatal编程技术网

Java 如何将MouseMotionListener添加到扩展JLabel的类中?

Java 如何将MouseMotionListener添加到扩展JLabel的类中?,java,swing,jlabel,rmi,mouselistener,Java,Swing,Jlabel,Rmi,Mouselistener,正如标题所说,我正在尝试将MouseMotionListener添加到我创建的扩展JLabel的类(Magnet)。该程序使用RMI在客户端和服务器之间进行通信。扩展JLabel的类是正在传输的数据。我的问题是,当我在客户端添加MouseMotionListener时,程序不会注册移动。正如我所说,我下面的代码没有打印任何东西,即使它应该打印。我没有收到任何警告或错误。我真的迷路了 这是磁铁类 private static final long serialVersionUID = 1L; pr

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

这是
磁铁

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);
}
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);
客户端

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);
}
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);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100400400);
可设置大小(假);
contentPane=newjpanel();
contentPane.setLayout(null);
试一试{
磁铁=(磁界面)命名
.lookup(“//localhost:6667/MagnetServer”);
System.out.println(“客户端:已连接!”);
对于(int i=0;i
简短的回答是不要(我理解这个问题)。UI组件不应通过线路发送,它们获取对无法序列化回服务器的本地系统的引用,最多会导致
NotSerializableException
s,更糟糕的是
NullPointerException
s。此外,基于您的代码,我看不出您在何处向容器添加
magnetics.getmagnetics(0)
,我建议包括导入和实际的类声明。我们要知道,您并不是无意中做了一些愚蠢的事情,比如
import instant.fail.JLabel
或类似于
class Magnet extends JLabel实现了可实现的
无关:a)所有JSO内容都应按原样使用(而不是扩展)b)不要对组件进行任何手动大小调整/定位,这是合适的LayoutManager的专属任务