以编程方式在JFrame中交换两个JPanel [英] Programmatically swap two JPanels in JFrame

查看:151
本文介绍了以编程方式在JFrame中交换两个JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to accomplish the above functionality, and am having little success. I am using GridLayout with 2 columns and 2 rows to show the user a puzzle-like game, where there are 4 (200x200 pixel) JPanels (3 colored, 1 default bgColor) which fill the whole contentPane. Clicking on a colored panel resolves in an assessment if the panel is next to the gray panel. If so, they should swap. I have accomplished every step to the last one, where they swap. Here's the code:

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只有一个新的布尔属性IsGrayPanel最初设置为false + getter和setter。
注意:这是以我有基本的Swing技巧的方式完成的,虽然我在此期间学到了更多关于java swing的知识,但当时我只是受限于基本的挥杆功能,所以我应该保持它办法。
因此我的问题是如何正确交换彼此相邻的两个面板,包括UI更新?

The class SpecialPanel is extending JPanel with just a new Boolean property IsGrayPanel initially set to false + getter and setter. NOTE: This is being done in a "I have basic Swing skills" manner, although I have learned more in the meantime about java swing, I was just limited back then with basic swing functionality, so I should keep it that way. Therefore my question is how to properly swap the two panels which are next to each other, including UI update?

推荐答案


  • 不需要将 JPanels 移动到容器中,否则你需要使用大量无用的代码来删除两个来自带有两个索引的contianer的 JPanels 然后使用交换索引布局回容器

    • 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,

      (仅限于 Color )仅交换 setBackground(Color.Xxx)两个之间 JPanels

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

      类似益智游戏约为图像,拼图或minesweaper即将发布,(不清楚...)

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


      1. 图像设为图标/ ImageIcons JLabel 而不是 JPanel 鼠标事件切换( setIcon())与图标之间 JLabels ,加载图标到本地变量

      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

      (最简单的方法) JToggleButton和Icon ,有 JLabel 的逻辑类似物,但是关于在上显示图标 setPressedIcon()

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


    • 这篇关于以编程方式在JFrame中交换两个JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆