【问题标题】:creating slideshow in java swing在 java swing 中创建幻灯片
【发布时间】:2014-04-07 13:12:40
【问题描述】:

我正在尝试使用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

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

【问题讨论】:

  • 你开始你的Timer了吗?
  • 不,我没有,我应该在哪里做?

标签: java swing jpanel slideshow


【解决方案1】:

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

您想在创建Timer 后在构造函数中将其start()。在你 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 的意义。你只需要一个。

【讨论】:

    【解决方案2】:
    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());
    
    }
    
        }
    }
    

    【讨论】:

    • 请解释
    • 如果我们想根据时间显示图像的幻灯片放映,那么可以使用此代码。它使用java语言并入eclipse ide。
    • edit请发帖
    • 图像将显示在 JLabel 中。在按钮上单击幻灯片将​​开始
    • 为什么以及在什么地方?
    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多