Programmatically swap two JPanels in JFrame

落花浮王杯 提交于 2019-12-06 13:36:10
mKorbel
  • there wouldn't need to move with JPanels into container, otherwise you woud need to use bunch of quite useless code to remove two JPanels from from contianer with two indexes then to layout back to container with swaping indexes,

  • (if is about Color only) only to swap setBackground(Color.Xxx) betweens two JPanels

  • puzzle-like game is about Images, puzzle or minesweaper is about, (not clear from your ...)

    1. put Images as Icon/ImageIcons to JLabel instead of JPanel and on Mouse Events to switch (setIcon()) with Icons betweens JLabels, load Icons to local variables

    2. (easiest of ways) JToggleButton and Icon, there is logic similair as for JLabel, but about showing Icon on setPressedIcon()

I'm not sure if this works. It looks too easy.

JPanel tempPanel = sp;
sp = grayPanel;
grayPanel = tempPanel;

this.setVisible(false);
this.setVisible(true);
this.validate(); //try this first
this.repaint(); // if it doesnt work, add this function. 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!