【发布时间】:2012-08-25 22:17:44
【问题描述】:
我需要在 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 时定位是正确的,但它并不总是显示它...... 这里出了什么问题...
【问题讨论】:
-
我在this answer 中处理过类似的问题。它可能会对你有所帮助。
-
但是如果您需要更多帮助,请考虑创建并发布sscce,因为根据您无法运行的代码的 sn-ps 可能无法猜测您可能做错了什么到目前为止已经发布了。
标签: java swing jlabel repaint jlayeredpane