为什么幻灯片不能在 Java 中使用 Swing 和 Awt 工作?

发布于 2024-10-24 00:28:31 字数 2468 浏览 3 评论 0原文

我正在使用计时器在java中制作图像程序的幻灯片。 在计时器事件列表中,我已将代码添加到 chnage 图像,但图像没有改变。 下面是我写的代码

class ImagePanel extends JPanel {

    private Image backgroundImage;

    public ImagePanel(Image backgroundImage) {
        super();
        this.backgroundImage = backgroundImage;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);        
        g.drawImage(this.backgroundImage, 0, 0, null);
    }
}

public class A extends JFrame{
    static int counter;
    List<String> imagePaths;
    int nimgpaths=0;
    static A frame = new A();
    public static void main(String[] args) {
        frame.setSize(1024, 768);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getPath();
        /* Getting required image */
        Image backgroundImage = null;
        String pathToTheImage = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\civ1.JPG";
        try {
            backgroundImage = ImageIO.read(new File(pathToTheImage));
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* Initializing panel with the our image */
        ImagePanel panel = new ImagePanel(backgroundImage);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
        frame.timerEvent();
        //frame.show();
    }

    public void timerEvent(){
    Timer timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Time event occured");
            if(counter > nimgpaths)
                counter=0;
            String imgPath=imagePaths.get(counter);
            Image backgroundImage = null;
            try {
            backgroundImage = ImageIO.read(new File(imgPath));
        }catch (Exception e1) {
            e1.printStackTrace();
        }

        /* Initializing panel with the our image */
            frame.removeAll();
        ImagePanel panel = new ImagePanel(backgroundImage);
        panel.repaint();
        //panel.setBackground(backgroundImage);
        frame.getContentPane().add(panel);

        }
    });
    timer.start();
    }

   // To get path of images
    public void getPath(){
        DbOps db=new DbOps();
        imagePaths=db.getPath();
        nimgpaths=imagePaths.size();
        for(Iterator i=imagePaths.iterator();i.hasNext();){
            System.out.println((String)i.next());
        }

    }

}

I am doing slideshow of images program in java using timer.
In timer event listner i have added code to chnage image but image is not changing.
Below is the code i have written

class ImagePanel extends JPanel {

    private Image backgroundImage;

    public ImagePanel(Image backgroundImage) {
        super();
        this.backgroundImage = backgroundImage;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);        
        g.drawImage(this.backgroundImage, 0, 0, null);
    }
}

public class A extends JFrame{
    static int counter;
    List<String> imagePaths;
    int nimgpaths=0;
    static A frame = new A();
    public static void main(String[] args) {
        frame.setSize(1024, 768);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getPath();
        /* Getting required image */
        Image backgroundImage = null;
        String pathToTheImage = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\civ1.JPG";
        try {
            backgroundImage = ImageIO.read(new File(pathToTheImage));
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* Initializing panel with the our image */
        ImagePanel panel = new ImagePanel(backgroundImage);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
        frame.timerEvent();
        //frame.show();
    }

    public void timerEvent(){
    Timer timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Time event occured");
            if(counter > nimgpaths)
                counter=0;
            String imgPath=imagePaths.get(counter);
            Image backgroundImage = null;
            try {
            backgroundImage = ImageIO.read(new File(imgPath));
        }catch (Exception e1) {
            e1.printStackTrace();
        }

        /* Initializing panel with the our image */
            frame.removeAll();
        ImagePanel panel = new ImagePanel(backgroundImage);
        panel.repaint();
        //panel.setBackground(backgroundImage);
        frame.getContentPane().add(panel);

        }
    });
    timer.start();
    }

   // To get path of images
    public void getPath(){
        DbOps db=new DbOps();
        imagePaths=db.getPath();
        nimgpaths=imagePaths.size();
        for(Iterator i=imagePaths.iterator();i.hasNext();){
            System.out.println((String)i.next());
        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千と千尋 2024-10-31 00:28:31

为什么要使用定制面板和绘画?

您的代码只是按照首选尺寸绘制图像。当您使用 JLabel 时,此功能可用。然后,当您使用标签时,您需要做的就是使用:

label.setIcon(....);

当您想要更改图像时。阅读 Swing 教程中关于如何使用图标的部分更多信息。

创建自定义组件的唯一原因是您计划缩放图像或执行类似的操作。如果是这种情况,那么您可以使用后台面板之类的东西它支持缩放图像以及 setImage() 方法,因此您可以动态更改图像。

Why are you using a custom panel and painting?

Your code is simply painting the image at its preferred size. This functionality is available when you use a JLabel. Then when you use the label all you need to do is use:

label.setIcon(....);

when you want to change the image. Read the section from the Swing tutorial on How to Use Icons for more information.

The only reason to create a custom component is if you plan to scale the image or do something fancy like that. If this is the case then you can use something like the Background Panel which supports scaled images as well as a setImage() method so you can change the image dynamically.

零崎曲识 2024-10-31 00:28:31

ImagePanel 的更好设计可以让您只替换图像,而不是删除组件。但是,如果您确实必须替换可见组件,则必须在其容器上调用 validate() ,否则新组件不会显示(无论如何,大多数时候)。我认为这就是您的问题。

A much better design for ImagePanel would let you just replace the image, rather than removing the component. If you do have to replace a visible component, though, you have to call validate() on its container, or the new one isn't going to show up (most of the time, anyway.) I think that's your problem here.

洒一地阳光 2024-10-31 00:28:31

frame.removeAll() 没有执行您期望的操作 - 它是从框架本身中删除组件,而不是从框架的内容窗格中删除组件。将计时器操作侦听器末尾的代码更改为类似以下内容来修复它:

    ImagePanel panel = new ImagePanel(backgroundImage);
    frame.getContentPane().removeAll();
    frame.getContentPane().add(panel);
    frame.getContentPane().invalidate();
    frame.getContentPane().validate();

frame.removeAll() is not doing what you would expect - it is removing the components from the frame itself rather than removing the components from the content pane of the frame. Change the code at the end of the timer's action listener to something like this to fix it:

    ImagePanel panel = new ImagePanel(backgroundImage);
    frame.getContentPane().removeAll();
    frame.getContentPane().add(panel);
    frame.getContentPane().invalidate();
    frame.getContentPane().validate();
誰ツ都不明白 2024-10-31 00:28:31

你的观念本身就是错误的。

您可以像这样刷新面板:

public void refreshPanel(JPanel panel){
   panel.removeAll();
   panel.invalidate();
   panel.validate();
}

问题:

我在您的代码中看到您正在尝试创建同一面板的多个对象,您需要刷新这些对象。

最好创建一个面板对象并刷新该对象。

ImagePanel panel = new ImagePanel(backgroundImage);

希望您能理解我想向您解释的内容。

如果您仍然感到困惑,请告诉我。

Your concept itself is wrong.

You can refresh the panel like so:

public void refreshPanel(JPanel panel){
   panel.removeAll();
   panel.invalidate();
   panel.validate();
}

Problem:

I see in your code that you are trying to create more than one object of the same panel, which you need to refresh.

It would be better to create one panel object and refresh that object.

ImagePanel panel = new ImagePanel(backgroundImage);

Hope you can understand what I wanted to explain to you.

If you are still confused then let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文