1

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

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

如: 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);
4

1 回答 1

4

您的代码为变量分配了一个新的引用contentPane,这不会改变屏幕上已经存在的内容,它只是将contentPane引用的内容(可能是屏幕上的内容)移动到其他内容,而将屏幕上的内容准确地留在原处。

尝试将新面板添加到contentPane

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

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

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

于 2012-12-13T22:21:44.660 回答