我无法找到某人从文本文件中获取特定行并使用它来触发JFrame按钮事件的示例。我想使用这个,所以当这个音板打开时,它将播放与关闭程序时相同的歌曲,但仅限于你没有停止播放歌曲。
要明确我不希望这些歌曲在它停止播放的地方启动我只想在开头就开始播放相同的歌曲。
txt文档从四行开始为0,但我希望他们检查一下是否需要让它自动运行。
此外,我对阅读和编写文件的概念不熟悉,所以如果我以正确的方式这样做,我会非常感谢任何提示。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Scanner;
public class beepBoop extends JFrame {
JButton Loop1 = new JButton("Play");
JButton Stop1 = new JButton("Stop");
URL song1 = beepBoop.class.getResource("song1.wav");
AudioClip clip = Applet.newAudioClip(song1);
public beepBoop() {
super("AddingSound");
setLayout(new FlowLayout());
Loop1.addActionListener(new ActionListener() {
/* THIS IS WHERE I WOULD LIKE TO CHANGE AND SAVE A SPECIFIC LINE OF A TEXT FILE
* AS A 1 AND WHEN THIS SPECIFIC LINE OF TEXT IS A 1 WHEN THE PROGRAM BEGINS IT
* WILL ATTOMATICALLY PLAY THE SONG
* TO BE CLEAR I DO NOT!!! WANT THE SONG TO PICK UP WHERE IT LEFT OFF I JUST WANT
* THE SAME SONG TO PLAY FROM THE BEGINNING
* I WOULD LIKE TO REPEAT THIS PROCESS AT LEAST FOUR TIMES
*/
public void actionPerformed(ActionEvent e) {
clip.loop();
}
});
Stop1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clip.stop();
}
});
add(Loop1);
add(Stop1);
}
public static void main(String args[]) {
JFrame frame = new beepBoop();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
File soundstatus = new File("soundstatus.txt");
try {
PrintWriter output = new PrintWriter(soundstatus);
output.println("0");
output.println("0");
output.println("0");
output.println("0");
output.close();
} catch (IOException ex) {
System.out.printf("ERROR: %s\n", ex);
}
try {
Scanner input = new Scanner(soundstatus);
int sound1 = input.nextInt();
int sound2 = input.nextInt();
int sound3 = input.nextInt();
int sound4 = input.nextInt();
System.out.printf("sound1: %s sound2: %d\n", sound1, sound2,
sound3, sound4);
} catch (FileNotFoundException ex) {
System.out.printf("Please choose a different file: %s\n", ex);
}
}
}
到目前为止,该项目的一个小图片。我知道我是一个了不起的设计师不是吗? XD