我需要在 JLayeredPane 上动态移动一些 JLabels。问题是它们没有正确刷新。有时它们会正确移动,有时它们根本不会出现。
我的代码如下所示:
构建面板
JLayeredPane gameBoardPane = new JLayeredPane();
gameBoardPane.setLayout( null );
Dimension gameBoardLabelSize = gameBoardLabel.getPreferredSize();
gameBoardLabel.setBounds(30, 30, gameBoardLabelSize.width, gameBoardLabelSize.height);
gameBoardPane.add(gameBoardLabel, new Integer(10));
gameBoardPane.setAlignmentY(Component.TOP_ALIGNMENT);
// ...
JPanel gamePane = new JPanel();
gamePane.setLayout( new BoxLayout(gamePane, BoxLayout.LINE_AXIS) );
gamePane.add(gameBoardPane);
gamePane.add(gameControlPane);
// ...
Container contentPane = getContentPane();
contentPane.add(gamePane, BorderLayout.PAGE_START);
SnLFrame.setSize( 720,600 );
添加 JLabel:
Coordinates position = new Coordinates();
position = convertToCoordinates(blockNumber);
position.x = position.x * 50;
position.y = position.y * 50;
Dimension smallPlayer1IconLabelSize = smallPlayer1IconLabel.getPreferredSize();
smallPlayer1IconLabel.setBounds(position.x, position.y, smallPlayer1IconLabelSize.width, smallPlayer1IconLabelSize.height);
gameBoardPane.add(smallPlayer1IconLabel, new Integer(100));
SnLFrame.invalidate();
SnLFrame.validate();
SnLFrame.repaint();
当它确实显示 JLabel 时定位是正确的,但它并不总是显示它......这里出了什么问题......