无法在边框上显示Mediaview和square

时间:2016-04-15 01:34:55

标签: java javafx

我需要一些帮助,试图让两个东西出现在这个边框上。目前我将中心设置为Gridpane。我需要将一个mediaview和ButtonDemo类中显示的文本添加到Gridpane中。我还必须设置边框的顶部才能有一个移动的方块。我无法弄清楚如何为广场制作动画。但目前我无法显示这两件事。如果有人可以帮助解释为什么这两个对象将不会显示,这将是非常有帮助的。谢谢!

import javafx.application.Application;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

//Up and Down Button Class

class ButtonDemo extends Application {
   protected Text text = new Text(50, 50, "Assignment 7");
   public GridPane grid = new GridPane();

   protected BorderPane getPane() {
      HBox paneForButtons = new HBox(20);
      Button btUp = new Button("^ Up ");
      Button btDown = new Button("v Down ");
      paneForButtons.getChildren().addAll(btUp, btDown);
      paneForButtons.setStyle("-fx-border-color: green");

      BorderPane border = new BorderPane();
      border.setBottom(paneForButtons);

      Pane paneForText = new Pane();
      paneForText.getChildren().add(text);

      grid.setHgap(10);
      grid.setVgap(10);
      grid.setPadding(new Insets(0, 10, 0, 10));
      border.setCenter(grid);
      grid.add(paneForText, 1, 10);



      btUp.setOnAction(e -> text.setY(text.getY() - 10));
      btDown.setOnAction(e -> text.setY(text.getY() + 10));

      return border;

      }

      @Override
      public void start(Stage primaryStage) {
         Scene scene = new Scene(getPane(), 800, 650);
         primaryStage.setTitle("Assignment 7");
         primaryStage.setScene(scene);
         primaryStage.show();
      }
}

//Font Type Class

class CheckBoxDemo extends ButtonDemo {
  @Override
  protected BorderPane getPane() {
    BorderPane border = super.getPane();

    Font fontBoldItalic = Font.font("Arial",
      FontWeight.BOLD, FontPosture.ITALIC, 20);
    Font fontBold = Font.font("Arial",
      FontWeight.BOLD, FontPosture.REGULAR, 20);
    Font fontItalic = Font.font("Arial",
      FontWeight.NORMAL, FontPosture.ITALIC, 20);
    Font fontNormal = Font.font("Arial",
      FontWeight.NORMAL, FontPosture.REGULAR, 20);

    text.setFont(fontNormal);

    VBox paneForCheckBoxes = new VBox(20);
    paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5));
    paneForCheckBoxes.setStyle("-fx-border-color: green");
    CheckBox chkBold = new CheckBox("Bold");
    CheckBox chkItalic = new CheckBox("Italic");
    paneForCheckBoxes.getChildren().addAll(chkBold, chkItalic);
    border.setLeft(paneForCheckBoxes);

    EventHandler<ActionEvent> handler = e -> {
      if (chkBold.isSelected() && chkItalic.isSelected()) {
        text.setFont(fontBoldItalic);
      }
      else if (chkBold.isSelected()) {
        text.setFont(fontBold);
      }
      else if (chkItalic.isSelected()) {
        text.setFont(fontItalic);
      }      
      else {
        text.setFont(fontNormal);
      }
    };

    chkBold.setOnAction(handler);
    chkItalic.setOnAction(handler);

    return border;
  }
}

//Color Button Class

class RadioButtonDemo extends CheckBoxDemo {
  @Override
  protected BorderPane getPane() {
    BorderPane border = super.getPane();

    VBox paneForRadioButtons = new VBox(20);
    paneForRadioButtons.setPadding(new Insets(5, 5, 5, 5));
    paneForRadioButtons.setStyle("-fx-border-color: black");
    paneForRadioButtons.setStyle("-fx-border-color: green");
    RadioButton rbYellow = new RadioButton("Yellow");
    RadioButton rbOrange = new RadioButton("Orange");
    RadioButton rbPurple = new RadioButton("Purple");
    paneForRadioButtons.getChildren().addAll(rbYellow, rbOrange, rbPurple);
    border.setRight(paneForRadioButtons);

    ToggleGroup group = new ToggleGroup();
    rbYellow.setToggleGroup(group);
    rbOrange.setToggleGroup(group);
    rbPurple.setToggleGroup(group);

    rbYellow.setOnAction(e -> {
      if (rbYellow.isSelected()) {
        text.setFill(Color.YELLOW);
      }
    });

    rbOrange.setOnAction(e -> {
      if (rbOrange.isSelected()) {
        text.setFill(Color.ORANGE);
      }
    });

    rbPurple.setOnAction(e -> {
      if (rbPurple.isSelected()) {
        text.setFill(Color.PURPLE);
      }
    });

    return border;
  }
}

//Rectangle Bouncing Class

class BouncingRectangle extends RadioButtonDemo {
   private Rectangle rectangle = new Rectangle( 0, 0, 10, 10);
   private Timeline animation;
    @Override
    protected BorderPane getPane() {
    BorderPane border = super.getPane();

    Pane squarePane = new Pane();
    rectangle.setFill(Color.BLUE);
    squarePane.getChildren().add(rectangle);
    border.setTop(squarePane);

    return border;

    }
}

//MP4 Class
class MediaDemo extends RadioButtonDemo {
   private static final String MEDIA_URL =
      "http://cs.armstrong.edu/liang/common/sample.mp4";     
  @Override
  protected BorderPane getPane() {
  BorderPane border = super.getPane();

    Media media = new Media(MEDIA_URL);
    MediaPlayer mediaPlayer = new MediaPlayer(media);
    MediaView mediaView = new MediaView(mediaPlayer);

    Button playButton = new Button(">");
    playButton.setOnAction(e -> {
      if (playButton.getText().equals(">")) {
         mediaPlayer.play();
         playButton.setText("||");
      } else {
         mediaPlayer.pause();
         playButton.setText(">");
      }
    });

    Button rewindButton = new Button("<<");
    rewindButton.setOnAction(e -> mediaPlayer.seek(Duration.ZERO));

    Slider slVolume = new Slider();
    slVolume.setPrefWidth(150);
    slVolume.setMaxWidth(Region.USE_PREF_SIZE);
    slVolume.setMinWidth(30);
    slVolume.setValue(50);
    mediaPlayer.volumeProperty().bind(
      slVolume.valueProperty().divide(100));

      HBox hBox = new HBox(10);
      hBox.setAlignment(Pos.CENTER);
      hBox.getChildren().addAll(playButton, rewindButton,
         new Label("Volume"), slVolume);

      BorderPane paneForMedia = new BorderPane();
      paneForMedia.setCenter(mediaView);
      paneForMedia.setBottom(hBox);
      grid.add(paneForMedia, 6, 10);

      return border;
  }
}  

public class n00935124 extends RadioButtonDemo {
   public static void main(String[] args) {
    launch(args);
  }
}

再次感谢

1 个答案:

答案 0 :(得分:0)

首先,这是组织事物的非标准方式。在任何意义上都没有错(*),但只是如此不标准,我花了一段时间来弄清楚发生了什么。

您没有看到最后两个内容添加的原因是您的继承层次结构是错误的。你想要:

class MediaDemo extends BouncingRectangle

public class n00935124 extends MediaDemo

而不是扩展RadioButtonDemo(我猜测它只是一个复制粘贴错误)。

(*)我想问题是你只使用继承来在这里添加 state ,这是一种反模式。继承最好用于添加行为或提供行为的替代实现。