Java 缩短变量以使用路径

Java 缩短变量以使用路径,java,swing,netbeans,imageicon,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 SlideSho

这是我的完整代码,这里有一些解释

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);
}
公共类幻灯片放映扩展了javax.swing.JFrame{
JPanel幻灯片;
卡片布局经理;
私人按钮btnPrev;
私人JButton btnNext;
私人吉布顿酒店;
私人JButton btnSound;
公共幻灯片(){
超级();
setLayout(新的FlowLayout(FlowLayout.CENTER,10,10));
btnPrev=newjavax.swing.JButton();
btnNext=newjavax.swing.JButton();
btnHome=newjavax.swing.JButton();
btnSound=newjavax.swing.JButton();
setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/resources/back+button.png”));
setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/resources/next2.png”));
setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/resources/home_icons.png”));
setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/resources/Media Controls Volume Down icon.png”));
幻灯片=新的JPanel();
幻灯片.背景(颜色.白色);
slides.setboorder(BorderFactory.createLineBorder(Color.BLACK));
slides.setLayout(layoutManager=newCardLayout(0,0));

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

对原始版本的这些更改在下面的代码中可见:

  • 添加了一个
    slideIndex
    字段以跟踪当前幻灯片索引。该字段在启动时和按下主页按钮时初始化为零。按下下一个或上一个按钮时也会对其进行修改
  • 添加了带有声音文件路径的
    声音路径
    列表
  • 按钮有一些文本,并且有一个
    createIcon
    方法
  • 幻灯片
    面板被填充时,
    声音路径
    列表也被填充。(我使用了一个包含图像和声音的目录。)
  • 原始声音代码在我的机器上不起作用;因此我使用了在堆栈溢出答案中找到的代码:。循环应该很容易:如果调用
    clip.loop
    方法,它应该能够循环
  • 根据文档,Java Sound支持各种音频格式(如.au、.aif和.wav),但其可用性取决于操作系统。有关更多信息,请参见示例:
    • stackoverflow.com/a/29713583/1694043(链接不工作?!)
  • 您还可以将原始声音文件转换为支持的格式
代码如下:

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);
    }
}
import java.awt.*;
导入java.awt.event.*;
导入java.io.*;
导入java.nio.file.*;
导入java.util.*;
导入javax.sound.sampled.*;
导入javax.swing.*;
公共类幻灯片放映扩展了JFrame{
私人JPanel幻灯片;
私有int slideIndex;
私有java.util.List声音路径;
私人卡布局经理;
私人按钮btnPrev;
私人JButton btnNext;
私人吉布顿酒店;
私人JButton btnSound;
公共幻灯片(){
超级();
setLayout(新的FlowLayout(FlowLayout.CENTER,10,10));
btnPrev=新按钮(“先前”);
btnNext=新的JButton(“下一步”);
btnHome=新的JButton(“家”);
BTN声音=新的按钮(“声音”);
setIcon(createIcon(“/resources/back+button.png”);
setIcon(createIcon(“/resources/next2.png”);
setIcon(createIcon(“/resources/home_icons.png”);
setIcon(createIcon(“/resources/Media Controls Volume Down icon.png”);
幻灯片=新的JPanel();
幻灯片.背景(颜色.白色);
slides.setboorder(BorderFactory.createLineBorder(Color.BLACK));
slides.setLayout(layoutManager=newCardLayout(0,0));
声音路径=新的ArrayList();
String directory=“资源/图像和声音/”;
对于(int i=2;i 0)
?滑动索引-1
:slides.getComponentCount()-1;
}
});
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
layoutManager.next(幻灯片);
slideIndex=(slideIndex+1)%slides.getComponentCount();
}
});
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
close();
帧fr=新帧();
fr.setVisible(真);
slideIndex=0;
}
});
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
//音乐(“.//构建/类/资源/测试.wav”);
if(Files.exists(path.get(soundpath.get(slideIndex))){
音乐(soundpath.get(slideIndex));
}
}
});
setDefaultCloseOperation(关闭时退出);
设置大小(700530);
}
private ImageIcon createIcon(字符串名称){
返回新的ImageIcon(getClass().getResource(name));
}
公众假期结束(){
super.dispose();
}
公共静态无效音乐(字符串路径)
{
// https://stackoverflow.com/a/30587573/1694043
试一试{
Clip Clip=AudioSystem.getClip();
open(AudioSystem.getAudioInputStream(新文件(路径));
clip.start();
//卡环(连续卡环);
}catch(LineUnavailableException | IOException
|不支持数据文件(e){
e、 printStackTrace();
}
}
公共静态void main(字符串参数[]){
幻灯片放映框架=新幻灯片放映();
frame.setVisible(true);
}
}

这是我自己的版本,有点不切实际,
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);
}