gpt4 book ai didi

swing - 在后面选择 JPanel parent

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:43 26 4
gpt4 key购买 nike

下面是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 将被激活并且它会打印“parent panel clicked!”。
  • 如果 e.getClickCount()==2 则子 MouseListner 将激活并打印出“child panel clicked!”。

    Edit1:更接近建议的解决方案。

    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");
    }
    });

如果鼠标事件需要通过多个容器,这 link可能会感兴趣。

最佳答案

如果您选择解释双击,请考虑使用用户的首选间隔,如建议的那样here .

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

关于swing - 在后面选择 JPanel parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com