选择后面的JPanel父级
下面是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您选择解释双击,请考虑使用用户的首选间隔,如建议的 此处。
If you elect to interpret double clicks, consider using the user's preferred interval, as suggested here.
我认为(据我所知)不可能将鼠标事件从子级重定向到其父级,只需从 MouseCursor 下的组件获取父级,或者从从 MouseClick 接收事件的组件获取父级,
也许有人可以帮助您: -),但这里是您与父项成功所需的代码
:
子项:
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:
Child: