在Java swing中创建幻灯片

问题描述 投票:1回答:2

我正在尝试使用Java Swing创建幻灯片。我从实现类PicturePanel

开始
public class PicturePanel extends JPanel {

private int counter = 0;
private ImageIcon[] images = new ImageIcon[10];
private JLabel label;


public PicturePanel()
{

for(int i = 0 ; i <images.length;i++)
{
images[counter] = new ImageIcon("check.png");
label = new JLabel();
add(label);
Timer timer = new Timer(100, new TimerListener());
}

}

private class TimerListener implements ActionListener {

    public TimerListener() {
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        counter++;
        //counter% =images.length;
        label.setIcon(images[counter]);

    }
}
}

然后我通过此代码在Jframe中调用此类:

panProfil= new PicturePanel();

[panProfil是我的表单中的Jpanel

当我运行项目时,我没有收到任何错误,但是我的表单中没有任何内容。有人可以指出我正确的方向吗?

java swing jpanel slideshow
2个回答
1
投票

所以您还没有开始Timer就是问题所在(如@ItachiUchiha所指出的)。但是您需要做的另一件事是知道何时stop() Timer,否则它将继续运行

您想在创建start()后在构造函数中将其Timer。在您的ActionListener中,要停止它,您将需要执行以下操作。

private class TimerListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (counter == images.length) {
            ((Timer)e.getSource()).stop();
        } else {
            label.setIcon(images[counter]);
            counter++;
        }
    }
}

如果要从主GUI类访问Timer,以便可以对其进行控制,则需要一个getter,并在全局范围内进行声明

public class PicturePanel extends JPanel {
    private Timer timer = null;

    public PicturePanel() {
        timer = new Timer(1000, new TimerListener());
    }

    public Timer getTimer() {
        return timer;
    }
}

然后您可以从主GUI类启动和停止它

DrawPanel panel = new DrawPanel();
Timer timer = panel.getTimer();

而且,我看不到每次迭代都创建JLabel并将其添加到JPanel的意义。您只需要一个。


0
投票
public class Project2 {
    int c=0;
    public static void main(String arg[]) throws InterruptedException {
        JFrame login = new JFrame("Login"); 
        // creating a new frame
        login.setSize(700, 500);
        JPanel addPanel = new JPanel();
        JLabel pic = new JLabel();

        Project2 p2= new Project2();

        String[] list = {"C:\\Users\\divyatapadia\\Desktop\\pic1.jpg", "C:\\Users\\divyatapadia\\Desktop\\benefit.PNG" , "C:\\Users\\divyatapadia\\Desktop\\pic2.jpg"};
        pic.setBounds(40, 30, 500, 300);
        JButton log = new JButton("SHOW");ImageIcon[] img  = new ImageIcon[3];
        for(int time = 0;time<3;time++) {
            img[time]= new ImageIcon(list[time]);
            }
        addPanel.add(pic);
        addPanel.add(log);
        login.add(addPanel);
        login.setVisible(true);
        try {
            log.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                        Timer t  ;
                     t= new Timer(1000,new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent e) {
                                if(p2.c<3) {


                                    pic.setIcon(img[p2.c]);
                                    p2.c++;
                                    }}


                            });
                     t.start();
                 }
}
);

}catch(Exception ex)
 {
            System.out.println(ex.toString());

}

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.