如何设置框架以在秋千中显示新面板?

时间:2012-12-13 21:37:54

标签: java swing jframe jpanel

我显然在这里遗漏了一些东西。

如何更改swing框架以显示全新的面板?

如下: 1.显示带有新内容的全新面板                   要么 2.使用我新添加的

显示相同的面板

我在另一个程序中显示了我的新面板,它可以正确创建和显示。

然而,当带有我的按钮的面板先前已经在框架中时,它从不显示我的新面板。

我调用contentPane.revalidate();在我的代码中几次更新我的按钮,它们完美地更新它们只会在我尝试删除旧按钮并添加新按钮时出现问题。

将我的新面板添加到contentPane之后,我尝试过:

contentPane.validate();
contentPane.revalidate();
contentPane.removeAll();        
contentPane.repaint();
setContentPane(contentPane);

但它永远不会改变。

 @Override
public void actionPerformed(ActionEvent e) 
{
    String buttonPressed = e.getActionCommand();
    int pos = Integer.valueOf(buttonPressed);

    if (Control.model.cardsRemaining == 0) 
    {
        contentPane.removeAll();
        //contentPane.validate();
        //contentPane = new JPanel();
        //contentPane.add(createSuccess());
        contentPane = createSuccess();
        contentPane.revalidate();
        contentPane.repaint();      

        System.out.println("entered success");
    }

    else 
    {
        System.out.println("Cards left: " + Control.model.cardsRemaining);
        action = Control.model.ReceiveCardsTurned(pos);

        keypadArray[pos].setIcon(myIcons[pos]);
        currentTime.setText("" + Control.model.time);
        currentScore.setText("" + Control.model.score);

        System.out.println("this card: " + pos + "last card: "
                + Control.model.lastCard);

        if (action == "unturn") 
        {
            try 
            {
                Thread.sleep(1000);
            }

            catch (InterruptedException e1) 
            {
                e1.printStackTrace();
            }

            contentPane.revalidate();
            keypadArray[pos].setIcon(back);
            keypadArray[Control.model.lastCard].setIcon(back);
        }

        //System.out.println(action);
    }
}



private JPanel createSuccess() 
{
    //final 
    JPanel Success = new JPanel();
    JLabel image = new JLabel(success);
    Success.add(image);

    return Success; 
}

我的代码中还有更多内容:

ImageIcon success = new ImageIcon("icons/success.png");

JPanel contentPane = new JPanel();

刚试过:

contentPane = createSuccess(); 
frame.setContentPane(contentPane);  
frame.pack();                

第268行是:frame.setContentPane(contentPane);

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pairs.GUI.actionPerformed(GUI.java:268)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

谢谢我的朋友,他的工作非常完美:

contentPane.removeAll();
contentPane.add(createSuccess());
setContentPane(contentPane);

1 个答案:

答案 0 :(得分:4)

您的代码为变量contentPane指定了一个新的引用,这不会改变屏幕上已有的内容,它只是将contentPane引用的内容(可能是屏幕上的内容)移动到其他内容,离开屏幕上的内容究竟在哪里。

尝试将新面板添加到contentPane

contentPane.removeAll();
contentPane.add(createSuccess());

如果您想切换视图,CardLayout是更好的选择。

如果您只是想“重置”视图,则必须实际编写代码以将视图的内容返回到“默认”状态。在这种情况下,您实际上更擅长创建包含视图所需字段的自定义组件并提供管理方法。