选择后面的JPanel父级

发布于 2024-12-01 10:01:13 字数 3485 浏览 0 评论 0原文

下面是 SSCCE 来描述我的问题。

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class APanel extends JPanel{

public APanel() {

    this.setVisible(true);
    this.setBackground(Color.red);
    this.addMouseListener(new MouseListener() {

        public void mouseClicked(MouseEvent e) {
            if(e.getClickCount()==2)
            {

            }
            System.out.println("Child panel clicked!");
        }
        public void mousePressed(MouseEvent e) { }
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited(MouseEvent e) {}
    });
  }
}


    import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PPanel extends JPanel{
    private APanel panel1;
    private APanel panel2;
    private APanel panel3;
    public PPanel() {
        this.setLayout(new GridLayout(0,1));
        panel1 = new APanel();
        panel2 = new APanel();
        panel2.setBackground(Color.yellow);
        panel3 = new APanel();
        panel3.setBackground(Color.green);
        this.add(panel1);
        this.add(panel2);
        this.add(panel3);
        this.setBackground(Color.blue);
         this.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                System.out.println("Parent panel clicked!");
            }
            public void mousePressed(MouseEvent e) { }
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) { }
            public void mouseExited(MouseEvent e) {}
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        PPanel panel = new PPanel();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(350, 300));
        frame.setTitle("Demo");
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

我该如何执行以下操作:

  • 如果 e.getClickCount()==1 则父 MouseListener 将处于活动状态,并且它将打印“已单击父面板!”。
  • 如果 e.getClickCount()==2 则子 MouseListner 将激活并打印出“已单击子面板!”。

    编辑1:更接近建议的解决方案。

    导入java.awt.*;
    导入 java.awt.event.*;
    导入javax.swing.JPanel;
    导入 javax.swing.Timer;
    
    公共类 APanel 扩展 JPanel {
    
    私有静态最终长serialVersionUID = 1L;
    私人点 pt;
    
    公共APanel(){
        计时器.setRepeats(假);
        addMouseListener(new MouseAdapter() {
    
            @覆盖
            公共无效鼠标点击(MouseEvent e){
                if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) {
                    System.out.println("来自孩子的双倍");
                    pt = 空;
                    计时器.stop();
                } 别的 {
                    pt = e.getPoint();
                    组件组件 = (Component)e.getSource();
                    component.getParent().dispatchEvent(e);
                    计时器.重新启动();
                }
            }
        });
        setBackground(颜色.红色);
        设置可见(真);
    }
    
    私有定时器计时器 = new Timer(200, new ActionListener() {
    
        @覆盖
        公共无效actionPerformed(ActionEvent e){
            //System.out.println("单从孩子");
        }
    });
    

    }

mouseEvent 需要传递多个容器,请使用此 链接 可能会感兴趣。

Below is SSCCE to describe my problem.

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class APanel extends JPanel{

public APanel() {

    this.setVisible(true);
    this.setBackground(Color.red);
    this.addMouseListener(new MouseListener() {

        public void mouseClicked(MouseEvent e) {
            if(e.getClickCount()==2)
            {

            }
            System.out.println("Child panel clicked!");
        }
        public void mousePressed(MouseEvent e) { }
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited(MouseEvent e) {}
    });
  }
}


    import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PPanel extends JPanel{
    private APanel panel1;
    private APanel panel2;
    private APanel panel3;
    public PPanel() {
        this.setLayout(new GridLayout(0,1));
        panel1 = new APanel();
        panel2 = new APanel();
        panel2.setBackground(Color.yellow);
        panel3 = new APanel();
        panel3.setBackground(Color.green);
        this.add(panel1);
        this.add(panel2);
        this.add(panel3);
        this.setBackground(Color.blue);
         this.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                System.out.println("Parent panel clicked!");
            }
            public void mousePressed(MouseEvent e) { }
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) { }
            public void mouseExited(MouseEvent e) {}
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        PPanel panel = new PPanel();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(350, 300));
        frame.setTitle("Demo");
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

How can I do the following:

  • If e.getClickCount()==1 then the parent MouseListener will active and it will print "parent panel clicked!".
  • If e.getClickCount()==2 then the children MouseListner will active and print out "child panel clicked!".

    Edit1: Closer to the proposed solution.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class APanel extends JPanel {
    
    private static final long serialVersionUID = 1L;
    private Point pt;
    
    public APanel() {
        timer.setRepeats(false);
        addMouseListener(new MouseAdapter() {
    
            @Override
            public void mouseClicked(MouseEvent e) {
                if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) {
                    System.out.println("double from child");
                    pt = null;
                    timer.stop();
                } else {
                    pt = e.getPoint();
                    Component component = (Component)e.getSource();
                    component.getParent().dispatchEvent(e);
                    timer.restart();
                }
            }
        });
        setBackground(Color.red);
        setVisible(true);
    }
    
    private Timer timer = new Timer(200, new ActionListener() {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            //System.out.println("single from child");
        }
    });
    

    }

In case, a mouseEvent needs to pass through multiplevel of containers, this link might be of interest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言 2024-12-08 10:01:13

如果您选择解释双击,请考虑使用用户的首选间隔,如建议的 此处

Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

If you elect to interpret double clicks, consider using the user's preferred interval, as suggested here.

Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
栀梦 2024-12-08 10:01:13

我认为(据我所知)不可能将鼠标事件从子级重定向到其父级,只需从 MouseCursor 下的组件获取父级,或者从从 MouseClick 接收事件的组件获取父级,

也许有人可以帮助您: -),但这里是您与父项成功所需的代码

import java.awt.*;
import javax.swing.*;

public class PPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private APanel panel1;
    private APanel panel2;
    private APanel panel3;

    public PPanel() {
        setLayout(new GridLayout(0, 1));
        panel1 = new APanel();
        panel2 = new APanel();
        panel2.setBackground(Color.yellow);
        panel3 = new APanel();
        panel3.setBackground(Color.green);
        add(panel1);
        add(panel2);
        add(panel3);
        setBackground(Color.blue);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                PPanel panel = new PPanel();
                frame.add(panel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(350, 300));
                frame.setTitle("Demo");
                frame.setLocationRelativeTo(null);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

子项:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.Timer;

public class APanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private Point pt;

    public APanel() {
        timer.setRepeats(false);
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) {
                    System.out.println("double from child");
                    pt = null;
                    timer.stop();
                } else {
                    pt = e.getPoint();
                    timer.restart();
                }
            }
        });
        setBackground(Color.red);
        setVisible(true);
    }

    private Timer timer = new Timer(400, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("single from child");
        }
    });
}

I think (as I know) that not is possible redirect Mouse event from child to its parent, just get parent from Component that is under the MouseCursor, or get parent from Component that's received Events from MouseClick

sure maybe someone can help you with that :-), but here is code which you needed for success with that

parent:

import java.awt.*;
import javax.swing.*;

public class PPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private APanel panel1;
    private APanel panel2;
    private APanel panel3;

    public PPanel() {
        setLayout(new GridLayout(0, 1));
        panel1 = new APanel();
        panel2 = new APanel();
        panel2.setBackground(Color.yellow);
        panel3 = new APanel();
        panel3.setBackground(Color.green);
        add(panel1);
        add(panel2);
        add(panel3);
        setBackground(Color.blue);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                PPanel panel = new PPanel();
                frame.add(panel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(350, 300));
                frame.setTitle("Demo");
                frame.setLocationRelativeTo(null);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

Child:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.Timer;

public class APanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private Point pt;

    public APanel() {
        timer.setRepeats(false);
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) {
                    System.out.println("double from child");
                    pt = null;
                    timer.stop();
                } else {
                    pt = e.getPoint();
                    timer.restart();
                }
            }
        });
        setBackground(Color.red);
        setVisible(true);
    }

    private Timer timer = new Timer(400, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("single from child");
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文