滑动JPanel

时间:2019-01-06 03:58:41

标签: java nullpointerexception jpanel

我正在尝试使用现有代码滑动JPanel。但是当我尝试滑动时会出现此错误,

  

线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException     在   Library.PanelSlider.PanelSlider.nextSlidPanel(PanelSlider.java:44)     在FDB.Main.MainWindow.addSBMouseClicked(MainWindow.java:180)     在FDB.Main.MainWindow.access $ 000(MainWindow.java:18)在   FDB.Main.MainWindow $ 1.mouseClicked(MainWindow.java:70)在   java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)     在java.awt.Component.processMouseEvent(Component.java:6536)在   javax.swing.JComponent.processMouseEvent(JComponent.java:3324)在   java.awt.Component.processEvent(Component.java:6298)在   java.awt.Container.processEvent(Container.java:2236)在   java.awt.Component.dispatchEventImpl(Component.java:4889)在   java.awt.Container.dispatchEventImpl(Container.java:2294)在   java.awt.Component.dispatchEvent(Component.java:4711)在   java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)     在   java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)     在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)     在java.awt.Container.dispatchEventImpl(Container.java:2280)在   java.awt.Window.dispatchEventImpl(Window.java:2746)在   java.awt.Component.dispatchEvent(Component.java:4711)在   java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)在   java.awt.EventQueue.access $ 500(EventQueue.java:97)在   java.awt.EventQueue $ 3.run(EventQueue.java:709)在   java.awt.EventQueue $ 3.run(EventQueue.java:703)在   java.security.AccessController.doPrivileged(本机方法),位于   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)     在   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)     在java.awt.EventQueue $ 4.run(EventQueue.java:731)在   java.awt.EventQueue $ 4.run(EventQueue.java:729)在   java.security.AccessController.doPrivileged(本机方法),位于   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:728)在   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

================================================ ==========================

  

这是我的PanelSlider代码,

package Library.PanelSlider;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class PanelSlider extends JPanel 
{
    public  enum  direct {
        Left,
        Right,
        Up,
        Down
    };

    public PanelSlider() {
        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        Dimension size = new Dimension(this.getWidth(), this.getHeight());
    }

    public void nextSlidPanel(Component ShowPanel, direct DirectionMove) {
        nextSlidPanel(10, 40, ShowPanel, DirectionMove);      
    }

    public void nextSlidPanel(int SpeedPanel, Component ShowPanel, direct DirectionMove) {
        nextSlidPanel(SpeedPanel, 40, ShowPanel, DirectionMove);
    }   

    public void nextSlidPanel(int SpeedPanel, int TimeSpeed, Component ShowPanel, direct DirectionMove) { 
        if (!ShowPanel.getName().equals(getCurrentComponentShow(this))) { 
            Component currentComp = getCurrentComponent(this);           
            ShowPanel.setVisible(true);                     
            JPanelSlidingListener sl = new JPanelSlidingListener(SpeedPanel, currentComp, ShowPanel, DirectionMove);
            Timer t = new Timer(TimeSpeed, sl);            
            sl.timer = t;           
            t.start();  
        } 
        refresh();
    }

    public void previous(direct direct) {
        Component currentComp = getCurrentComponent(this);
        Component previousComp = getPreviousComponent(this);
        Rectangle b = currentComp.getBounds();
        previousComp.setVisible(true);
        JPanelSlidingListener sl = new JPanelSlidingListener(10, currentComp, previousComp, direct);
        Timer t = new Timer(40, sl);
        sl.timer = t;
        t.start();
        refresh();
    }

    public void next(direct direct) {
        Component currentComp = getCurrentComponent(this);
        Component nextComp = getNextComponent(this);
        Rectangle b = currentComp.getBounds();
        nextComp.setVisible(true);
        JPanelSlidingListener sl = new JPanelSlidingListener(10, currentComp, nextComp, direct);
        Timer t = new Timer(40, sl);
        sl.timer = t;
        t.start();
        refresh();
    }

    public Component getCurrentComponent(Container parent) {
        Component comp = null;
        int n = parent.getComponentCount();
        for (int i = 0 ; i < n ; i++) {
            comp = parent.getComponent(i);
            if (comp.isVisible()) {
                return comp;
            }
        }
        return comp;
    }

    public Component getNextComponent(Container parent) {
        int n = parent.getComponentCount();
        for (int i = 0 ; i < n ; i++) {
            Component comp = parent.getComponent(i);
            if (comp.isVisible()) {
                int currentCard = (i + 1) % n;
                comp = parent.getComponent(currentCard);
                return comp;
            }
        }
        return null;
    }

    public Component getPreviousComponent(Container parent) {
        int  n = parent.getComponentCount();
        for (int i = 0 ; i < n ; i++) {
            Component comp = parent.getComponent(i);

            if (comp.isVisible()) {
                int currentCard = ((i > 0) ? i - 1 : n - 1);
                comp = parent.getComponent(currentCard);
                return comp;
            }
        }
        return null;
    }

    public String getCurrentComponentShow(Container parent) {
        String PanelName = null;
        Component comp = null;
        int n = parent.getComponentCount();
        for (int i = 0 ; i < n ; i++) {
            comp = parent.getComponent(i);
            if (comp.isVisible()) {
                PanelName = comp.getName();
                return PanelName;
            }
        }
        return PanelName;
    }

    public class JPanelSlidingListener implements ActionListener {
        Component HidePanel;
        Component ShowPanel;
        int steps;
        int step = 0;
        Timer timer;
        direct direct;

        public  JPanelSlidingListener(int steps, Component HidePanel, Component ShowPanel, direct direct) {
            this.steps = steps;
            this.HidePanel = HidePanel;
            this.ShowPanel = ShowPanel;
            this.direct = direct;
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            Rectangle bounds = HidePanel.getBounds();
            int shift = bounds.width/steps;
            int shiftup = bounds.height/steps;
            switch(direct) 
            {
                case Left:
                    HidePanel.setLocation(bounds.x - shift, bounds.y);
                    ShowPanel.setLocation(bounds.x - shift + bounds.width, bounds.y);
                    break;
                case Right:
                    HidePanel.setLocation(bounds.x + shift, bounds.y);
                    ShowPanel.setLocation(bounds.x + shift - bounds.width, bounds.y);
                    break;

                case Up:
                    HidePanel.setLocation(bounds.x, bounds.y - shiftup);
                    ShowPanel.setLocation(bounds.x, bounds.y - shiftup + bounds.height);
                    break;
                case Down:
                    HidePanel.setLocation(bounds.x, bounds.y + shiftup);
                    ShowPanel.setLocation(bounds.x, bounds.y + shiftup - bounds.height);
                    break; 
            }

            repaint();
            step++;

            if (step == steps) {
                timer.stop();
                HidePanel.setVisible(false);
            }
        }
    }

    public void refresh() {
        revalidate();
        repaint();
    } 
}
  

当我单击前两个错误行时,它将向我显示这些代码。但是我   无法弄清楚出了什么问题。

public void nextSlidPanel(int SpeedPanel, Component ShowPanel, direct DirectionMove) {
        nextSlidPanel(SpeedPanel, 40, ShowPanel, DirectionMove);
    }   

    public void nextSlidPanel(int SpeedPanel, int TimeSpeed, Component ShowPanel, direct DirectionMove) { 
        if (!ShowPanel.getName().equals(getCurrentComponentShow(this))) { 
            Component currentComp = getCurrentComponent(this);           
            ShowPanel.setVisible(true);                     
            JPanelSlidingListener sl = new JPanelSlidingListener(SpeedPanel, currentComp, ShowPanel, DirectionMove);
            Timer t = new Timer(TimeSpeed, sl);            
            sl.timer = t;           
            t.start();  
        } 
        refresh();
    }

有人可以帮助我吗?我非常感谢。预先感谢。

0 个答案:

没有答案