缩短变量以使用路径

时间:2015-12-15 20:31:56

标签: java swing netbeans imageicon

这是我的完整代码,并附有一些解释。

public class SlideShow extends javax.swing.JFrame {

JPanel      slides;
CardLayout  layoutManager;
    private JButton btnPrev;
    private JButton btnNext;
    private JButton btnHome;
    private JButton btnSound;

public SlideShow() {
super();

setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

    btnPrev = new javax.swing.JButton();
    btnNext = new javax.swing.JButton();
    btnHome = new javax.swing.JButton();
    btnSound = new javax.swing.JButton();
    btnPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/back+button.png")));
    btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/next2.png")));
    btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/home_icons.png")));
    btnSound.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Media-Controls-Volume-Down-icon.png")));

slides = new JPanel();
slides.setBackground(Color.WHITE);
slides.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    slides.setLayout(layoutManager = new CardLayout(0,0));

    for(int i=2; i<=24; i++){
    slides.add(i+".png", new JLabel(new ImageIcon(getClass().getResource("/resources/"+i+".png"))));
    }
    add(slides);

    add(btnHome);
    add(btnPrev);
    add(btnNext);
    add(btnSound);

    btnPrev.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            layoutManager.previous(slides);
        }
    });

    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            layoutManager.next(slides);
        }
    });

    btnHome.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            close();
            Frame fr = new Frame();
            fr.setVisible(true);
        }
    });

    btnSound.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            music("././build/classes/resources/test.wav");
        }
    });

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(700,530);
}

    public void close(){
    super.dispose();
}

    public static void music(String path) 
{
    AudioStream BGM;
    AudioData MD;
    AudioDataStream audiostream;

    try{
    BGM = new AudioStream (new FileInputStream(path));
    MD = BGM.getData();

    audiostream = new AudioDataStream(MD);
    AudioPlayer.player.start(audiostream);
    }catch(IOException error){}

}

public static void main(String args[]) {
    SlideShow frame =  new SlideShow();
    frame.setVisible(true);
}

}

我的JFrame内有多张图片的幻灯片。每张幻灯片都有一个按钮,点击它时会输出一些声音。这些幻灯片在同一个JFrame中调用。所以,我没有为每张幻灯片制作许多JFrame。我想为每张幻灯片制作不同的声音。我所要做的只是称为幻灯片图像的路径以匹配声音。

我的情况基本上是,我想缩短ImageIcon的变量,以便我可以返回一个特定的路径,如 5.png 来插入声音。但是,如果不调用ImageIcon中的完整路径,我就无法做到这一点,不管怎样,即使我调用了完整路径,它根本不起作用。

所以,如果我可以在slides获得一个特定路径作为变量或类似的东西,我可以使用它来使用相同的按钮调用不同的声音。如何缩短它? 或者,有没有办法从slides获取特定变量?如何调用变量?此应用程序中有24张幻灯片图像以及如何区分它?

我在JOptionPane.showMessageDialog(null, "Test!");中测试了此代码for loop,看起来此代码在实际幻灯片出现之前输出了24次。所以,这意味着,for loop只输入图像,我不知道如何回调它,所以我可以制作类似if else语句的内容,将声音放在不同的幻灯片上。

2 个答案:

答案 0 :(得分:2)

您可以在列表中存储声音文件的路径,并使用当前幻灯片的索引来选择正确的声音路径。我没有找到使用CardLayout类索引的方法;它有currentCard字段,但无法访问。

以下代码中显示了对原始版本的这些更改:

以下是代码:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import javax.sound.sampled.*;
import javax.swing.*;

public class SlideShow extends JFrame {

    private JPanel slides;
    private int slideIndex;
    private java.util.List<String> soundPaths;
    private CardLayout layoutManager;
    private JButton btnPrev;
    private JButton btnNext;
    private JButton btnHome;
    private JButton btnSound;

    public SlideShow() {
        super();

        setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

        btnPrev = new JButton("Previous");
        btnNext = new JButton("Next");
        btnHome = new JButton("Home");
        btnSound = new JButton("Sound");
        btnPrev.setIcon(createIcon("/resources/back+button.png"));
        btnNext.setIcon(createIcon("/resources/next2.png"));
        btnHome.setIcon(createIcon("/resources/home_icons.png"));
        btnSound.setIcon(createIcon("/resources/Media-Controls-Volume-Down-icon.png"));

        slides = new JPanel();
        slides.setBackground(Color.WHITE);
        slides.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        slides.setLayout(layoutManager = new CardLayout(0,0));

        soundPaths = new ArrayList<>();
        String directory = "resources/images-and-sounds/";
        for(int i=2; i<=24; i++){
            final String name = "/resources/" + i + ".png";
            slides.add(i + ".png", new JLabel(createIcon(name)));
            //slides.add(i+".png", new JLabel(new ImageIcon(directory + i + ".png")));
            soundPaths.add(directory + i + ".wav");
        }
        add(slides);

        add(btnHome);
        add(btnPrev);
        add(btnNext);
        add(btnSound);

        btnPrev.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                layoutManager.previous(slides);
                slideIndex = (slideIndex > 0)
                        ? slideIndex - 1
                        : slides.getComponentCount() - 1;
            }
        });

        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                layoutManager.next(slides);
                slideIndex = (slideIndex + 1) % slides.getComponentCount();
            }
        });

        btnHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                close();
                Frame fr = new Frame();
                fr.setVisible(true);
                slideIndex = 0;
            }
        });

        btnSound.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //music("././build/classes/resources/test.wav");

                if (Files.exists(Paths.get(soundPaths.get(slideIndex)))) {
                    music(soundPaths.get(slideIndex));
                }
            }
        });

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(700,530);
    }

    private ImageIcon createIcon(String name) {
        return new ImageIcon(getClass().getResource(name));
    }

    public void close(){
        super.dispose();
    }

    public static void music(String path)
    {
        // https://stackoverflow.com/a/30587573/1694043
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(new File(path)));
            clip.start();
            //clip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (LineUnavailableException | IOException
                | UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        SlideShow frame = new SlideShow();
        frame.setVisible(true);
    }
}

答案 1 :(得分:0)

这是我自己的版本,有点不实用,但至少我理解它。基本上,我添加了另一个变量j来替换页面幻灯片,因为我似乎无法返回其路径。由于我的幻灯片从第2页开始,因此我将其声明为j=2。之后,每次单击Next / Previous按钮,我都会递增/递减变量j。因此,我可以用j的值替换幻灯片的编号。

为了防止点击超过我的幻灯片总数,我在每个按钮上添加.setVisible(false)并再次致电.setVisible(true)以返回按钮。

通过声明j,我可以使用if else声明定义任何页面,并通过相同按钮根据页面添加任何声音。

public class SlideShow extends javax.swing.JFrame {

JPanel      slides;
CardLayout  layoutManager;
    private JButton btnPrev;
    private JButton btnNext;
    private JButton btnHome;
    private JButton btnSound;
    private int j=2;

public SlideShow() {
super();

setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

    btnPrev = new javax.swing.JButton();
    btnNext = new javax.swing.JButton();
    btnHome = new javax.swing.JButton();
    btnSound = new javax.swing.JButton();
    btnPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/back+button.png")));
    btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/next2.png")));
    btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/home_icons.png")));
    btnSound.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Media-Controls-Volume-Down-icon.png")));

slides = new JPanel();
slides.setBackground(Color.WHITE);
slides.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    slides.setLayout(layoutManager = new CardLayout(0,0));

            if(j==2)
                btnPrev.setVisible(false);

    for(int i=2; i<=24; i++){
    slides.add(i+".png", new JLabel(new ImageIcon(getClass().getResource("/resources/"+i+".png"))));
    }
    add(slides);

    add(btnHome);
    add(btnPrev);
    add(btnNext);
    add(btnSound);

    btnPrev.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            layoutManager.previous(slides);
            j--;
            if(j!=24)
                btnNext.setVisible(true);
            if(j==2)
                btnPrev.setVisible(false);
            //JOptionPane.showMessageDialog(null, "Slide "+j);
        }
    });

    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            layoutManager.next(slides);
            j++;
            if(j==24)
                btnNext.setVisible(false);
            if(j!=2)
                btnPrev.setVisible(true);
            //JOptionPane.showMessageDialog(null, "Slide "+j);
        }
    });

    btnHome.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            close();
            Frame fr = new Frame();
            fr.setVisible(true);
        }
    });

    btnSound.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                if(j==2)
                    music("././build/classes/resources/test1.wav");
                else if(j==3)
                    music("././build/classes/resources/test2.wav");
        }
    });

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(700,530);
}

    public void close(){
    super.dispose();
}

    public static void music(String path) 
{
    AudioStream BGM;
    AudioData MD;
    AudioDataStream audiostream;

    try{
    BGM = new AudioStream (new FileInputStream(path));
    MD = BGM.getData();

    audiostream = new AudioDataStream(MD);
    AudioPlayer.player.start(audiostream);
    }catch(IOException error){}

}

public static void main(String args[]) {
    SlideShow frame =  new SlideShow();
    frame.setVisible(true);
}

}