简体   繁体   English

可扩展组件,用于隐藏或取消隐藏另一个组件

[英]Expandable component to hide or unhide another component

I am wondering if there is a Swing-component which is able to be expanded, so that I am able to hide or unhide something like a menu. 我想知道是否有一个可以扩展的Swing组件,这样我就可以隐藏或取消隐藏菜单之类的东西。

As an example something similar can be found in MS Outlook: 举个例子,可以在MS Outlook中找到类似的东西:

在此输入图像描述

This is the default look, where all mail folders are unhidden. 这是默认外观,其中所有邮件文件夹都被取消隐藏。 But clicking on the little arrow (circled red) hides that view: 但点击小箭头(带圆圈的红色)隐藏了该视图:

在此输入图像描述

I would like to have something similar in my Java-GUI to do the same, while the included component should be hidden by default. 我希望在我的Java-GUI中有类似的东西来做同样的事情,而默认情况下隐藏包含的组件。 I am not sure what component should be under that expandable "tab", but right now I am thinking about a JTree. 我不确定哪个组件应该在那个可扩展的“tab”下面,但是现在我正在考虑一个JTree。


This is what I am generally trying. 这是我一般的尝试。 But if you want a bonus cooky, you could consider the requirement that this expandable menu has to expand in a flowing, smooth animation, instead of being hidden or unhidden instantly. 但是如果你想要一个奖金,你可以考虑这个可扩展菜单必须以流畅,流畅的动画展开的要求,而不是立即隐藏或取消隐藏。 The latter can be found in TeamViewer for example. 例如,后者可以在TeamViewer中找到。 There you have a menu bar on top, which can be hidden or unhidden, while it's going up and down in a smooth animation. 在那里你有一个顶部的菜单栏,可以隐藏或取消隐藏,同时它以平滑的动画上下移动。

Example, TeamViewer: 示例,TeamViewer:

在此输入图像描述


EDIT 编辑

First I tried the JSplitPane , but moving all existing components to fit the split pane schema was not a solution I would prefer. 首先我尝试了JSplitPane ,但是移动所有现有组件以适应拆分窗格架构并不是我想要的解决方案。 Instead I was looking for something more independent. 相反,我正在寻找更独立的东西。

The next thing I tried was using Swing Timer to expand the width of the JFrame using its setBounds -method. 我尝试的下一件事是使用Swing Timer使用setBounds -method扩展JFrame的宽度。 It works exactly the way I want when it comes to toggling additional space for a menu. 当切换菜单的额外空间时,它完全按照我想要的方式工作。 The JFrame gets bigger or smaller while the resizing process is animated. 在调整大小过程动画时, JFrame会变大或变小。 But I can see two disadvantages of this approach: 但我可以看到这种方法的两个缺点:

  • The animation is kind of slow and not perfectly smooth. 动画有点慢而且不够流畅。 I removed the delay. 我删除了延迟。 It is quite OK so far, but a more smoother solution is preferred here. 到目前为止,这是相当不错的,但这里更喜欢更顺畅的解决方案。 But I can totally live with it how it is currently. 但我现在可以完全接受它。
  • A big disadvantage is that the increasing of the size leaves black spaces between the old and the new width for half of a second. 一个很大的缺点是尺寸的增加会使旧宽度和新宽度之间留下黑色空间半秒钟。 If anyone knows how to avoid that, I would have my perfect solution to this problem. 如果有人知道如何避免这种情况,我会完全解决这个问题。

To make it clearer what I mean with "black spaces", see: 为了让我更清楚“黑色空间”的含义,请参阅:

在此输入图像描述

Now you can see that black area. 现在你可以看到黑色区域。 Like I said, it only remains for half of a second or even less. 就像我说的那样,它只能保持半秒甚至更短。 With Swing Timer I added 100 pixels to the width of the JFrame. 使用Swing Timer,我在JFrame的宽度上添加了100个像素。 The higher the value I add to the width, the higher the black area. 我添加到宽度的值越高,黑色区域越高。 If the JFrame 's width is completely resized, everything is in the correct color again. 如果JFrame的宽度已完全调整大小,则所有内容的颜色都会再次正确。

So does anyone know why this happens? 所以有人知道为什么会这样吗? Is this hardware related or is it just simply a standard behavior of Java or Swing? 这个硬件是相关的还是仅仅是Java或Swing的标准行为? Does anyone know solutions or workarounds for this? 有谁知道这方面的解决方案或解决方法?

See splitpane . splitpane

For example 例如

JSplitPane mainSplitPanel = new JSplitPane();
mainSplitPanel.setDividerLocation(650);
mainSplitPanel.setOneTouchExpandable(true);

For samples click here 如需样品, 请点击此处

The solution which fit the best for me can be found in the edited part of my question. 在我的问题的编辑部分中可以找到最适合我的解决方案。 I found a good combination of delay time and frame resizing which appeared smooth enough (1 millesecond delay and increasing the width with 45 pixels). 我发现延迟时间和帧大小调整的良好组合看起来足够平滑(1毫秒延迟并增加宽度,45像素)。 The issue with the black frame is not problematical anymore. 黑框的问题不再是问题。 Now the black screen is even shorter in its duration, and if the user waits around 2 seconds, the black area won't be displayed (visibly) at all. 现在黑屏的持续时间更短,如果用户等待大约2秒,黑色区域将根本不显示(可见)。 In that case it's OK for me, because the user should spend some seconds after expanding anyways. 在那种情况下,对我来说没关系,因为用户应该在扩展之后花几秒钟。

For everyone who wants to know more about this black area while resizing JFrames, see here . 对于想要在调整JFrame大小时了解更多关于此黑色区域的所有人,请参阅此处

The code of the solution I described in my edited question: 我在编辑过的问题中描述的解决方案的代码:

    final Timer timer = new Timer(1, null);
    timer.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt)
        {

            double width = myFrame.getBounds().getWidth();

            if(isExpanded == false)
                width += FRAME_PIXEL_CHANGE;
            else
                width -= FRAME_PIXEL_CHANGE;



            if(myFrame.getBounds().getWidth() >= FRAME_SIZE_EXPANDED && isExpanded == false)
            {
                myFrame.setBounds(FRAME_X, FRAME_Y,  FRAME_SIZE_EXPANDED, FRAME_HEIGTH);
                btnExpand.setIcon(new ImageIcon(GUI.class.getResource("/img/close.png")));
                timer.stop();
                isExpanded = true;
            }
            else if(myFrame.getBounds().getWidth() <= FRAME_SIZE_REGULAR && isExpanded == true)
            {
                myFrame.setBounds(FRAME_X, FRAME_Y,  FRAME_SIZE_REGULAR, FRAME_HEIGTH);
                btnExpand.setIcon(new ImageIcon(GUIMain.class.getResource("/img/expand.png")));
                timer.stop();
                isExpanded = false;
            }
            else
            {
                myFrame.setBounds(FRAME_X, FRAME_Y, (int) width, (int) FRAME_HEIGTH);
                btnExpand.setBounds((int) (width-36), 246, 36, 36);
            }
        }
    });

    return timer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM