Java 移动JLabel';它在一个红色的窗格上

Java 移动JLabel';它在一个红色的窗格上,java,swing,jlabel,repaint,jlayeredpane,Java,Swing,Jlabel,Repaint,Jlayeredpane,我需要在JLayeredPane的顶部动态移动一些JLabel。 问题是它们不能正确刷新。有时它们移动正确,有时根本不出现 我的代码如下所示: 构建面板 JLayeredPane gameBoardPane = new JLayeredPane(); gameBoardPane.setLayout( null ); Dimension gameBoardLabelSize = gameBoardLabel.getPreferredSize(); gameBoardLabel.setBounds(

我需要在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时,定位是正确的,但并不总是显示它。。。
这里出了什么问题…

我已经浏览了气垫船之前的答案,我很确定它会满足您的需求,但我忍不住要玩一玩

这只是一个例子。动画引擎是垃圾(互联网上有更好的框架可用),但它有点有趣;)

public类AnimateGlassPane{
公共静态void main(字符串[]args){
新MyFrame();
}
公共静态类MyFrame扩展了JFrame{
公共MyFrame(){
设定标题(“测试”);
设置大小(800600);
setLocationRelativeTo(空);
setDefaultCloseOperation(关闭时退出);
setGlassPane(新的MyGlassPane());
getGlassPane().setVisible(true);
setVisible(真);
}
}
公共静态类MyGlassPane扩展了JPanel{
专用JLabel[]标签;
私有JLabel随机;
私人定时器;
私有浮动进度=0f;
私人浮动增量=0.1f;
公共玻璃窗(){
设置不透明(假);
setLayout(空);
标签=新的JLabel[4];
标签[0]=新的JLabel(“向上/向下”);
标签[1]=新的JLabel(“向下/向上”);
标签[2]=新的JLabel(“左/右”);
标签[3]=新的JLabel(“右/左”);
for(int index=0;index1){
进展=1;
增量=-0.1f;
}
int width=getWidth()-1;
int height=getHeight()-1;
int x=(宽度-标签[0].getWidth())/2;
int y=数学圆((高度*进度));
如果(y+标签[0].getHeight()>height){
y=高度-标签[0]。getHeight();
}else if(y<0){
y=0;
}
标签[0]。设置位置(x,y);
y=数学圆((高度*(1f-进度));
如果(y+标签[1].getHeight()>height){
y=高度-标签[1]。getHeight();
}else if(y<0){
y=0;
}
标签[1]。设置位置(x,y);
y=(高度-标签[2].getHeight())/2;
x=数学四舍五入(宽度*进度);
如果(x+标签[2].getWidth()>width){
x=宽度-标签[2]。getWidth();
}else如果(x<0){
x=0;
}
标签[2]。设置位置(x,y);
x=数学圆(宽度*(1f-进度));
如果(x+标签[3].getWidth()>width){
x=宽度-标签[3]。getWidth();
}else如果(x<0){
x=0;
}
标签[3]。设置位置(x,y);
重新油漆();
int值=(int)Math.round(Math.random()*100d);
如果(值%5==0){
random.setVisible(!random.isVisible());
x=(int)Math.round(Math.random()*宽度);
y=(int)Math.round(Math.random()*高度);
如果(x+random.getWidth()>width){
x=width-random.getWidth();
}else如果(x<0){
x=0;
}
如果(y+random.getHeight()>height){
y=高度-随机。getHeight();
}else if(y<0){
y=0;
}
随机设定位置(x,y);
}
}
});
timer.setRepeats(真);
timer.setCoalesce(真);
timer.start();
}
}
}

问题实际上在我的定位算法中。

我在中处理过类似的问题。这可能对你有帮助。但是如果你需要更多的帮助,请考虑创建和发布一个,因为你可能无法猜测你可能犯了什么错误。
public class AnimateGlassPane {

    public static void main(String[] args) {
        new MyFrame();
    }

    public static class MyFrame extends JFrame {

        public MyFrame() {
            setTitle("Testing");
            setSize(800, 600);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            setGlassPane(new MyGlassPane());
            getGlassPane().setVisible(true);

            setVisible(true);              
        }

    }

    public static class MyGlassPane extends JPanel {

        private JLabel[] labels;
        private JLabel random;
        private Timer timer;
        private float progress = 0f;
        private float increment = 0.1f;

        public MyGlassPane() {

            setOpaque(false);
            setLayout(null);

            labels = new JLabel[4];
            labels[0] = new JLabel("Up/Down");
            labels[1] = new JLabel("Down/Up");
            labels[2] = new JLabel("Left/Right");
            labels[3] = new JLabel("Right/Left");

            for (int index = 0; index < labels.length; index++) {
                labels[index].setSize(labels[index].getPreferredSize());
                add(labels[index]);
            }

            random = new JLabel("Look at me!");
            random.setSize(random.getPreferredSize());
            random.setVisible(false);
            random.setOpaque(true);
            random.setBackground(Color.RED);
            random.setForeground(Color.YELLOW);

            add(random);

            timer = new Timer(125, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    progress += increment;
                    if (progress < 0) {
                        progress = 0;
                        increment = 0.1f;
                    } else if (progress > 1) {
                        progress = 1;
                        increment = -0.1f;
                    }

                    int width = getWidth() - 1;
                    int height = getHeight() - 1;

                    int x = (width - labels[0].getWidth()) / 2;
                    int y = Math.round((height * progress));
                    if (y + labels[0].getHeight() > height)  {
                        y = height - labels[0].getHeight();
                    } else if (y < 0)  {
                        y = 0;
                    }

                    labels[0].setLocation(x, y);

                    y = Math.round((height * (1f - progress)));
                    if (y + labels[1].getHeight() > height)  {
                        y = height - labels[1].getHeight();
                    } else if (y < 0)  {
                        y = 0;
                    }

                    labels[1].setLocation(x, y);

                    y = (height - labels[2].getHeight()) / 2;
                    x = Math.round(width * progress);
                    if (x + labels[2].getWidth() > width)  {
                        x = width - labels[2].getWidth();
                    } else if (x < 0)  {
                        x = 0;
                    }

                    labels[2].setLocation(x, y);

                    x = Math.round(width * (1f - progress));
                    if (x + labels[3].getWidth() > width)  {
                        x = width - labels[3].getWidth();
                    } else if (x < 0)  {
                        x = 0;
                    }

                    labels[3].setLocation(x, y);

                    repaint();

                    int value = (int)Math.round(Math.random() * 100d);
                    if (value % 5 == 0) {

                        random.setVisible(!random.isVisible());
                        x = (int)Math.round(Math.random() * width);
                        y = (int)Math.round(Math.random() * height);

                        if (x + random.getWidth() > width) {
                            x = width - random.getWidth();
                        } else if (x < 0) {
                            x = 0;
                        }
                        if (y + random.getHeight() > height) {
                            y = height - random.getHeight();
                        } else if (y < 0) {
                            y = 0;
                        }

                        random.setLocation(x, y);

                    }
                }
            });

            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }
    }
}