Java 在JFrame中以编程方式交换两个JPanel
我正在尝试完成上述功能,但收效甚微。 我使用2列2行的GridLayout向用户展示了一个类似于拼图的游戏,其中有4个(200x200像素)JPanel(3种颜色,1种默认bgColor)填充整个contentPane。如果面板靠近灰色面板,则单击彩色面板将在评估中解析。如果是这样,他们应该交换。我已经完成了每一步,直到最后一步,在那里他们交换。代码如下:Java 在JFrame中以编程方式交换两个JPanel,java,swing,jpanel,awt,swap,Java,Swing,Jpanel,Awt,Swap,我正在尝试完成上述功能,但收效甚微。 我使用2列2行的GridLayout向用户展示了一个类似于拼图的游戏,其中有4个(200x200像素)JPanel(3种颜色,1种默认bgColor)填充整个contentPane。如果面板靠近灰色面板,则单击彩色面板将在评估中解析。如果是这样,他们应该交换。我已经完成了每一步,直到最后一步,在那里他们交换。代码如下: public class MainClass extends JFrame { //Generated private static fin
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类使用一个新的布尔属性IsGrayPanel扩展了JPanel,该属性最初设置为false+getter和setter。
注意:这是以一种“我有基本的Swing技能”的方式完成的,虽然我在这段时间里学到了更多关于java Swing的知识,但我当时只限于基本的Swing功能,所以我应该保持这种方式。
因此,我的问题是如何正确地交换相邻的两个面板,包括UI更新?我不确定这是否有效。看起来太容易了
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.
- 不需要将
移动到容器中,否则您需要使用一堆非常无用的代码从包含两个JPanels
索引的容器中删除两个
,然后使用JPanels
交换索引将其布局回容器中
- (如果是关于
only)仅在两个Color
jpanel之间交换
setBackground(Color.Xxx)
是关于益智类游戏
,益智类或地雷类游戏是关于,(你不清楚…)图像
- 将
作为Images
放置到Icon/ImageIcons
而不是JLabel
,并在JPanel
上使用Mouse Events
图标切换(
),将setIcon()
图标加载到本地变量
- (最简单的方法)有与
相似的逻辑,但是关于在JLabel
setPressedIcon()上显示
图标