如何为JLabel制作动画而不是推动其他组件

时间:2023-01-28 00:12:55

I'm trying to animate the JLabel, when i'm setting it visible true. And when my JLbael is showing up, other components are pushed down. Is there any way to keep jlabel in one position when visible and not visible? I'm using gridbaglayout.

我正在尝试为JLabel设置动画,当我将其设置为真实时。当我的JLbael出现时,其他组件被推倒。有没有办法在可见和不可见时将jlabel保持在一个位置?我正在使用gridbaglayout。

1 个解决方案

#1


3  

The visible state of a component will affect how most layout managers will treat the component, typically, they ignore them when they aren't visible.

组件的可见状态将影响大多数布局管理器处理组件的方式,通常,当它们不可见时,它们会忽略它们。

You could write your own layout manager, but that's kind of annoying and time consuming. You could use a "fake" component (adding the label inside it), but that's kind of messy and hard to keep the two components in sync or you could "fake" the visibility state...

您可以编写自己的布局管理器,但这有点烦人且耗时。您可以使用“假”组件(在其中添加标签),但这有点混乱,难以保持两个组件同步或您可以“伪造”可见性状态...

Start by creating a custom component which extends from JLabel, override it's paintComponent and using a AlphaComposite change the transparency level of the content which is painted. This way, you could animate the "alpha" level instead of the visibility level...

首先创建一个从JLabel扩展的自定义组件,覆盖它的paintComponent并使用AlphaComposite更改绘制内容的透明度级别。这样,您可以设置“alpha”级别的动画而不是可见性级别......

如何为JLabel制作动画而不是推动其他组件

import java.awt.AlphaComposite;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private FadeLabel fadeLabel;
        private Timer timer;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            fadeLabel = new FadeLabel("Rabbit");
            fadeLabel.setAlpha(0f);

            add(new JLabel("Watch me pull a..."), gbc);
            add(fadeLabel, gbc);

            JButton btn = new JButton("out of my hat");
            add(btn, gbc);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!timer.isRunning()) {
                        fadeLabel.setAlpha(0);
                        timer.start();
                    }
                }
            });

            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    float alpha = fadeLabel.getAlpha();
                    alpha += 0.01;
                    if (alpha >= 1.0f) {
                        alpha = 1.0f;
                        timer.stop();
                    }
                    fadeLabel.setAlpha(alpha);
                }
            });

        }

    }

    public class FadeLabel extends JLabel {

        private float alpha = 1f;

        public FadeLabel(String text, Icon icon, int horizontalAlignment) {
            super(text, icon, horizontalAlignment);
        }

        public FadeLabel(String text, int horizontalAlignment) {
            super(text, horizontalAlignment);
        }

        public FadeLabel(String text) {
            super(text);
        }

        public FadeLabel(Icon image, int horizontalAlignment) {
            super(image, horizontalAlignment);
        }

        public FadeLabel(Icon image) {
            super(image);
        }

        public FadeLabel() {
        }

        public float getAlpha() {
            return alpha;
        }

        public void setAlpha(float value) {
            if (value != alpha) {
                float old = alpha;
                this.alpha = value;
                firePropertyChange("alpha", old, alpha);
                repaint();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
            super.paintComponent(g2d);
            g2d.dispose();
        }

    }
}

Now, you can just as easily set the alpha level from 0 to 1 and you will get the same instance change as you would if you changed the visibility state, but without the bumping of surrounding components

现在,您可以轻松地将alpha级别从0设置为1,并且您将获得与更改可见性状态时相同的实例更改,但不会出现周围组件的碰撞

#1


3  

The visible state of a component will affect how most layout managers will treat the component, typically, they ignore them when they aren't visible.

组件的可见状态将影响大多数布局管理器处理组件的方式,通常,当它们不可见时,它们会忽略它们。

You could write your own layout manager, but that's kind of annoying and time consuming. You could use a "fake" component (adding the label inside it), but that's kind of messy and hard to keep the two components in sync or you could "fake" the visibility state...

您可以编写自己的布局管理器,但这有点烦人且耗时。您可以使用“假”组件(在其中添加标签),但这有点混乱,难以保持两个组件同步或您可以“伪造”可见性状态...

Start by creating a custom component which extends from JLabel, override it's paintComponent and using a AlphaComposite change the transparency level of the content which is painted. This way, you could animate the "alpha" level instead of the visibility level...

首先创建一个从JLabel扩展的自定义组件,覆盖它的paintComponent并使用AlphaComposite更改绘制内容的透明度级别。这样,您可以设置“alpha”级别的动画而不是可见性级别......

如何为JLabel制作动画而不是推动其他组件

import java.awt.AlphaComposite;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private FadeLabel fadeLabel;
        private Timer timer;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            fadeLabel = new FadeLabel("Rabbit");
            fadeLabel.setAlpha(0f);

            add(new JLabel("Watch me pull a..."), gbc);
            add(fadeLabel, gbc);

            JButton btn = new JButton("out of my hat");
            add(btn, gbc);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!timer.isRunning()) {
                        fadeLabel.setAlpha(0);
                        timer.start();
                    }
                }
            });

            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    float alpha = fadeLabel.getAlpha();
                    alpha += 0.01;
                    if (alpha >= 1.0f) {
                        alpha = 1.0f;
                        timer.stop();
                    }
                    fadeLabel.setAlpha(alpha);
                }
            });

        }

    }

    public class FadeLabel extends JLabel {

        private float alpha = 1f;

        public FadeLabel(String text, Icon icon, int horizontalAlignment) {
            super(text, icon, horizontalAlignment);
        }

        public FadeLabel(String text, int horizontalAlignment) {
            super(text, horizontalAlignment);
        }

        public FadeLabel(String text) {
            super(text);
        }

        public FadeLabel(Icon image, int horizontalAlignment) {
            super(image, horizontalAlignment);
        }

        public FadeLabel(Icon image) {
            super(image);
        }

        public FadeLabel() {
        }

        public float getAlpha() {
            return alpha;
        }

        public void setAlpha(float value) {
            if (value != alpha) {
                float old = alpha;
                this.alpha = value;
                firePropertyChange("alpha", old, alpha);
                repaint();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
            super.paintComponent(g2d);
            g2d.dispose();
        }

    }
}

Now, you can just as easily set the alpha level from 0 to 1 and you will get the same instance change as you would if you changed the visibility state, but without the bumping of surrounding components

现在,您可以轻松地将alpha级别从0设置为1,并且您将获得与更改可见性状态时相同的实例更改,但不会出现周围组件的碰撞