JAVA设计GUI界面如何实现播放歌曲详细代码
时间: 2023-09-22 22:07:53 浏览: 66
首先,你需要使用一个合适的音频播放库,如JavaFX或Java音频系统(Java Sound API),来播放音乐文件。
然后,你需要创建一个GUI界面,界面上应该包含一个播放按钮、一个停止按钮、一个暂停/继续按钮和一个音乐播放进度条。
接下来,你需要编写代码来实现以下功能:
1. 加载音乐文件
```java
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(stage);
if (selectedFile != null) {
Media media = new Media(selectedFile.toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(false);
}
```
2. 播放音乐
```java
mediaPlayer.play();
```
3. 暂停/继续音乐
```java
if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
mediaPlayer.pause();
} else {
mediaPlayer.play();
}
```
4. 停止音乐
```java
mediaPlayer.stop();
```
5. 更新音乐播放进度条
```java
mediaPlayer.currentTimeProperty().addListener((ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) -> {
progressBar.setValue(newValue.toSeconds());
});
```
完整代码如下:
```java
import java.io.File;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
public class MusicPlayer extends Application {
private MediaPlayer mediaPlayer;
private ProgressBar progressBar;
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
root.setPadding(new Insets(10));
root.setVgap(10);
root.setHgap(10);
Button playButton = new Button("播放");
playButton.setOnAction(e -> mediaPlayer.play());
Button pauseButton = new Button("暂停/继续");
pauseButton.setOnAction(e -> {
if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
mediaPlayer.pause();
} else {
mediaPlayer.play();
}
});
Button stopButton = new Button("停止");
stopButton.setOnAction(e -> mediaPlayer.stop());
Button fileButton = new Button("选择文件");
fileButton.setOnAction(e -> {
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) {
Media media = new Media(selectedFile.toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(false);
mediaPlayer.currentTimeProperty().addListener((ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) -> {
progressBar.setValue(newValue.toSeconds());
});
}
});
progressBar = new ProgressBar(0);
progressBar.setMaxWidth(Double.MAX_VALUE);
root.addRow(0, playButton, pauseButton, stopButton);
root.addRow(1, fileButton, progressBar);
Scene scene = new Scene(root, 400, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)