java - JPanel z-ordering

标签 java swing jpanel

我有一个基础 JPanel,我将在其上放置其他 JPanel 项目。我想在另一个 JPanel 项目之上显示一个 JPanel

我尝试了下面的代码,但没有成功。

package test;

import java.awt.Color;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JPanelParent extends JFrame
{

    public JPanelParent() throws HeadlessException {
        JPanel nPanel = getNode(10,10,20,20, Color.red,1);
        JPanel nPanel2 = getNode(20,20,20,20, Color.yellow,2);
        JPanel nPanel3 = getNode(30,30,20,20, Color.green,3);
        add(nPanel);
        add(nPanel2);
        add(nPanel3);
    }

    public JPanel getNode(int i, int i1, int j, int j1, Color color, int zOrder){
        JPanel nodePanel = new JPanel();
        nodePanel.setBackground(color);
        nodePanel.setBounds(i,i1,j,j1);
        nodePanel.setOpaque(true);
        nodePanel.setLayout(null);
        return nodePanel;
    }


    public static void main(String argv[]){
        JPanelParent jpp = new JPanelParent();
        jpp.setVisible(true);
        jpp.setSize(300,300);
        jpp.setBackground(Color.black);
    }
}

谢谢

最佳答案

您可以使用 JLayeredPane。查看更多How to Use JLayeredPane

public JPanelParent() throws HeadlessException {
    JPanel nPanel = getNode(10, 10, 20, 20, Color.red, 1);
    JPanel nPanel2 = getNode(20, 20, 20, 20, Color.yellow, 2);
    JPanel nPanel3 = getNode(30, 30, 20, 20, Color.green, 3);
    JLayeredPane pane = new JLayeredPane();
    pane.add(nPanel, new Integer(1));
    pane.add(nPanel2, new Integer(2));
    pane.add(nPanel3, new Integer(3));

    setContentPane(pane);
    getContentPane().setBackground(Color.BLACK);
}

注意:传递的Integer 值是图层顺序。数字越高,层数越高

enter image description here

import java.awt.Color;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JPanelParent extends JFrame {

    public JPanelParent() throws HeadlessException {
        JPanel nPanel = getNode(10, 10, 20, 20, Color.red, 1);
        JPanel nPanel2 = getNode(20, 20, 20, 20, Color.yellow, 2);
        JPanel nPanel3 = getNode(30, 30, 20, 20, Color.green, 3);
        JLayeredPane pane = new JLayeredPane();
        pane.add(nPanel, new Integer(1));
        pane.add(nPanel2, new Integer(2));
        pane.add(nPanel3, new Integer(3));

        setContentPane(pane);
        getContentPane().setBackground(Color.BLACK);
    }

    public JPanel getNode(int i, int i1, int j, int j1, Color color, int zOrder) {
        JPanel nodePanel = new JPanel();
        nodePanel.setBackground(color);
        nodePanel.setBounds(i, i1, j, j1);
        nodePanel.setOpaque(true);
        nodePanel.setLayout(null);
        return nodePanel;
    }

    public static void main(String argv[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanelParent jpp = new JPanelParent();
                jpp.setVisible(true);
                jpp.setSize(300, 300);
                jpp.setBackground(Color.black);
            }
        });

    }
}

旁注

  • Swing 程序应该从事件调度线程运行(正如我在上面的示例中所做的那样)。查看更多信息 Initial Threads

关于java - JPanel z-ordering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21725745/

相关文章:

java - 为什么 Feedly API 没有返回类别流中的所有 id 条目?

Java使用定时器来断开客户端与服务器的连接

java - 继承返回空值

java - 按插入顺序获取选定的JTree节点索引

java - J表: how to align data in the center or how to set row background color alternate

java - 我怎样才能更好地清理和组织我的 View (MVC)

Java - JPanel 只是我的 JFrame 顶部中心的一个小像素

java - 尝试计算在总 (14) 餐中吃鱼/鸡/肉的百分比并显示出来。它用全 0 填充数组

java - 如何使用扩展类的 JPanel 对象?

Java GUI 101 - 更改 JPanel 的标题