具有边框布局的 JPanel 上的事件

发布于 2024-12-08 22:44:54 字数 997 浏览 2 评论 0原文

当我将 MouseListener/FocusListener 添加到其中包含 BorderLayout 和 JComponent 的 JPanel 时,我无法捕获鼠标或焦点事件。有没有办法捕获具有 BorderLayout 的 JPanel 鼠标和焦点事件?

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

public class Application extends JFrame{

    public Application(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel jPanel = new JPanel(new BorderLayout());
        jPanel.add(new JButton("Button"));

        jPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("mouseExited");
            }
        });

//        if border is set then listener works if not does not
//        jPanel.setBorder(new LineBorder(Color.black, 1));
        setLayout(new FlowLayout());
        add(jPanel);
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String[]args){
        new Application().setVisible(true);
    }

}

When I add a MouseListener/FocusListener to a JPanel which has a BorderLayout and JComponents in it, I can't catch mouse or focus events. Is there any way to catch a JPanel's mouse and focus events which has a BorderLayout?

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

public class Application extends JFrame{

    public Application(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel jPanel = new JPanel(new BorderLayout());
        jPanel.add(new JButton("Button"));

        jPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("mouseExited");
            }
        });

//        if border is set then listener works if not does not
//        jPanel.setBorder(new LineBorder(Color.black, 1));
        setLayout(new FlowLayout());
        add(jPanel);
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String[]args){
        new Application().setVisible(true);
    }

}

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

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

发布评论

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

评论(2

不醒的梦 2024-12-15 22:44:54

正如所说,只是一个简单的错误。由于 JFrame 被赋予了 FlowLayout,因此 JPanel 仅占据 JButton 所需的区域。您可以通过向 JPanel 添加 Border 来测试这一点。

现在可以了,

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

public class Application extends JFrame {

    private static final long serialVersionUID = 1L;

    public Application() {
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout());
        jPanel.add(new JButton("Button"));
        jPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("mouseExited");
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(jPanel);
        setSize(400, 400);
        setVisible(true);
    }

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

            @Override
            public void run() {
                new Application().setVisible(true);
            }
        });
    }
}

As said, just a simple mistake. Because JFrame is given a FlowLayout, the JPanel occupies the area required for JButton only. You can test that by adding a Border to the JPanel.

Now it works,

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

public class Application extends JFrame {

    private static final long serialVersionUID = 1L;

    public Application() {
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout());
        jPanel.add(new JButton("Button"));
        jPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("mouseExited");
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(jPanel);
        setSize(400, 400);
        setVisible(true);
    }

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

            @Override
            public void run() {
                new Application().setVisible(true);
            }
        });
    }
}
海螺姑娘 2024-12-15 22:44:54

以下代码将相应的事件打印到 StdOut。

    JFrame frame = new JFrame();

    JPanel panel = new JPanel(new BorderLayout());
    JPanel innerPanel = new JPanel();
    innerPanel.setSize(200,200);
    panel.add(innerPanel);
    panel.addMouseListener(new MouseListener() {
        public void mouseReleased(MouseEvent e) {
            System.out.println("MouseReleased");
        }
        public void mousePressed(MouseEvent e) {
            System.out.println("MousePressed");
        }
        public void mouseExited(MouseEvent e) {
            System.out.println("MouseExited");
        }
        public void mouseEntered(MouseEvent e) {
            System.out.println("MouseEntered");
        }
        public void mouseClicked(MouseEvent e) {
            System.out.println("MouseClicked");
        }
    });

    frame.setContentPane(panel);
    frame.setVisible(true);`

The following Code prints the corresponding Events to StdOut.

    JFrame frame = new JFrame();

    JPanel panel = new JPanel(new BorderLayout());
    JPanel innerPanel = new JPanel();
    innerPanel.setSize(200,200);
    panel.add(innerPanel);
    panel.addMouseListener(new MouseListener() {
        public void mouseReleased(MouseEvent e) {
            System.out.println("MouseReleased");
        }
        public void mousePressed(MouseEvent e) {
            System.out.println("MousePressed");
        }
        public void mouseExited(MouseEvent e) {
            System.out.println("MouseExited");
        }
        public void mouseEntered(MouseEvent e) {
            System.out.println("MouseEntered");
        }
        public void mouseClicked(MouseEvent e) {
            System.out.println("MouseClicked");
        }
    });

    frame.setContentPane(panel);
    frame.setVisible(true);`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文