如何使JSplitPane在鼠标悬停时自动展开? [英] How to make JSplitPane auto expand on mouse hover?

查看:193
本文介绍了如何使JSplitPane在鼠标悬停时自动展开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个布局为Swing的Java程序,其中左侧有一个菜单,当鼠标悬停在菜单区域上时,该菜单会展开,但是在鼠标离开后会自动收缩.

I want to make a Java program with Swing layout where there is a menu on the left that is expanded when a mouse hovers over the menu area, but auto contracts after your mouse leaves.

我正在尝试模仿Android移动版Youtube或Weebly的编辑器之类的效果.对于那些不知道的对象,这两种布局的左侧都有菜单,当鼠标悬停在这些菜单上时,这些菜单会展开.然后,当鼠标离开该区域后,菜单再次收缩并且不可见.

I am trying to imitate the effect of something like mobile Youtube for Android, or Weebly's editor. Fro those who don't know, both layouts have menus on the left that expand when your mouse hovers over them. Then after your mouse leaves the area, the menu contracts again and is out of view.

我能够成功创建包含菜单的JSplitPane,但是我不知道当用户的鼠标悬停在JSplitPane组件上时如何使其自动展开,或者在鼠标离开后如何使其收缩该地区.

I was able to create the JSplitPane containing my menu successfully, but I have no idea how to make it automatically expand when the user's mouse hovers over the JSplitPane component, or how to make it contract after the mouse leaves the area.

万一有人想知道为什么:这种类型的菜单易于用户使用,但是由于不需要时可以隐藏起来,因此我可以为程序的主要部分留出更多空间.

In case anyone is wondering why: This type of menu is easy for the user to use but because it hides away when unneeded, allows me to have more space for the main part of the program.

任何帮助将不胜感激!

推荐答案

很好地利用jSplitPan.setDividerLocation(location);:设置分隔线的位置. location-指定UI特定值(通常为像素数)的int.

Well make use of jSplitPan.setDividerLocation(location);: Sets the location of the divider. location - an int specifying a UI-specific value (typically a pixel count).

jSplitPane1.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent evt) {
                // use evt.getSource() if needed
                jSplitPan1.setDividerLocation(location);
            }
        });

您可能需要智能地计算分频器位置.提示:通过计算相关的首选大小的提示.相关部分应由您发现.

You will probably need to compute the divider location a little bit intelligently. hints: by computing the relevant preferred-size hints. The part relevant should be discovered by you.

请看一下官方教程页面以了解: How to use MouseMotionListeners 包括其他事件侦听器.

Take a look into official tutorial page to know: How to use MouseMotionListeners including other event listeners.

这篇关于如何使JSplitPane在鼠标悬停时自动展开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆