Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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 我能把一块玻璃板一分为二,然后在每个玻璃板上分别加一个鼠标听筒吗?_Java_Swing_User Interface_Format_Glasspane - Fatal编程技术网

Java 我能把一块玻璃板一分为二,然后在每个玻璃板上分别加一个鼠标听筒吗?

Java 我能把一块玻璃板一分为二,然后在每个玻璃板上分别加一个鼠标听筒吗?,java,swing,user-interface,format,glasspane,Java,Swing,User Interface,Format,Glasspane,我有一些代码,在玻璃窗格下有两个图像。我想让每个图像都在它自己的窗格玻璃下,每个窗格玻璃都发出它自己鼠标听筒的信号。目前,我已经在一块玻璃窗下制作了两个鼠标,整个玻璃窗都有一个鼠标听筒。两幅图像并排在网格布局中,所以将玻璃窗格一分为二应该不会太难。下面是一个玻璃窗格的代码,但请注意,我正在尝试为每个图像创建两个玻璃窗格和两个独立的鼠标侦听器类。这只是针对**两个图像的one*鼠标侦听器的代码: package Buttons; import java.awt.GridLayout; impo

我有一些代码,在玻璃窗格下有两个图像。我想让每个图像都在它自己的窗格玻璃下,每个窗格玻璃都发出它自己鼠标听筒的信号。目前,我已经在一块玻璃窗下制作了两个鼠标,整个玻璃窗都有一个鼠标听筒。两幅图像并排在网格布局中,所以将玻璃窗格一分为二应该不会太难。下面是一个玻璃窗格的代码,但请注意,我正在尝试为每个图像创建两个玻璃窗格和两个独立的鼠标侦听器类。这只是针对**两个图像的one*鼠标侦听器的代码:

package Buttons;


import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Giraffewindow extends JDialog {
public Giraffewindow() {
    JDialog giraffewindow = new JDialog();

    Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png"));
    Icon windows = new ImageIcon(getClass().getResource("windows.png"));

    giraffewindow.setLayout(new GridLayout(1, 2, 0, 0));
    giraffewindow.add(new JLabel(windows));
    giraffewindow.add(new JLabel(giraffe));


    giraffewindow.pack();
    giraffewindow.setTitle("GIRAFFE!");
    giraffewindow.setVisible(true);
    giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    JPanel glass = ((JPanel) giraffewindow.getGlassPane());
    glass.setVisible(true);
    status = new JLabel("I can change");

    glass.add(status);
    glass.setLayout(null);
    giraffemousehandler giraffemouse = new giraffemousehandler();
    glass.addMouseListener(giraffemouse);
    glass.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture
    // setLayout(null);
}


JLabel status = null;

class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse

    }

    @Override
    public void mouseEntered(MouseEvent e) {

        status.setText("Enter);

    }

    @Override
    public void mouseExited(MouseEvent e) {

        status.setText("Exit");
        // status.setBounds(e.getX(), e.getY(), 5, 6);

    }

}
}
这是camickr请求的代码,请注意有两个独立的鼠标侦听器,我想知道如何做,否则。当JLabel跟随鼠标时,1)它离鼠标非常远,2)它不显示完整的JLabel,3)在一次退出/进入后它不会改变。我非常感谢您的帮助,以下是基于camickrs建议的代码:

import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

class SSCCE extends JDialog {

public SSCCE() {
    JDialog giraffewindow = new JDialog();

    Icon giraffe = new ImageIcon(getClass().getResource("giraffe.png"));
    Icon windows = new ImageIcon(getClass().getResource("windows.png"));

    giraffewindow.setLayout(new GridLayout(1, 2, 0, 0));
    JLabel giraffelabel = new JLabel();
    JLabel windowlabel = new JLabel();

    windowlabel.setIcon(windows);
    giraffelabel.setIcon(giraffe);

    giraffewindow.add(windowlabel);
    giraffewindow.add(giraffelabel);

    giraffewindow.setTitle("Title!");
    giraffewindow.setSize(1100, 600);
    giraffewindow.setVisible(true);
    giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    JPanel glass = ((JPanel) giraffewindow.getGlassPane()); //Glasspane
    glass.setVisible(true);

    status = new JLabel("I can change"); //This is the JLabel which should follow my mouse

    glass.add(status);
    glass.setLayout(null);

    giraffemousehandler giraffemouse = new giraffemousehandler();
    windowmousehandler windowmouse = new windowmousehandler();

    windowlabel.addMouseListener(windowmouse);
    giraffelabel.addMouseMotionListener(giraffemouse); //Can I add mouse motion listener to a picture

    // setLayout(null);
}

JLabel status = null;

class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        status.setBounds(e.getX(), e.getY(), 50, 60); //Makes JLabel follow mouse

    }

    @Override
    public void mouseEntered(MouseEvent e) {

        status.setText("Mouse is on giraffe");

    }

    @Override
    public void mouseExited(MouseEvent e) {

        status.setText("Mouse has left giraffe");
        // status.setBounds(e.getX(), e.getY(), 5, 6);

    }

}

class windowmousehandler extends MouseAdapter implements MouseListener, MouseMotionListener {
    public void mouseMoved(MouseEvent event) {
        // TODO Auto-generated method stub
        status.setBounds(event.getX(), event.getY(), 50, 60); //Makes JLabel follow mouse

    }

    public void mouseEntered(MouseEvent event) {

        status.setText("Mouse is on window");

    }

    @Override
    public void mouseExited(MouseEvent event) {

        status.setText("Mouse has left window");
        // status.setBounds(e.getX(), e.getY(), 5, 6);

    }
}
}

public class Icallsscce {
    public static void main(String [] args) {
        SSCCE object = new SSCCE();
    }
}
如果您真正理解您要实现的目标,那么几乎每个问题都可以用10-50行代码来演示

不可以。组件就是组件,不能将组件一分为二

您有两个选择:

  • 如果将鼠标侦听器添加到玻璃窗格中,则需要使用事件中的鼠标点并使用
    容器.getComponentAt(…)
    确定鼠标当前位于哪个组件上。请注意,玻璃窗格覆盖整个框架,因此鼠标点相对于包含边框的框架,因此首先需要使用
    SwingUtilities.convertPoint(…)
    方法将鼠标点转换为相对于添加标签的面板的点

  • 如果向每个标签添加单独的鼠标侦听器,则鼠标点将相对于标签,因此移动弹出标签时,需要将标签的“x”值添加到玻璃窗格上的点。我认为这种方法更容易。注意:使用这种方法,您可以共享同一个侦听器,您只需要使用MouseEvent的
    getSource()
    方法来获取标签

  • 编辑:

    如何将getSource()方法添加到mouseevent

    这已在10天前得到答复:

    因为它不是一个完整的程序,所以当你甚至不看代码的时候,它会变得很烦人。我们不是来为您编写代码的。当有人花时间回答一个问题时,你至少要花时间去理解这个建议

    1) 它离老鼠非常远

    我在这个问题上已经回答了这个问题。我说过
    您需要添加标签的“x”值。
    。您需要这样做,因为鼠标事件是相对于添加侦听器的组件生成的

    因此,对于长颈鹿标签,x的值将从0开始,并随着鼠标向右移动而增加。但是,长颈鹿从玻璃窗格的中心开始,因此不能使用0作为弹出标签的位置。您还需要包括长颈鹿的x locaton

    因此,基本准则应该是:

    popupLabel.setLocation(e.getX() + giraffe.getLocation().x, e.getY());
    
    当然,您应该使用MouseEvent的“源”对象(而不是长颈鹿标签),如上面的答案所示。使用源对象时,代码将适用于两个标签

    2) 它不显示完整的JLabel

    因为在setBounds()方法中使用了“幻数”。为什么标签的宽度用“60”这样的数字?不要硬编码数字。当您更改标签的文本时,可以执行以下操作:

    label.setText(....);
    label.setSize(label.getPreferredSize());
    
    现在标签将具有适当的宽度/高度。然后,当您在玻璃窗格上移动标签时,只需使用
    setLocation(…)
    方法来定位标签

    它在一次退出/进入后不会更改

    您正在实现一个MouseListener和一个MouseMotionListener,因此需要将这两个侦听器添加到每个组件中。因此,您需要:

    长颈鹿;长颈鹿(…); 长颈鹿。addMouseMotionListener(…); window.addMouseListener(…); addMouseMotionListener(…)

    同样,请记住,您只需要一个类来实现MouseListener和MouseMotionListener,如10天前所示。监听器的同一个实例可以添加到两个组件中,这将有助于清理代码

    如果您真正理解您要实现的目标,那么几乎每个问题都可以用10-50行代码来演示

    不可以。组件就是组件,不能将组件一分为二

    您有两个选择:

  • 如果将鼠标侦听器添加到玻璃窗格中,则需要使用事件中的鼠标点并使用
    容器.getComponentAt(…)
    确定鼠标当前位于哪个组件上。请注意,玻璃窗格覆盖整个框架,因此鼠标点相对于包含边框的框架,因此首先需要使用
    SwingUtilities.convertPoint(…)
    方法将鼠标点转换为相对于添加标签的面板的点

  • 如果向每个标签添加单独的鼠标侦听器,则鼠标点将相对于标签,因此移动弹出标签时,需要将标签的“x”值添加到玻璃窗格上的点。我认为这种方法更容易。注意:使用这种方法,您可以共享同一个侦听器,您只需要使用MouseEvent的
    getSource()
    方法来获取标签

  • 编辑:

    如何将getSource()方法添加到mouseevent

    这已在10天前得到答复:

    因为它不是一个完整的程序,所以当你甚至不看代码的时候,它会变得很烦人。我们不是来为您编写代码的。当有人花时间回答问题时,至少
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRootPane;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
    
    public class Giraffewindow {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
                    new Giraffewindow();
                }
            });
        }
    
        public Giraffewindow() {
            JDialog giraffewindow = new JDialog();
    
            try {
                Icon giraffe = new ImageIcon(ImageIO.read(new File("...")));
                Icon windows = new ImageIcon(ImageIO.read(new File("...")));
    
                JLabel left = new JLabel(windows);
                JLabel right = new JLabel(giraffe);
    
                giraffewindow.setLayout(new GridBagLayout());
                giraffewindow.add(left);
                giraffewindow.add(right);
    
                giraffewindow.pack();
                giraffewindow.setLocationRelativeTo(null);
                giraffewindow.setTitle("GIRAFFE!");
                giraffewindow.setVisible(true);
                giraffewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                JPanel glass = ((JPanel) giraffewindow.getGlassPane());
                glass.setVisible(true);
                status = new JLabel("I can change");
                status.setForeground(Color.WHITE);
                status.setBackground(Color.RED);
                status.setOpaque(true);
    
                glass.add(status);
                glass.setLayout(null);
                giraffemousehandler giraffemouse = new giraffemousehandler();
    
                glass.addMouseMotionListener(giraffemouse);
    
                // setLayout(null);
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }
    
        JLabel status = null;
    
        class giraffemousehandler extends MouseAdapter implements MouseListener, MouseMotionListener { //MouseAdapter makes it so that you don't have to have all 7 implemented mouse listener methods
    
            private Component last = null;
    
            @Override
            public void mouseMoved(MouseEvent e) {
                // TODO Auto-generated method stub
    
                Point p = e.getPoint();
    
                JRootPane rootPane = SwingUtilities.getRootPane(e.getComponent());
                Container contentPane = rootPane.getContentPane();
                Component comp = contentPane.getComponentAt(p);
                if (comp != last) {
                    if (last != null) {
                        mouseExited(last);
                    }
                    if (comp != null) {
    
                        if (comp != contentPane) {
                            last = comp;
                            mouseEntered(comp);
                        } else {
                            last = null;
                        }
    
                    }
                }
    
                status.setSize(status.getPreferredSize());
                status.setLocation(p.x, p.y);
    
            }
    
            protected void mouseExited(Component comp) {
                System.out.println("Exited");
                status.setText("Exited");
            }
    
            protected void mouseEntered(Component comp) {
                status.setText("Entered");
            }
    
        }
    }