java - 以编程方式交换 JFrame 中的两个 JPanel

标签 java swing jpanel awt swap

我正在尝试完成上述功能,但收效甚微。 我使用 2 列和 2 行的 GridLayout 向用户展示一个类似拼图的游戏,其中有 4 个(200x200 像素)JPanel(3 个颜色,1 个默认 bgColor)填充整个 contentPane。如果彩色面板位于灰色面板旁边,则单击彩色面板会在评估中解析。如果是这样,他们应该交换。我已经完成了每一步,直到最后一步,他们交换了。代码如下:

public class MainClass extends JFrame {
//Generated
private static final long serialVersionUID = 4710628273679698058L;

private SpecialPanel redPanel;
private SpecialPanel greenPanel;
private SpecialPanel yellowPanel;
private SpecialPanel grayPanel;

public MainClass() {
    super("Puzzle");

    initPanels();

    setSize(410, 410);
    setLayout(new GridLayout(2, 2));

    this.add(redPanel);
    this.add(greenPanel);
    this.add(grayPanel);
    this.add(yellowPanel);
}
private void initPanels() {
    redPanel = new SpecialPanel();
    greenPanel = new SpecialPanel();
    yellowPanel = new SpecialPanel();
    grayPanel = new SpecialPanel();

    grayPanel.setGreyPanel(true);

    redPanel.setBackground(Color.RED);
    greenPanel.setBackground(Color.GREEN);
    yellowPanel.setBackground(Color.YELLOW);
    grayPanel.setBackground(this.getBackground());

    redPanel.setSize(200, 200);
    greenPanel.setSize(200, 200);
    yellowPanel.setSize(200, 200);
    grayPanel.setSize(200, 200);

    MouseAdapter ma = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            super.mouseClicked(arg0);

            SpecialPanel sp = (SpecialPanel) arg0.getComponent();

            if (sp.getIsGray()) {
                //Do nothing
            } else if (checkIfNeighbourToGray(sp, grayPanel)) {
                //Swap them
                System.out.println("Swap notification");
                swap(sp, grayPanel);                
                //Update UI
            } else {
                //Again, do nothing for the clicked panel is diagonal to the gray panel
            }
        }

        private boolean checkIfNeighbourToGray(SpecialPanel sp, SpecialPanel grayPanel) {

            Point startPointSp = sp.getLocation();
            double x = startPointSp.getX();
            double y = startPointSp.getY();

            double width = sp.getWidth();
            double height = sp.getHeight();

            Point grayPoint = grayPanel.getLocation();
            double xG = grayPanel.getX();
            double yG = grayPanel.getY();

            double widthG = grayPanel.getWidth();
            double heightG = grayPanel.getHeight();

            //Gray panel is RIGHT of clicked one
            if (x + width == xG && y + height == yG + heightG) {
                return true;                    
            }
            //Gray panel is LEFT of clicked one
            else if (x - width == xG && y + height == yG + heightG) {
                return true;
            }
            //Gray panel is ABOVE of clicked one
            else if (x == xG && x + width == xG + widthG) {
                return true;
            }
            //Gray panel is UNDER of clicked one
            else if (xG == x && yG + widthG == x + width) {
                return true;
            }

            return false;
        }

        private void swap(SpecialPanel sp, SpecialPanel grayPanel) {
            //Swap logic
        }

    };
    redPanel.addMouseListener(ma);
    greenPanel.addMouseListener(ma);
    yellowPanel.addMouseListener(ma);
    grayPanel.addMouseListener(ma);

}
public static void main(String[] args) {
    MainClass mc = new MainClass();
    mc.setVisible(true);

}

}

类 SpecialPanel 正在扩展 JPanel,仅使用一个新的 boolean 属性 IsGrayPanel 最初设置为 false + getter 和 setter。 注意:这是以“我有基本的 Swing 技能”的方式完成的,尽管我在此期间学到了更多关于 java swing 的知识,但当时我对基本的 swing 功能的了解有限,所以我应该保持这种方式。 因此我的问题是如何正确交换彼此相邻的两个面板,包括 UI 更新?

最佳答案

  • 不需要将 JPanels 移动到容器中,否则您需要使用一堆毫无用处的代码从容器中删除两个 JPanels使用两个索引,然后使用交换索引布局回容器,

  • (如果仅涉及 Color)仅在两个 JPanels 之间交换 setBackground(Color.Xxx)

  • 类似拼图的游戏是关于图像的,拼图或扫雷是关于(从你的...中不清楚)

    1. Images作为Icon/ImageIcons放入JLabel而不是JPanel并放在上鼠标事件JLabels之间切换(setIcon())和Icons,将Icons加载到本地变量

    2. (最简单的方法)JToggleButton and Icon ,与 JLabel 的逻辑类似,但关于在 setPressedIcon()

    3. 上显示 Icon

关于java - 以编程方式交换 JFrame 中的两个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17727706/

相关文章:

java - -XX :OnOutOfMemoryError doesn't work for java. lang.OutOfMemoryError:无法创建新的 native 线程

java - TIME_WAIT 中的 tcp 连接不允许重新连接,java

java - SWT中如何将组合体划分为不同的空间

java - 是否有支持通过 websocket 传输二进制文件的 Java/JVM 服务器?

java - 如何将jframe中的组件移动到某个位置

java - JPanel 中没有应用颜色?

java - 为什么它说 next(int) 在 java.util.Random 中具有 protected 访问权限?

java - 初学者 Swing 递归

java - 从 JPanel 内的 JButton 而非 JFrame 替换当前 JPanel

java - 消失时可调整大小的布局 Swing JPanel