简体   繁体   English

如何在Java Swing中使用可折叠/可扩展的JPanel

[英]How to have Collapsable/Expandable JPanel in Java Swing

I want a JPanel that can be Collapsed or Expanded when user clicks on a text/icon on its border. 当用户点击其边框上的文字/图标时,我想要一个可以折叠或展开的JPanel。 I need this type of panel due to space crunch in my application. 由于我的应用程序中的空间紧张,我需要这种类型的面板。

I read about CollapsiblePanel class but not sure how to use it.. I think SwingX is needed to be downloaded but did not find that anywhere. 我读到了关于CollapsiblePanel类但不确定如何使用它。我认为需要下载SwingX但是没有找到任何地方。

Moreover, it would be better if I get the solution to this in basic Java Swing. 而且,如果我在基本的Java Swing中得到解决方案会更好。

not sure where you looked, but it's not that difficult to find - even given the infrastructure mess we are in ;-) 不知道你在哪里看,但它并不找到-甚至考虑到基础设施的混乱我们是在;-)

Go to the project home of SwingX , then follow the link in the first paragraph to the (barebone) download section, down to releases\\1.6.2. 转到SwingX的项目主页,然后按照第一段中的链接进入(准系统)下载部分,直到发布版本1.6.2。 Nothing special to the collapsibles themselves, just containers to put components into. 可折叠物本身没有什么特别之处,只是放入组件的容器。

I think you can use a JSplitPane to tackle your problem. 我认为您可以使用JSplitPane来解决您的问题。 Utilizing the property to set the position of divider judiciously, you can achieve what you want. 利用该属性明智地设置分隔符的位置,您可以实现您想要的。

So here comes a little class purely in Swing :) This implementation assumes the title to be top left... 所以这里有一个纯粹在Swing中的小类:)这个实现假设标题是左上角...

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JCollapsiblePanel extends JPanel {
  private TitledBorder border;
  private Dimension visibleSize;
  private boolean collapsible;

  public JCollapsiblePanel(String title, Color titleCol) {
    super();

    collapsible = true;

    border = new TitledBorder(title);
    border.setTitleColor(titleCol);
    border.setBorder(new LineBorder(Color.white));
    setBorder(border);

    // as Titleborder has no access to the Label we fake the size data ;)
    final JLabel l = new JLabel(title);
    Dimension size = l.getPreferredSize();

    addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        if (!collapsible) {
          return;
        }

        Insets i = getBorder().getBorderInsets(JCollapsiblePanel.this);
        if (e.getX() < i.left + size.width && e.getY() < i.bottom + size.height) {
          if (visibleSize == null || getHeight() > size.height) {
            visibleSize = getSize();
          }
          if (getSize().height < visibleSize.height) {
            setMaximumSize(new Dimension(visibleSize.width, 20000));
            setMinimumSize(visibleSize);
          } else {
            setMaximumSize(new Dimension(visibleSize.width, size.height));
          }
          revalidate();
          e.consume();
        }
      }
    });
  }

  public void setCollapsible(boolean collapsible) {
    this.collapsible = collapsible;
  }

  public void setTitle(String title) {
    border.setTitle(title);
  }
}

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

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