有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Slidepuzzle,切换图标

我们正在使用Eclipse在java中制作一个slidepuzzle。我们在按钮上加了Icon。现在我们想用所谓的“空按钮”的图标来切换我们点击的按钮的Icon

int location =(new Integer(e.getActionCommand())).intValue(); //The buttons are in an Array:  "Pieces[]", Location: index
if (Bounds(location,InvisibleButton)) {
//The method Bounds, is one we made to see if the two pieces are next to eachother,
Icon temp;  //Temporar ButtonIcon
temp=Knop[location].getIcon(); 
Pieces[location].setIcon(Piece[InvisibleButton].getIcon()); 
Pieces[InvisibleButton].setIcon(temp); 
InvisibleButton = location; 
}

为什么图标不切换位置

错误:

只有当我们点击空工件旁边的工件时,它才会给出一个错误,我们可以点击所有其他工件。代码是有效的,但当我们想要切换图像时,它就会中断。 以下是错误代码:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Mozaiq.Schuifpuzzelpaneel.actionPerformed(Schuifpuzzelpaneel.java:93)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at     javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6382)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6147)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4744)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:710)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:669)
at java.awt.EventQueue$2.run(EventQueue.java:667)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:683)
at java.awt.EventQueue$3.run(EventQueue.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:680)
at     java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at     java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

共 (2) 个答案

  1. # 1 楼答案

    快速猜测一下:调用repaint()方法

    可能是这样的:

    Pieces[location].repaint();
    

    在你工作的最后

  2. # 2 楼答案

    所以问题似乎是Knop[location]不包含对工件对象的引用。也许这是一个更好的方法:

    int location =(new Integer(e.getActionCommand())).intValue(); 
    //The buttons are in an  Array:  "Pieces[]", Location: index
    if (Bounds(location,InvisibleButton)) {
      //The method Bounds, is one we made to see if the two pieces are next to eachother,
      Icon temp;  //Temporar ButtonIcon
    
      temp=Pieces[location].getIcon(); //<         -Changed this line
    
      Pieces[location].setIcon(Piece[InvisibleButton].getIcon()); 
      Pieces[InvisibleButton].setIcon(temp); 
      InvisibleButton = location; 
    }
    

    错误

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Mozaiq.Schuifpuzzelpaneel.actionPerformed(Schuifpuzzelpaneel.java:136)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at    javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6382)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6147)
    at java.awt.Container.processEvent(Container.java:2083)
    at java.awt.Component.dispatchEventImpl(Component.java:4744)
    at java.awt.Container.dispatchEventImpl(Container.java:2141)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.java:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:710)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:669)
    at java.awt.EventQueue$2.run(EventQueue.java:667)
    at java.security.AccessController.doPrivileged(Native Method)
    at    java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at  java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:683)
    at java.awt.EventQueue$3.run(EventQueue.java:681)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:680)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)