所以我试图将我的面板分成不同的层,但它不起作用,我做错了什么?如何管理图层?我希望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);
}
});
}
}
答案 0 :(得分:2)
关于JLayerPane
的问题是你需要为你添加的所有组件设置界限。
wkp.setBounds(0, 0, 100, 100);
panel.setBounds(50, 50, 100, 100);
table.setBounds(100, 100, 100, 100);
同时将JLayerPane
设置为内容窗格
frame.setContentPane(lpane);
//frame.add(wkp); No need to add these to the frame
//frame.add(table);
//frame.add(lpane);
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);
}
});
}
}