Swing JPanel实现动态滑动效果

来源:互联网 发布:怎么知道淘宝卖家地址 编辑:程序博客网 时间:2024/06/05 08:37
private static JPanel centerPanel=new JPanel();centerPanel.setLayout(null);//使用绝对布局//向左切换返回,panel为切换的页面public static void returnPanel(final JPanel panel) {    int count = centerPanel.getComponentCount();    List list = new ArrayList();    for (Component comp : centerPanel.getComponents())     {        list.add(comp);    }    if (!list.contains(panel))     {        centerPanel.add(panel);    }    panel.setBounds(0, 0, centerPanel.getWidth(), centerPanel.getHeight());    if (count > 0)     {        for (int i = 0; i < count; i++) {            Component comp = centerPanel.getComponent(i);            if (comp instanceof JPanel) {                final JPanel currentPanel = (JPanel) comp;//当前panel                if (currentPanel != panel) {                    new Thread() {                        public void run() {                            Rectangle rec = currentPanel.getBounds();                            int y = centerPanel.getWidth();                            for (int i = 0; i >= -centerPanel.getWidth(); i -= 20) {                            //移动当前panel位置                                currentPanel.setBounds(i, 0,                                        centerPanel.getWidth(),                                        centerPanel.getHeight());                            //移动切换的panel位置                                panel.setBounds(y, rec.y,                                        centerPanel.getWidth(),                                        centerPanel.getHeight());                                try {                                    Thread.sleep(5);                                } catch (InterruptedException e) {                                    // TODO Auto-generated catch block                                    e.printStackTrace();                                }                                y -= 20;                            }                            centerPanel.remove(currentPanel);//移除当前panel                            panel.setBounds(rec.x, rec.y, centerPanel.getWidth(),                                    centerPanel.getHeight());                        }                    }.start();                    break;                }            }        }    }    centerPanel.validate();    centerPanel.repaint();}//向右切换,panel为切换的页面public static void jumpTo(final JPanel panel) {    int count = centerPanel.getComponentCount();    List list = new ArrayList();    for (Component comp : centerPanel.getComponents())     {        list.add(comp);    }    if (!list.contains(panel))     {        centerPanel.add(panel);    }    panel.setBounds(0, 0, centerPanel.getWidth(), centerPanel.getHeight());    if (count > 0)     {        for (int i = 0; i < count; i++) {            Component comp = centerPanel.getComponent(i);            if (comp instanceof JPanel) {                final JPanel currentPanel = (JPanel) comp;                if (currentPanel != panel) {                    new Thread() {                        public void run() {                            Rectangle rec = currentPanel.getBounds();                            int y = -centerPanel.getWidth();                            for (int i = 0; i <= centerPanel.getWidth(); i += 20) {                                currentPanel.setBounds(i, 0,                                        centerPanel.getWidth(),                                        centerPanel.getHeight());                                panel.setBounds(y, rec.y,                                        centerPanel.getWidth(),                                        centerPanel.getHeight());                                try {                                    Thread.sleep(5);                                } catch (InterruptedException e) {                                    // TODO Auto-generated catch block                                    e.printStackTrace();                                }                                y += 20;                            }                            centerPanel.remove(currentPanel);                            panel.setBounds(rec.x, rec.y, centerPanel.getWidth(),                                    centerPanel.getHeight());                        }                    }.start();                    break;                }            }        }    }    centerPanel.validate();    centerPanel.repaint();}
0 0
原创粉丝点击