Java实现类Windows导航栏

                Java实现类Windows导航栏

在Windows系统中,通常都会有左侧导航栏以供选择,而Java架构下并没有给我们提供同样的组件,但是构建同样的东西实际上是很容易的。

 
package  org.loon.framework.swing.test;

import  java.awt.BorderLayout;
import  java.awt.Color;
import  java.awt.Component;
import  java.awt.Dimension;
import  java.awt.EventQueue;
import  java.awt.Font;
import  java.awt.GradientPaint;
import  java.awt.Graphics;
import  java.awt.Graphics2D;
import  java.awt.GridLayout;
import  java.awt.Insets;
import  java.awt.Rectangle;

import  java.awt.event.ComponentAdapter;
import  java.awt.event.ComponentEvent;
import  java.awt.event.MouseAdapter;
import  java.awt.event.MouseEvent;
import  java.util.ArrayList;
import  java.util.Iterator;
import  java.util.List;

import  javax.swing.BorderFactory;
import  javax.swing.ButtonGroup;
import  javax.swing.JCheckBox;
import  javax.swing.JComponent;
import  javax.swing.JFrame;
import  javax.swing.JLabel;
import  javax.swing.JPanel;
import  javax.swing.JRadioButton;
import  javax.swing.JScrollPane;
import  javax.swing.JSplitPane;
import  javax.swing.SpringLayout;
    import javax.swing.WindowConstants;
import javax.swing.border.Border;

/** *//**
 * 


 * Title: LoonFramework
 * 


 * 


 * Description:
 * 


 * 


 * Copyright: Copyright (c) 2007
 * 


 * 


 * Company: LoonFramework
 * 


 * 
 * 
@author chenpeng
 * @email:[email protected]
 * 
@version 0.1
 
*/


public class ExampleAccordion extends JPanel ...{
    
/** *//**
     * 
     
*/

    
private static final long serialVersionUID = 1L;

    
private final JPanel panel = new JPanel();

    
private final JLabel label = new JLabel();

    
// 分割窗体
    private final JSplitPane split = new JSplitPane();

    
private final JScrollPane scroll;

    
// 折叠效果
    public ExampleAccordion() ...{
        
super(new BorderLayout());
        panel.setOpaque(
true);
        panel.setBackground(
new Color(116149226));
        
// 滚动条
        scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.getVerticalScrollBar().setUnitIncrement(
10);
        scroll.getViewport().add(panel);

        
// 构建数据列表
        List panelList = makeList();
        
// 设定监听
        accordionListener exr = new accordionListener() ...{
            
public void accordionStateChanged(accordionEvent e) ...{
                initComponent();
            }

        }
;
        
for (Iterator it = panelList.iterator(); it.hasNext();) ...{
            AccordionPanel epl 
= (AccordionPanel) it.next();
            addComponent(epl);
            epl.addaccordionListener(exr);
        }

        
// 加载滚动条监听
        scroll.getViewport().addComponentListener(new ComponentAdapter() ...{
            
public void componentResized(ComponentEvent e) ...{
                initComponent();
            }

        }
);
        
// 设定大小
        label.setPreferredSize(new Dimension(200260));

        scroll.setPreferredSize(
new Dimension(200260));

        scroll.setMinimumSize(
new Dimension(200260));

        split.setLeftComponent(scroll);
        split.setRightComponent(label);

        split.setDividerSize(
1);
        split.setBackground(Color.WHITE);
        add(split, BorderLayout.CENTER);
    }


    
public void initComponent() ...{
        Rectangle re 
= scroll.getViewport().getViewRect();
        Insets ins 
= panel.getInsets();
        
int cw = (int) re.getWidth() - ins.left - ins.right - 20;
        
int ch = 10;
        Component[] list 
= panel.getComponents();
        
for (int i = 0; i < list.length; i++...{
            JComponent tmp 
= (JComponent) list[i];
            
int th = tmp.getPreferredSize().height;
            tmp.setPreferredSize(
new Dimension(cw, th));
            ch 
= ch + th + 10;
        }

        panel.setPreferredSize(
new Dimension((int) re.getWidth(), ch + ins.top
                
+ ins.bottom));
        panel.revalidate();
    }


    
public void addComponent(Component label) ...{
        SpringLayout layout 
= new SpringLayout();
        Component[] list 
= panel.getComponents();
        
if (list.length == 0...{
            layout.putConstraint(SpringLayout.WEST, label, 
10,
                    SpringLayout.WEST, panel);
            layout.putConstraint(SpringLayout.NORTH, label, 
10,
                    SpringLayout.NORTH, panel);
        }
 else ...{
            JComponent cmp 
= null;
            
for (int i = 0; i < list.length; i++...{
                JComponent tmp 
= (JComponent) list[i];
                layout.putConstraint(SpringLayout.WEST, tmp, 
10,
                        SpringLayout.WEST, panel);
                
if (cmp == null...{
                    layout.putConstraint(SpringLayout.NORTH, tmp, 
10,
                            SpringLayout.NORTH, panel);
                }
 else ...{
                    layout.putConstraint(SpringLayout.NORTH, tmp, 
10,
                            SpringLayout.SOUTH, cmp);
                }

                cmp 
= tmp;
            }

            layout.putConstraint(SpringLayout.WEST, label, 
10,
                    SpringLayout.WEST, panel);
            layout.putConstraint(SpringLayout.NORTH, label, 
10,
                    SpringLayout.SOUTH, cmp);
        }

        panel.add(label);
        panel.setLayout(layout);
        initComponent();
    }


    
private List makeList() ...{
        List panelList 
= new ArrayList();
        panelList.add(
new AccordionPanel("列表1"...{
            
/** *//**
             * 
             
*/

            
private static final long serialVersionUID = 1L;

            
public JPanel makePanel() ...{
                JPanel pnl 
= new JPanel(new GridLayout(01));
                JCheckBox c1 
= new JCheckBox("aaaaaa");
                JCheckBox c2 
= new JCheckBox("bbbbbb");
                c1.setOpaque(
false);
                c2.setOpaque(
false);
                pnl.add(c1);
                pnl.add(c2);
                pnl.setSize(
new Dimension(060));
                pnl.setBorder(BorderFactory.createEmptyBorder(
515515));
                
return pnl;
            }

        }
);
        panelList.add(
new AccordionPanel("列表2"...{
            
/** *//**
             * 
             
*/

            
private static final long serialVersionUID = 1L;

            
public JPanel makePanel() ...{
                JPanel pnl 
= new JPanel(new GridLayout(01));
                pnl.add(
new JLabel("辛苦遭逢起一经"));
                pnl.add(
new JLabel("干戈寥落四周星"));
                pnl.add(
new JLabel("山河破碎风飘絮"));
                pnl.add(
new JLabel("身世浮沉雨打萍"));
                pnl.setSize(
new Dimension(0100));
                pnl.setBorder(BorderFactory.createEmptyBorder(
515515));
                
return pnl;
            }

        }
);
        panelList.add(
new AccordionPanel("列表3"...{
            
/** *//**
             * 
             
*/

            
private static final long serialVersionUID = 1L;

            
public JPanel makePanel() ...{
                JPanel pnl 
= new JPanel(new GridLayout(01));
                JRadioButton b1 
= new JRadioButton("aa");
                JRadioButton b2 
= new JRadioButton("bb");
                JRadioButton b3 
= new JRadioButton("cc");
                b1.setOpaque(
false);
                b2.setOpaque(
false);
                b3.setOpaque(
false);
                pnl.add(b1);
                pnl.add(b2);
                pnl.add(b3);
                ButtonGroup bg 
= new ButtonGroup();
                bg.add(b1);
                bg.add(b2);
                bg.add(b3);
                b1.setSelected(
true);
                pnl.setSize(
new Dimension(080));
                pnl.setBorder(BorderFactory.createEmptyBorder(
515515));
                
return pnl;
            }

        }
);
        
return panelList;
    }


    
public static void main(String[] args) ...{
        EventQueue.invokeLater(
new Runnable() ...{
            
public void run() ...{
     &nbs

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

你可能感兴趣的:(Java实现类Windows导航栏)