Accordion Menu用于显示java中的图像集

时间:2011-05-15 18:55:04

标签: java swing

我正在做一个包含一些图形内容的Java项目。我想在Accordion菜单下显示一组BufferedImages。这意味着当我点击一个Accordion菜单根项时,它应该以该名称显示一组图像,当点击另一个根菜单项时,它应该显示另一组图像。我怎么能用Java实现这个?有没有办法将JPanel添加为Accordion菜单叶子项?如果任何人都可以提供示例代码,那真的很可观。

2 个答案:

答案 0 :(得分:1)

无法抗拒一些乐趣:事实证明,可以稍微调整JXTaskPaneContainer(在SwingX中)以表现类似于手风琴。所有这一切都需要它来强制最多一个包含的JXTaskPaneContainers进行扩展。像代码片段一样:

    JXTaskPaneContainer container = new JXTaskPaneContainer() {

        private JXTaskPane current;

        private PropertyChangeListener expansionListener;

        /**
         * @inherited <p>
         */
        @Override
        protected void addImpl(Component comp, Object constraints, int index) {
            super.addImpl(comp, constraints, index);
            if (comp instanceof JXTaskPane) {
                grabExpansionControl((JXTaskPane) comp);
            }
        }

        private void grabExpansionControl(JXTaskPane comp) {
            if (current != null) {
                comp.setCollapsed(true);
            } else {
                current = comp;
                comp.setCollapsed(false);
            }
            comp.addPropertyChangeListener("collapsed",
                    getExpansionListener());
        }

        private void updateCurrentTaskPane(JXTaskPane source) {
            if (source != current) {
                if (!source.isCollapsed()) {
                    if (current != null) {
                        current.setCollapsed(true);
                    }
                    current = source;
                }
            }
        }

        private PropertyChangeListener createExpansionListener() {
            PropertyChangeListener l = new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    // TODO Auto-generated method stub
                    updateCurrentTaskPane((JXTaskPane) evt.getSource());
                }
            };
            return l;
        }


        private PropertyChangeListener getExpansionListener() {
            if (expansionListener == null) {
                expansionListener = createExpansionListener();
            }
            return expansionListener;
        }


    };
    ((VerticalLayout) container.getLayout()).setGap(0);

答案 1 :(得分:0)

一点点谷歌搜索,我发现了这个链接。 它可能对你有帮助 - http://code.google.com/p/martin-personal-project/downloads/detail?name=SwingAccordionMenu.zip&can=2&q=

您将获得一个ZIP文件,解压缩并运行SwingAccordionMenu.jar,您将得到手风琴作为输出,如 - enter image description here