Skocz do zawartości

Przełożenie programu napisanego w javiefx na FXML+ kontroler


ZYWIEC

Recommended Posts

Witam mam napisany video player w javiefx i cały GUI jest napisany kodem, a potrzebuję przełożyć go na FXML + jego kontroler. Próbuje sam, ale po skompilowaniu opcja Otwórz (FileChooser) ani nawet zamknij nie działa. Tak wygląda kontroller:

public class VideoMainController implements Initializable {

   private MediaPlayer mediaPlayer;
   private Media media;

    @FXML
    private Slider volumeSlider;

    @FXML
    private Button btnStop;

    @FXML
    private Button btnPlay;

    @FXML
    private Button btnPause;

    @FXML
    private Button btnOtworz;

    @FXML
    private Slider slider1;

    @FXML
    private MediaView mediaplayer;

    @FXML
    private MenuItem btnExit;

 private void OpenFile(String name)
    {

        if(mediaPlayer != null) mediaPlayer.stop();
         try{

            media = new Media(new File(name).toURI().toURL().toExternalForm());
        }catch(MalformedURLException ex)
        {

            System.out.printf("Nie można załadować pliku");
            return;
        }

        mediaPlayer = new MediaPlayer(media);

        mediaplayer.setMediaPlayer(mediaPlayer);

        mediaPlayer.setOnReady(new Runnable(){
            @Override
            public void run() {

                slider1.setMin(0.0);
                slider1.setValue(0.0);
                slider1.setMax(mediaPlayer.getTotalDuration().toSeconds());

                mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
                    @Override
                    public void changed(ObservableValue<? extends Duration> observableValue, Duration duration, Duration current) {
                        slider1.setValue(current.toSeconds());
                    }
                });
            }
        });
        mediaPlayer.play();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        btnPlay.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                mediaPlayer.play();
            }
        });

        btnStop.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                mediaPlayer.stop();
            }
        });

        btnPause.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                mediaPlayer.pause();
            }
        });
        btnExit.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				Platform.exit();
			}
                        });
        btnOtworz.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                Node node = (Node) event.getSource();
                FileChooser fileChooser = new FileChooser();
                fileChooser.setTitle("Open file");
                File file = fileChooser.showOpenDialog(mediaplayer.getScene().getWindow());
                if (file != null) {
                    OpenFile(file.getPath());
                }
            }
        });
    }
    }

A tak wygląda plik FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.media.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <MenuBar layoutX="2.0" layoutY="8.0" prefHeight="25.0" prefWidth="60.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
                  <MenuItem fx:id="btnExit" mnemonicParsing="false" text="Zamknij" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <Button fx:id="btnPlay" layoutX="144.0" layoutY="77.0" mnemonicParsing="false" text="Play" />
      <Button fx:id="btnPause" layoutX="208.0" layoutY="77.0" mnemonicParsing="false" text="Pause" />
      <Button fx:id="btnStop" layoutX="274.0" layoutY="77.0" mnemonicParsing="false" text="Stop" />
      <Slider fx:id="slider1" layoutX="14.0" layoutY="45.0" prefHeight="14.0" prefWidth="565.0" />
      <Slider fx:id="volumeSlider" layoutX="338.0" layoutY="83.0" />
      <MediaView fx:id="mediaplayer" fitHeight="250.0" fitWidth="550.0" layoutX="25.0" layoutY="112.0" />
      <Button fx:id="btnOtworz" layoutX="62.0" layoutY="8.0" mnemonicParsing="false" text="Otwórz" />
   </children>
</AnchorPane>

Ew. czy ktoś za jakąś niewielką opłatą mógłby mi to ładnie przełożyć? To tylko jedna klasa kodu. Wyślę go zainteresowanym.

Link do komentarza
Udostępnij na innych stronach

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...