I have two java class. what code should i write, so that when i click on the button the video class will be called out?
The video is playing correctly.
the button code is as follows:
the video class
The video is playing correctly.
the button code is as follows:
import javax.swing.*;
import java.awt.*;//Container, Layout Manager, etc not starting with J belong to it
import java.awt.event.*;//not needed
class frame extends JFrame implements ActionListener
{
private JButton jbtvideo = new JButton("video");
public frame()
{
setTitle("Hi");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,100);
setLocation(324,20);
BorderLayout b = new BorderLayout();
setLayout(B)/>;
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,1));
p1.add(jbtvideo);
add(p1, BorderLayout.CENTER);
jbtvideo.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtvideo)
{
//What code do i write here to play the video?
}
}
}
public class button
{
public static void main(String[] args)
{
frame a = new frame();
a.setVisible(true);
}
}
the video class
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
public class MoviePlayer extends Application
{
@Override
public void start(final Stage stage) throws Exception
{
stage.setTitle("Movie Player");
Group root = new Group();
Media media = new Media("file:///C:/Users/Jun/Desktop/HTP.mp4");
final MediaPlayer player = new MediaPlayer(media);
MediaView view = new MediaView(player);
final VBox vbox = new VBox();
final Slider slider = new Slider();
vbox.getChildren().add(slider);
root.getChildren().add(view);
root.getChildren().add(vbox);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
player.setOnReady(new Runnable(){
public void run(){
int w = player.getMedia().getWidth();
int h = player.getMedia().getHeight();
stage.setMinWidth(w);
stage.setMinHeight(h);
vbox.setMinSize(w, 100);
vbox.setTranslateY(h-60);
slider.setMin(0.0);
slider.setValue(0.0);
slider.setMax(player.getTotalDuration().toSeconds());
}
});
player.currentTimeProperty().addListener(new ChangeListener<Duration>()
{
@Override
public void changed(ObservableValue<? extends Duration> observableValue, Duration duration, Duration current)
{
slider.setValue(current.toSeconds());
}
});
slider.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent)
{
player.seek(Duration.seconds(slider.getValue()));
}
});
}
}