JavaFx目录选择Image和mp3并播放并显示它们

时间:2014-11-08 12:27:06

标签: javafx directory mp3

此刻我开始我的程序然后我可以选择一个dircetory。此信息将打印在Textfield2上。我在Textfiel1中复制此文本并添加" /textbild.png"例如。那么图像应该显示在另一个窗口中。与mp3一样的事情。 这是我的代码:

package javafxapplication1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javafx.scene.control.Label;
import javafx.scene.text.TextAlignment;
import javafx.scene.control.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.StageStyle;


import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
/**
 *
 * @author rodey_000
 */
public class JavaFXApplication1 extends Application {

 public static void main(String[] args) {
    launch(args);
}
@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root);
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();
    Button button1 = new Button("Bild");
    button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
    button1.setTranslateX(-430);
    button1.setTranslateY(30);
    String T ="TroubleSeeker.mp3";





    Label label1 = new Label("Bild");
    label1.setFont(Font.font("Cambria", 30));
    label1.setTranslateY(0); 

    Label label5 = new Label("User");
    label5.setFont(Font.font("Cambria", 30));
    label5.setTranslateY(100);
    label5.setTranslateX(-300);

    final TextField name = new TextField();
    name.setPromptText("Enter BildName.png.");
    name.setPrefColumnCount(10);
    name.setText("testbild.png");
    name.getText();
    name.setTranslateX(-40);


    final TextField name2 = new TextField();
    name2.setPromptText("Enter Songname.mp3.");
    name2.setPrefColumnCount(10);

    name2.getText();
    name2.setTranslateX(-270);
    name2.setTranslateY(55);


    final TextField name3 = new TextField();
    name3.setPromptText("Enter User");
    name3.setPrefColumnCount(10);
    name3.setText("Michael");
    name3.getText();

    name3.setTranslateX(-440);
    name3.setTranslateY(100);


    DirectoryChooser directoryChooser = new DirectoryChooser();
            File selectedDirectory = directoryChooser.showDialog(stage);
  String a= selectedDirectory.getAbsolutePath();
    name2.setText(a);

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

        public void handle(ActionEvent t) {
         Stage secondaryStage = new Stage(StageStyle.UTILITY);
         Group root2 = new Group();
         Scene scene2 = new Scene(root2);
         String TextBild = name.getText();
         String TextAufBild = name3.getText();
         String Song = name2.getText();


         int lenght = name3.getLength();





               Image image = new Image(getClass().getResourceAsStream(TextBild));

         double high = image.getHeight();
         double widht = image.getWidth();

            secondaryStage.setTitle("Label Sample");
            secondaryStage.setWidth(600);
            secondaryStage.setHeight(300);


        Label label3 = new Label();
         label3.setGraphic(new ImageView(image));

         Label label4= new Label(TextAufBild);
         label4.setFont(new Font("", 50));
         label4.setTextAlignment(TextAlignment.JUSTIFY);
         label4.setTranslateY(50);

         if(lenght == 1){
         label4.setTranslateX(-340);
         }
         else if(lenght == 2){
         label4.setTranslateX(-350);
         }
         else if(lenght == 3){
         label4.setTranslateX(-360);
         }
         else if(lenght == 4){
         label4.setTranslateX(-370);
         }
         else if(lenght == 5){
         label4.setTranslateX(-380);
         }
         else if(lenght == 6){
         label4.setTranslateX(-390);
         }
         else if(lenght == 7){
         label4.setTranslateX(-400);
         }
         else if(lenght == 8){
         label4.setTranslateX(-410);
         }
         else if(lenght == 9){
         label4.setTranslateX(-420);
         }
         else if(lenght == 10){
         label4.setTranslateX(-430);
         }
         else if(lenght == 11){
         label4.setTranslateX(-440);
         }
          else if(lenght == 12){
         label4.setTranslateX(-450);
         }
          else if(lenght == 13){
         label4.setTranslateX(-460);
         }
          else if(lenght == 14){
         label4.setTranslateX(-470);
         }
          else if(lenght == 15){
         label4.setTranslateX(-480);
         }

         HBox hbox2 = new HBox();  
         hbox2.setSpacing(10);

         hbox2.getChildren().add((label3));
         hbox2.getChildren().add((label4));

         ((Group)scene2.getRoot()).getChildren().add(hbox2);

         secondaryStage.setTitle("Secondary Stage"); 
         secondaryStage.setScene(scene2);
         secondaryStage.show();


       InputStream thing = getClass().getResourceAsStream(Song);

         Media audioFile = new Media( thing.toString() );
         MediaPlayer m = new MediaPlayer(audioFile);   
        m.play();






        }
    });



    Label label2 = new Label("Song");
    label2.setFont(Font.font("Cambria", 30));
    label2.setTranslateY(50);
    label2.setTranslateX(-58);
    label2.setTranslateZ(1);








    hbox.setSpacing(10);
    hbox.getChildren().add(label1);
    hbox.getChildren().add(label2);
    hbox.getChildren().add(name);
    hbox.getChildren().add((label5));
    hbox.getChildren().add(name2);
    hbox.getChildren().add(name3);

    hbox.getChildren().add(button1);
    ((Group)scene.getRoot()).getChildren().add(hbox);

    stage.setScene(scene);
    stage.show();
}

1 个答案:

答案 0 :(得分:0)

我认为问题在于此行

Image image = new Image(getClass().getResourceAsStream(TextBild));
....

InputStream thing = getClass().getResourceAsStream(Song);
Media audioFile = new Media( thing.toString() );

在变量a中保存了目录选择 所以你用以下代码替换以前的代码:

Image image = new Image("file:/"+a+TextBild);
....
Media audioFile = new Media("file:/"+a+Song);

如果你在图像和歌曲的TextField上添加路径(例如:c:\ image \ image.png和c:\ mp3 \ song.mp3),那么使用这个

Image image = new Image("file:/"+TextBild);
....
Media audioFile = new Media("file:/"+Song);