gpt4 book ai didi

java - JPanel z-ordering

转载 作者:行者123 更新时间:2023-12-03 19:07:20 25 4
gpt4 key购买 nike

我有一个基础 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com