Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像不会在Jframe中刷新_Java_Jframe - Fatal编程技术网

Java 图像不会在Jframe中刷新

Java 图像不会在Jframe中刷新,java,jframe,Java,Jframe,我正在编写在特定目录下Jframe中显示图像的代码。Jframe将一次显示一个图像 我的代码显示jframe的大小正在更改,但图像没有更改。 我把重新验证和油漆,但图像没有刷新 下面是UpdateName函数,它具有更新逻辑 private void updateFrame(JFrame f, String billBoardImageFileLocation, int imageNumber) throws ClassNotFoundException, IOException { D

我正在编写在特定目录下Jframe中显示图像的代码。Jframe将一次显示一个图像

我的代码显示jframe的大小正在更改,但图像没有更改。 我把重新验证和油漆,但图像没有刷新

下面是UpdateName函数,它具有更新逻辑

private void updateFrame(JFrame f, String billBoardImageFileLocation, int imageNumber) throws ClassNotFoundException, IOException {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //this is your screen size
    File folder = new File(billBoardImageFileLocation);
    String[] extensions = new String[]{"jpeg", "jpg"};
    List<File> imageFiles = (List<File>) FileUtils.listFiles(folder, extensions, false);
    ImageIcon image = new ImageIcon(ImageIO.read(imageFiles.get(imageNumber % imageFiles.size()))); //imports the image

    if (isDebug.equals("Y")) {
        System.out.println("Files:" + imageFiles.get(imageNumber % imageFiles.size()).getAbsolutePath());
    }

    JLabel lbl = new JLabel(image); //puts the image into a jlabel\
            f.getContentPane().add(lbl); //puts label inside the jframe
    f.setSize(image.getIconWidth(), image.getIconHeight()); //gets h and w of image and sets jframe to the size
    f.getContentPane().revalidate();
    int x = (screenSize.width - f.getSize().width) / 2; //These two lines are the dimensions
    int y = (screenSize.height - f.getSize().height) / 2; //of the center of the screen
    f.setLocation(x, y); //sets the location of the jframe
    f.setVisible(true); //makes the jframe visible
    f.revalidate(); // **** added ****
    f.repaint();
    f.getContentPane().revalidate();
    f.getContentPane().repaint();
}
不要创建新标签,只需更新现有标签的图标:

label.setIcon( image );
因此,更改方法参数以传入标签和帧


这就是你需要做的

您正在Swing事件线程上执行长时间运行的代码,并将其冻结。解决方法:不要。使用Swing计时器交换图像,因为这将允许不在Swing事件线程上执行的重复操作。也不要在此线程上调用
Thread.sleep
。有关EDT、Swing事件调度线程的更多信息,请阅读

JLabel lbl = new JLabel(image);
label.setIcon( image );