我正在构建一个程序来完成一个想法,那就是视频播放器,这是用javafx制作的,但是该程序是在普通的java中,我找到了一种播放视频的方法,但是真正的问题是,当关闭时右上角的X,视频的音乐不会停止,我尝试代替frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);把frame.setDefaultCloseOperation(exit());仍然在代码中。
编辑:我添加了实现WindowListener和所有抽象方法的方法,但是在关闭或打开窗口时不会调用它们(我使用System.out.println()来测试是否被调用)。
import br.com.cabine.GUI.GUIMessage;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
*
* @author Bruno
*/
public class FXPlayer extends JApplet implements WindowListener {
private static final int JFXPANEL_WIDTH_INT = 1240;
private static final int JFXPANEL_HEIGHT_INT = 720;
private static JFXPanel fxContainer;
private static MediaPlayer mp;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}
JFrame frame = new JFrame("Media Player");
//frame.setDefaultCloseOperation(2);
JApplet applet = new FXPlayer();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
@Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene();
}
});
}
private void createScene() {
File file = new File("filePath.txt");
File f = null;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
f = new File(br.readLine());
br.close();
} catch(IOException ex) {
GUIMessage.info("Não Foi possivel encontrar o arquivo", "Arquivo não encontrado!");
}
Media m=new Media(f.toURI().toString());
mp=new MediaPlayer(m);
MediaView mv=new MediaView(mp);
/*Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});*/
StackPane root = new StackPane();
root.getChildren().add(mv);
fxContainer.setScene(new Scene(root));
if (file.getPath() != null) {
mp.play();
}
}
public void stop(){
System.out.println("chama");
Platform.exit();
mp.stop();
}
@Override
public void windowOpened(WindowEvent e) {
System.out.println("chama0");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("chama1");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("chama2");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("chama3");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("chama4");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("chama5");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("chama6");
}
}