简体   繁体   English

移动时如何在另一个JPanel上显示一个JPanel?

[英]How to make a JPanel display over another JPanel when moved?

I have created a card game which allows the users to move the cards, which are JPanels, on top of each other. 我创建了一个纸牌游戏,该游戏允许用户将纸牌(即JPanels)彼此叠放。 However, I noticed that if I attempt to move a card to another cards exact location (ie on top of it), that card will not always be displayed on top of that card. 但是,我注意到,如果我尝试将一张卡移动到另一张卡的确切位置(即在其顶部),则该卡将不会始终显示在该卡的顶部。

For example, lets say we have 5 cards, which where built in order. 例如,假设我们有5张卡,这些卡按顺序构建。

If move card1 to card2's location, then card1 will appear on top of card2. 如果将card1移至card2的位置,则card1将出现在card2的顶部。 However, if I tried to move card5 to card3's location, then card5 will appear underneath card3. 但是,如果我尝试将card5移到card3的位置,则card5将出现在card3下方。

How can can I make is so that the last card that I move will be the one on top? 我该如何做才能使我移动的最后一张卡成为最上面的一张?

However, I noticed that if I attempt to move a card to another cards exact location (ie on top of it), that card will not always be displayed on top of that card. 但是,我注意到,如果我尝试将一张卡移动到另一张卡的确切位置(即在其顶部),则该卡将不会始终显示在该卡的顶部。

This sounds related to the Z-Ordering of components. 这听起来与组件的Z顺序有关。 Basically the default behaviour for Swing is that the last component added to a panel is painted first. 基本上,Swing的默认行为是首先绘制添加到面板的最后一个组件。

So you need to change the Z-Order when you add the card on the panel. 因此,在面板上添加卡片时,您需要更改Z顺序。 You are probably using code like: 您可能正在使用类似以下的代码:

 panel.add( card );

The easy solution is to use: 简单的解决方案是使用:

panel.add(0, card);

Or, when you handle the mousePressed() event when you click on the card your would use: 或者,在单击卡时处理mousePressed()事件时,将使用:

Component child = event.getComponent();
Component parent = child.getParent();
parent.setComponentZOrder(child, 0);

You may also want to look at the Overlap Layout which explains Z-Ordering a little more and provides a layout manager that can allow you to stack cards. 您可能还需要看一下“ 重叠布局” ,它对Z顺序进行了更多说明,并提供了一个布局管理器,可以让您堆叠卡片。

For this purpose card layout is your friend. 为此,卡布局是您的朋友。

How to use card layout https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html 如何使用卡片布局https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

Example uses: 示例使用:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardLayoutDemo implements ItemListener {
    JPanel cards; //a panel that uses CardLayout
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";

    public void addComponentToPane(Container pane) {
        //Put the JComboBox in a JPanel to get a nicer look.
        JPanel comboBoxPane = new JPanel(); //use FlowLayout
        String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);

        //Create the "cards".
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));

        JPanel card2 = new JPanel();
        card2.add(new JTextField("TextField", 20));

        //Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);

        pane.add(comboBoxPane, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
    }

    public void itemStateChanged(ItemEvent evt) {
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, (String)evt.getItem());
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("CardLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        CardLayoutDemo demo = new CardLayoutDemo();
        demo.addComponentToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

You can easily modify it to you fulfill your goal. 您可以轻松地对其进行修改,以实现您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM