gpt4 book ai didi

java - 如何管理我的图层?

转载 作者:行者123 更新时间:2023-11-30 04:03:27 25 4
gpt4 key购买 nike

所以我试图将面板分成不同的层,但它不起作用,我做错了什么?如何管理图层?我希望 table 始终位于后面,panel 位于中间,然后 wkp 位于所有内容之上。提前致谢。

public class Main  {
private static JFrame frame = new MyFrame();
private static WhiteKingPanel wkp = new WhiteKingPanel();
private static MyPanel panel = new MyPanel();
private static TablePanel table = new TablePanel();
private static JLayeredPane lpane = new JLayeredPane();

public static void main(String[] args) throws InterruptedException{

EventQueue.invokeLater(new Runnable(){
public void run(){
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
wkp.setOpaque(true);
wkp.setBounds(0, 0, 50, 50);
lpane.add(wkp, new Integer(3), 0);
lpane.add(panel, new Integer(2), 0);
lpane.add(table, new Integer(1), 0);
table.setOpaque(true);
frame.setTitle("ImageTest");
frame.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(150, 100, 512, 512);
frame.add(wkp);
frame.add(table);
frame.pack();
frame.setVisible(true);
WindowListener listener = new Terminator();
frame.addWindowListener(listener);

}

});

}


}

最佳答案

关于 JLayerPane 的问题是您需要为添加的所有组件设置边界。

            wkp.setBounds(0, 0, 100, 100);
panel.setBounds(50, 50, 100, 100);
table.setBounds(100, 100, 100, 100);

还将 JLayerPane 设置为内容 Pane

            frame.setContentPane(lpane);
//frame.add(wkp); No need to add these to the frame
//frame.add(table);
//frame.add(lpane);

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {

private static JFrame frame = new MyFrame();
private static JPanel wkp = new JPanel();
private static JPanel panel = new JPanel();
private static JPanel table = new JPanel();
private static JLayeredPane lpane = new JLayeredPane();

public static void main(String[] args) throws InterruptedException {

EventQueue.invokeLater(new Runnable() {
public void run() {

wkp.setBackground(Color.YELLOW);
panel.setBackground(Color.GREEN);
table.setBackground(Color.BLUE);

frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
wkp.setOpaque(true);
table.setOpaque(true);
panel.setOpaque(true);
lpane.add(wkp, new Integer(3), 0);
lpane.add(panel, new Integer(2), 0);
lpane.add(table, new Integer(1), 0);
wkp.setBounds(0, 0, 100, 100);
panel.setBounds(50, 50, 100, 100);
table.setBounds(100, 100, 100, 100);
frame.setTitle("ImageTest");
frame.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(lpane);
//frame.add(wkp);
//frame.add(table);
//frame.add(lpane);
frame.pack();
frame.setVisible(true);

}
});
}
}

关于java - 如何管理我的图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397067/

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