Java 图像未按计划显示,I';我正在尝试使用JLabel更改JFrame中的图像

Java 图像未按计划显示,I';我正在尝试使用JLabel更改JFrame中的图像,java,swing,jframe,jlabel,imageicon,Java,Swing,Jframe,Jlabel,Imageicon,我的代码是 JFrame frame=new JFrame("Change pic"); ImageIcon image=new ImageIcon("image.jpg"); ImageIcon image1=new ImageIcon("image1.jpg"); frame.setSize(image.getIconWidth(),image.getIconHeight()); frame.add(new JLabel(image)); Thread.sleep(3000); frame.

我的代码是

JFrame frame=new JFrame("Change pic");
ImageIcon image=new ImageIcon("image.jpg");
ImageIcon image1=new ImageIcon("image1.jpg");
frame.setSize(image.getIconWidth(),image.getIconHeight());
frame.add(new JLabel(image));
Thread.sleep(3000);
frame.add(new JLabel(image1));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
使用上述代码时,第一个图像(“图像”)不会出现,只有第二个图像(“图像2”)出现。即使不使用line Thread.sleep(3000)也会得到相同的结果。 但是当我以

JFrame frame=new JFrame("Change pic");
ImageIcon image=new ImageIcon("image.jpg");
ImageIcon image1=new ImageIcon("image1.jpg");
frame.setSize(image.getIconWidth(),image.getIconHeight());
frame.add(new JLabel(image));
frame.setVisible(true);
Thread.sleep(3000);
frame.add(new JLabel(image1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
然后第一个图像(“图像”)出现在第二个图像(“图像1”)的顶部,我只能在向右延伸帧时看到第二个图像

所以我的问题是为什么第二个代码的第一个图像(“图像”)出现在顶部?我需要做哪些更改,以使第二个图像(“图像1”)在3秒后替换第一个图像(“图像”)

对不起,如果问题是愚蠢的。任何帮助都将不胜感激。 事实上,我正试图开发一个远程桌面共享应用程序,我被困在这里了

  • JFrame
    具有默认的
    BorderLayout
    。添加组件而不指定
    BorderLayout
    位置将自动将其添加到
    BorderLayout.CENTER
    。每个位置只能有一个组件。您添加的最后一个组件将是唯一可见的组件。更多信息请访问

  • 避免使用
    Thread.sleep
    。如果需要动画,请使用
    javax.swing.Timer
    。更多信息请访问。使用此方法,您可以执行以下操作

    JLabel iconLabel = new JLabel(image);
    ...
    Timer timer = new Timer(3000, new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             iconLabel.setIcon(image1);
         }
    }); 
    timer.setRepeats(false);
    timer.start();
    
    不要尝试添加新标签,只需更改图标即可

  • 也不要设置框架的大小。打包吧。图像将为您设置
    JLabel
    的首选尺寸,并且
    pack
    固定框架将确保图标完全可见

  • 另外,在容器可见后添加组件时,需要
    revalidate()
    repaint()
    容器


    试着运行这个例子。只需输入图像图标的路径

    import java.awt.event.*;
    import javax.swing.*;
    
    public class ChangeIcon {
    
        public ChangeIcon() {
            final ImageIcon image = new ImageIcon("image.jpg");
            final ImageIcon image1 = new ImageIcon("image1.jpg");
            final JLabel iconLabel = new JLabel(image);
    
            Timer timer = new Timer(3000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    iconLabel.setIcon(image1);
                }
            });
            timer.setRepeats(false);
            timer.start();
    
            JFrame frame = new JFrame();
            frame.add(iconLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ChangeIcon();
                }
            });
        }
    }
    
  • JFrame
    具有默认的
    BorderLayout
    。添加组件而不指定
    BorderLayout
    位置将自动将其添加到
    BorderLayout.CENTER
    。每个位置只能有一个组件。您添加的最后一个组件将是唯一可见的组件。更多信息请访问

  • 避免使用
    Thread.sleep
    。如果需要动画,请使用
    javax.swing.Timer
    。更多信息请访问。使用此方法,您可以执行以下操作

    JLabel iconLabel = new JLabel(image);
    ...
    Timer timer = new Timer(3000, new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             iconLabel.setIcon(image1);
         }
    }); 
    timer.setRepeats(false);
    timer.start();
    
    不要尝试添加新标签,只需更改图标即可

  • 也不要设置框架的大小。打包吧。图像将为您设置
    JLabel
    的首选尺寸,并且
    pack
    固定框架将确保图标完全可见

  • 另外,在容器可见后添加组件时,需要
    revalidate()
    repaint()
    容器


    试着运行这个例子。只需输入图像图标的路径

    import java.awt.event.*;
    import javax.swing.*;
    
    public class ChangeIcon {
    
        public ChangeIcon() {
            final ImageIcon image = new ImageIcon("image.jpg");
            final ImageIcon image1 = new ImageIcon("image1.jpg");
            final JLabel iconLabel = new JLabel(image);
    
            Timer timer = new Timer(3000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    iconLabel.setIcon(image1);
                }
            });
            timer.setRepeats(false);
            timer.start();
    
            JFrame frame = new JFrame();
            frame.add(iconLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ChangeIcon();
                }
            });
        }
    }
    
  • JFrame
    具有默认的
    BorderLayout
    。添加组件而不指定
    BorderLayout
    位置将自动将其添加到
    BorderLayout.CENTER
    。每个位置只能有一个组件。您添加的最后一个组件将是唯一可见的组件。更多信息请访问

  • 避免使用
    Thread.sleep
    。如果需要动画,请使用
    javax.swing.Timer
    。更多信息请访问。使用此方法,您可以执行以下操作

    JLabel iconLabel = new JLabel(image);
    ...
    Timer timer = new Timer(3000, new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             iconLabel.setIcon(image1);
         }
    }); 
    timer.setRepeats(false);
    timer.start();
    
    不要尝试添加新标签,只需更改图标即可

  • 也不要设置框架的大小。打包吧。图像将为您设置
    JLabel
    的首选尺寸,并且
    pack
    固定框架将确保图标完全可见

  • 另外,在容器可见后添加组件时,需要
    revalidate()
    repaint()
    容器


    试着运行这个例子。只需输入图像图标的路径

    import java.awt.event.*;
    import javax.swing.*;
    
    public class ChangeIcon {
    
        public ChangeIcon() {
            final ImageIcon image = new ImageIcon("image.jpg");
            final ImageIcon image1 = new ImageIcon("image1.jpg");
            final JLabel iconLabel = new JLabel(image);
    
            Timer timer = new Timer(3000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    iconLabel.setIcon(image1);
                }
            });
            timer.setRepeats(false);
            timer.start();
    
            JFrame frame = new JFrame();
            frame.add(iconLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ChangeIcon();
                }
            });
        }
    }
    
  • JFrame
    具有默认的
    BorderLayout
    。添加组件而不指定
    BorderLayout
    位置将自动将其添加到
    BorderLayout.CENTER
    。每个位置只能有一个组件。您添加的最后一个组件将是唯一可见的组件。更多信息请访问

  • 避免使用
    Thread.sleep
    。如果需要动画,请使用
    javax.swing.Timer
    。更多信息请访问。使用此方法,您可以执行以下操作

    JLabel iconLabel = new JLabel(image);
    ...
    Timer timer = new Timer(3000, new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             iconLabel.setIcon(image1);
         }
    }); 
    timer.setRepeats(false);
    timer.start();
    
    不要尝试添加新标签,只需更改图标即可

  • 也不要设置框架的大小。打包吧。图像将为您设置
    JLabel
    的首选尺寸,并且
    pack
    固定框架将确保图标完全可见

  • 另外,在容器可见后添加组件时,需要
    revalidate()
    repaint()
    容器


    试着运行这个例子。只需输入图像图标的路径

    import java.awt.event.*;
    import javax.swing.*;
    
    public class ChangeIcon {
    
        public ChangeIcon() {
            final ImageIcon image = new ImageIcon("image.jpg");
            final ImageIcon image1 = new ImageIcon("image1.jpg");
            final JLabel iconLabel = new JLabel(image);
    
            Timer timer = new Timer(3000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    iconLabel.setIcon(image1);
                }
            });
            timer.setRepeats(false);
            timer.start();
    
            JFrame frame = new JFrame();
            frame.add(iconLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ChangeIcon();
                }
            });
        }
    }
    


    但这并没有发生,对于第二个代码,第一个图像是正确置换的图像,最后添加的图像只是略微向右显示,不确定是否没有可运行的示例,但尝试使用计时器而不是Thread.sleep。你最好不,先生,我确实试过了,就是这样,第一张图片显示得很好,但是第二张图片没有被很好地替换,只是在右边。先生,您可以在我们的计算机上试用。运行上面的示例,看看会发生什么。输入图像路径。使用第一个代码,您只能看到第二个图像,因为在显示第二个图像之前,帧不可见。在第二个代码中,在添加第二个标签之前,框架是可见的。在这种情况下,您需要重新验证并重新绘制框架。在容器已经可见后,每当向容器添加组件时,都必须执行此操作。但这并没有发生,对于第二个代码,第一个图像是正确置换的图像,最后添加的图像仅略微向右显示否