Java GUI:图像将被覆盖,路径相同->;在框架中显示(图像仍然相同)

Java GUI:图像将被覆盖,路径相同->;在框架中显示(图像仍然相同),java,image,swing,user-interface,filepath,Java,Image,Swing,User Interface,Filepath,我想在相框上显示一个不断变化的图像。imagepath始终相同,但另一个程序每隔10秒就会覆盖一次图像。 问题是,当我用另一个同名的图像覆盖它时,该图像没有改变。所以在我的理解中:编译器查看路径中的每一个外观,并获取图像->当图像更改时,它将在帧上更改 我希望你能理解我的问题,有人能帮助我 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.G

我想在相框上显示一个不断变化的图像。imagepath始终相同,但另一个程序每隔10秒就会覆盖一次图像。 问题是,当我用另一个同名的图像覆盖它时,该图像没有改变。所以在我的理解中:编译器查看路径中的每一个外观,并获取图像->当图像更改时,它将在帧上更改

我希望你能理解我的问题,有人能帮助我

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.io.File; 

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class GUI extends JFrame{

        public ImageIcon imageBar;

        public JLabel labelimage1;
        private JLabel labelimage2;

        private JLabel bar1 = new JLabel();
        private JLabel bar2 = new JLabel();
        private JLabel bar3 = new JLabel();
        private JLabel bar4 = new JLabel();
        private JLabel bar5 = new JLabel();

        private JButton buttonBar1 = new JButton("1");
        private JButton buttonBar2 = new JButton("2");
        private JButton buttonBar3 = new JButton("3");
        private JButton buttonBar4 = new JButton("4");
        private JButton buttonBar5 = new JButton("5");

        private JPanel panel1 = new JPanel();
        private JPanel panel2 = new JPanel();
        private JPanel panel3 = new JPanel();

        private JFrame window = new JFrame("Interface");

        public GUI(){

            //set the layouts
            panel1.setLayout(new GridLayout(1, 2)); 
            panel2.setLayout(new GridLayout(2, 1));
            panel3.setLayout(new GridLayout(2, 5));

            //place Panel2 and Panel3 in the window
            panel1.add(panel2); 
            panel1.add(panel3);

            //----Panel2
            //refreshImage();


            //----Panel3        
            panel3.add(buttonBar1); //add the bars 1-5 on panel3
            panel3.add(buttonBar2);
            panel3.add(buttonBar3);
            panel3.add(buttonBar4);
            panel3.add(buttonBar5);

            //configure the frame
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
            window.setSize(800, 400);
            window.getContentPane().add(panel1);
        }


        public void refreshImage() {

            panel2.removeAll(); //delete the old panel
            //panel2.repaint();
            //panel2.revalidate()

            DrawImage pan = new DrawImage();

            panel2.add(pan);
            panel2.add(labelimage2);
        }
}


import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class DrawImage extends JPanel implements ActionListener{

    private ImageIcon image;

    public DrawImage(){
        image = new ImageIcon("C:\\Users\\usuario\\Desktop\\image.png");
    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        image.paintIcon(this, g, 50, 50);
        repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

}

import java.io.File;

public class Main {

    public static void main(String[] args) {

        GUI Interface = new GUI(); 

        while(true)
        {
            Interface.refreshImage(); 
            try {
                        Thread.sleep(5000); //wait for 5000ms
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
        }

    }

}
多谢各位

  • 您的
    while(true)
    循环有可能键入锁定程序的Swing事件线程。如果它不能做到这一点,那么通过调用事件线程的Swing,您将面临无法预测的线程问题。这些问题可以通过使用Swing计时器(而不是while true循环)轻松解决
  • 与其移除和添加组件,为什么不在单个未交换的JLabel中简单地将图像显示为ImageIcon呢
  • 要在此处交换图像,只需在JLabel上调用
    setIcon(…)
  • 有关使用Swing计时器交换图像的示例,请查看我对类似问题的回答


    例如:

    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class TimerImageSwapper {
       public static final String[] IMAGE_URLS = {
             "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_01.png",
             "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_02.png",
             "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_04.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_08.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_05.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_01.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_06.png" };
    
    
       private ImageIcon[] icons = new ImageIcon[IMAGE_URLS.length];
       private JLabel mainLabel = new JLabel();
    
       private int iconIndex = 0;;
    
       public TimerImageSwapper(int timerDelay) throws IOException {
          for (int i = 0; i < icons.length; i++) {
             URL imgUrl = new URL(IMAGE_URLS[i]);
             BufferedImage image = ImageIO.read(imgUrl);
             icons[i] = new ImageIcon(image);
          }
    
          mainLabel.setIcon(icons[iconIndex]);
    
          new Timer(timerDelay, new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                iconIndex++;
                iconIndex %= IMAGE_URLS.length;
                mainLabel.setIcon(icons[iconIndex]);
             }
          }).start();
       }
    
       public Component getMainComponent() {
          return mainLabel;
       }
    
       private static void createAndShowGui() {
          TimerImageSwapper timerImageSwapper;
          try {
             timerImageSwapper = new TimerImageSwapper(5 * 1000);
             JFrame frame = new JFrame("Timer Image Swapper");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.getContentPane().add(timerImageSwapper.getMainComponent());
             frame.pack();
             frame.setLocationByPlatform(true);
             frame.setVisible(true);
    
          } catch (IOException e) {
             e.printStackTrace();
             System.exit(-1);
          }
    
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    导入java.awt.Component;
    导入java.awt.event.ActionEvent;
    导入java.awt.event.ActionListener;
    导入java.awt.image.buffereImage;
    导入java.io.IOException;
    导入java.net.URL;
    导入javax.imageio.imageio;
    导入javax.swing.*;
    公共类TimerImageSwapper{
    公共静态最终字符串[]图像\u URL={
    "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_01.png",
    "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_02.png",
    "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_04.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_08.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_05.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_01.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_06.png" };
    私有图像图标[]图标=新图像图标[IMAGE_url.length];
    私有JLabel mainLabel=新JLabel();
    私有整数指数=0;;
    公共TimerImageSwapper(int-timerDelay)引发IOException{
    对于(int i=0;i
  • 您的
    while(true)
    循环有可能键入锁定程序的Swing事件线程。如果它不能做到这一点,那么通过调用事件线程的Swing,您将面临无法预测的线程问题。这些问题可以通过使用Swing计时器(而不是while true循环)轻松解决
  • 与其移除和添加组件,为什么不在单个未交换的JLabel中简单地将图像显示为ImageIcon呢
  • 要在此处交换图像,只需在JLabel上调用
    setIcon(…)
  • 有关使用Swing计时器交换图像的示例,请查看我对类似问题的回答


    例如:

    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class TimerImageSwapper {
       public static final String[] IMAGE_URLS = {
             "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_01.png",
             "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_02.png",
             "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_04.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_08.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_05.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_01.png",
             "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_06.png" };
    
    
       private ImageIcon[] icons = new ImageIcon[IMAGE_URLS.length];
       private JLabel mainLabel = new JLabel();
    
       private int iconIndex = 0;;
    
       public TimerImageSwapper(int timerDelay) throws IOException {
          for (int i = 0; i < icons.length; i++) {
             URL imgUrl = new URL(IMAGE_URLS[i]);
             BufferedImage image = ImageIO.read(imgUrl);
             icons[i] = new ImageIcon(image);
          }
    
          mainLabel.setIcon(icons[iconIndex]);
    
          new Timer(timerDelay, new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                iconIndex++;
                iconIndex %= IMAGE_URLS.length;
                mainLabel.setIcon(icons[iconIndex]);
             }
          }).start();
       }
    
       public Component getMainComponent() {
          return mainLabel;
       }
    
       private static void createAndShowGui() {
          TimerImageSwapper timerImageSwapper;
          try {
             timerImageSwapper = new TimerImageSwapper(5 * 1000);
             JFrame frame = new JFrame("Timer Image Swapper");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.getContentPane().add(timerImageSwapper.getMainComponent());
             frame.pack();
             frame.setLocationByPlatform(true);
             frame.setVisible(true);
    
          } catch (IOException e) {
             e.printStackTrace();
             System.exit(-1);
          }
    
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    导入java.awt.Component;
    导入java.awt.event.ActionEvent;
    导入java.awt.event.ActionListener;
    导入java.awt.image.buffereImage;
    导入java.io.IOException;
    导入java.net.URL;
    导入javax.imageio.imageio;
    导入javax.swing.*;
    公共类TimerImageSwapper{
    公共静态最终字符串[]图像\u URL={
    "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_01.png",
    "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_02.png",
    "http://imaging.nikon.com/lineup/dslr/d7000/img/sample/img_04.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_08.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_05.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_01.png",
    "http://imaging.nikon.com/lineup/dslr/d3200/img/sample/img_06.png" };
    私有图像图标[]图标=新图像图标[IMAGE_url.length];
    私有JLabel mainLabel=新JLabel();
    私有整数指数=0;;
    公共TimerImageSwapper(int-timerDelay)引发IOException{
    对于(int i=0;i