我目前正在制作一个类似国际象棋的棋盘游戏,所以我制作了一个 11*11 的场地。在每个字段上都应该有一个 JButton(在默认层上),并且在更高的层上是一个可移动的 JLabel。但标签仍将按钮推开。这是简化的代码:public class Demo { public static void main(String[] args) { ImageIcon image = new ImageIcon("C:src\\myImage.png"); JFrame frame = new JFrame(); JPanel mainPanel = new JPanel(); JLayeredPane[] tileLayeredPane = new JLayeredPane[121]; JButton button = new JButton(); JLabel label = new JLabel(); label.setIcon(image); button.setText("I am not visible!"); for (int i = 0; i < tileLayeredPane.length; i++) { // creates 121 JLabels tileLayeredPane[i] = new JLayeredPane(); tileLayeredPane[i].setLayout(new BoxLayout(tileLayeredPane[i], BoxLayout.Y_AXIS)); tileLayeredPane[i].setOpaque(true); } tileLayeredPane[0].add(button, JLayeredPane.DEFAULT_LAYER); tileLayeredPane[0].add(label, JLayeredPane.PALETTE_LAYER); mainPanel.setLayout(new GridLayout(11, 11)); for(int i = 0; i < 121; i++) { mainPanel.add(tileLayeredPane[i]); } frame.add(mainPanel); frame.setVisible(true); }}
添加回答
举报
0/150
提交
取消