我需要动态地在JLayeredPane上移动一些JLabel。 问题是它们不能正确刷新。有时他们正确地移动,有时他们根本不出现。在JLayeredPane上移动JLabel的
我的代码如下所示:
构建面板
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,但它不” t总是显示它... 这里怎么回事...
我在[这个答案](http://stackoverflow.com/a/4687759/522444)中处理过类似的问题。它可能会帮助你。 –
但是,如果您需要更多帮助,请考虑创建并发布[sscce](http://sscce.org),因为根据您的不可运行代码片段,可能无法猜测出您可能会做错什么已经发布到目前为止。 –