有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在JavaSwing中创建幻灯片

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

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


共 (2) 个答案

  1. # 1 楼答案

    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());
    
    }
    
        }
    }
    
  2. # 2 楼答案

    所以你还没有开始你的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中有什么意义。你只需要一个