制作SlideShow JApplet

时间:2016-03-04 23:28:36

标签: java swing japplet

对于我的班级,我必须制作一个SlideShow JApplet。用户将能够打开他们选择的十张图片,并将在幻灯片放映中通过它们,每一张都有延迟。我把所有东西都放在我的代码中,他们能够调出打开的对话框来选择图像文件。但是一旦我开始搞乱我的buildPicturePanel(),当我运行文件时没有出现任何东西。有人可以给我一些帮助,我应该用这个改变或添加。请描述一下,这本书没有给出适用于此的图像的好例子。

到目前为止,这是我的代码。

public class Slideshow extends JApplet  {
   //setting all JItmes along with items for the image and timer
    private JLabel display;
    private JButton open ;
   private  JButton start ;
    private JButton stop ;
    private JPanel picturePanel;
    private JPanel navigationPanel;
    private JPanel mainPanel;
    private BufferedImage pics;
    private int counter = 0;
    private ImageIcon[] pictures ;
    private JLabel label;

    int currentImage = -1;
    String images[];
    private Object image;
    File selectedFile;
    String fileName;


  //basically the main method for a JApplet
    public void init() {
        this.setSize(400,500);
        buildNavPanel();
       buildPicturePanel();
        mainPanel();


    }
    //creates the panel that will hold the slide show
    private void buildPicturePanel(){
      try{
          pics = ImageIO.read(new File(fileName));
      }catch(IOException ex){
          System.out.println("Please open another selection of pictures.");
      }

    }

    //navigation panel where user can hit add, start, stop
    private void buildNavPanel(){
        navigationPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        navigationPanel.setLayout(new GridLayout(0,3));
        open = new JButton("Open Images (Up to 10)");
        start = new JButton ("Start Slide Show");
        stop = new JButton ("Stop Slide Show");
        open.addActionListener(new OpenListener());
        navigationPanel.add(open);
        navigationPanel.add(start);
        navigationPanel.add(stop);



    }

    //main panel holds all panels with location
    private void mainPanel(){
        buildNavPanel();
       buildPicturePanel();
        add(navigationPanel,BorderLayout.SOUTH);
        add(picturePanel, BorderLayout.CENTER);
    }

    //shows current picture
    public void showCurrent(){
        display.setIcon(new ImageIcon(images[currentImage]));

    }

    //shows next picture
    public void showNext(){
        currentImage = currentImage +1;
        if (currentImage >= images.length){
            currentImage = 0;
        }
        showCurrent();



    }




//this will make the openlistener work
   private class OpenListener implements ActionListener{

   //actionevent for openlistener
    public void actionPerformed(ActionEvent e) {
        JFileChooser fc= new JFileChooser();
     FileFilter filter = new FileNameExtensionFilter ("JPEG file", "jpg", "jpeg");
    fc.setFileFilter(filter);
    fc.setMultiSelectionEnabled(true);
     int response = fc.showOpenDialog(null);
     if (response == JFileChooser.APPROVE_OPTION){
         selectedFile = fc.getSelectedFile();
         fileName = selectedFile.getPath();

    }

    }

}
}

非常感谢任何帮助。

0 个答案:

没有答案