我创建了一个带有包含不同组件的 JPanel 的 JFrame,例如,当鼠标位于 JPanel 的边界内时,我希望 JPanel 有可见的边框和可见的图像。我的问题是,一旦鼠标悬停在 JPanel 内的“可交互”组件上,它就会在鼠标退出 JPanel 时注册。我希望它能够绘制这些东西,只要它在 JPanel 的边界内,并且当鼠标退出 JPanel 的边界时,边框和图像“消失”。有什么办法可以实现这一点吗?
这是一个小演示:
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new TestFrame();
}
static class TestFrame extends JFrame{
JPanel panel;
JButton hoverButton;
JButton appearingButton;
public TestFrame() {
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.red);
hoverButton = new JButton("Hover me!");
appearingButton = new JButton("I appeared!");
appearingButton.setVisible(false);
panel.add(hoverButton);
panel.add(appearingButton);
panel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) …Run Code Online (Sandbox Code Playgroud)