在java游戏结束时切换面板

时间:2015-10-07 18:03:18

标签: java nullpointerexception netbeans-8

我进行了搜索,但所有答案都是关于cardlayout的,并且我经历过cardlayout需要一个始终可见的面板来执行操作。我有这个游戏.. enter image description here

我想让它显示一个新面板,其中包含消息游戏结束和选项

  1. 再次播放
  2. 退出
    当序列出现时在同一窗口中 我创建了一个名为 GameOver 的新类,并在其构造函数中创建了一个面板,并在其中添加了上述选项。并尝试使用构造函数或新方法等。但是当我在主游戏类中调用它时它会给出空指针异常错误
    更新:这是整个Stack Trace。

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1091)
    at java.awt.Container.add(Container.java:1003)
    at javax.swing.JFrame.addImpl(JFrame.java:564)
    at java.awt.Container.add(Container.java:415)
    at Tic_Tac_new.mouseClicked(Tic_Tac_new.java:132)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6528)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    

    建立成功(总时间:11秒)

  3. 请告诉我怎么能这样做。
    这是添加pGOver

    的主要类条件
    @Override
        public void mouseClicked(MouseEvent e) {
    
                   if (e.getSource()== buttonsArray[0] && turn == true){
                            buttonsArray[0].setText("X");
                            turn = false;
                            checkXb1();
                            buttonsArray[0].setForeground(Color.red);
                            turntxt.setText(turnA);
    
    //                        newGame.remove(mainGamePanel);
                            this.add(GameOver.pGOver);
                        }
    

0 个答案:

没有答案