我显然在这里遗漏了一些东西。
如何更改摆动框架以显示全新的面板?
如: 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);