Question

/ Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import...

/ Finish the code to make it work

import static javafx.application.Application.launch;
import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;

public class JukeBox extends Application
{
private ChoiceBox<String> choice;
private AudioClip[] tunes;
private AudioClip current;
private Button playButton, stopButton;
  
//-----------------------
// presents an interface that allows the user to select and play
// a tune from a drop down box
//-----------------------
  
public void start(Stage primaryStage)

{
  
String [] names = { "Western Beats", "Classical Melody",
"Jeapordy Theme", "eightiesJam",
"New Age Rhythm", "lullaby",
"hitchcock"
};
  
  
  
// need to populate this list with the name of the songs
// that you want to use in this jukebox
// works with .wav and .mp3 file extensions
  
  
File [] audioFiles = {new File("westernBeat.wav"),
new File("classical.wav"),
new File("jeapordy.mp3"),
new File("eightiesJam.wav"),
new File("New Age Rhythm.wav"),
new File("lullaby.mp3"),
new File("hitchcock.wav")
};
  
tunes = new AudioClip[audioFiles.length];
for(int i = 0; i < audioFiles.length; i++)
{
tunes[i] = new AudioClip(audioFiles[i].toURI().toString());

}
  
current = tunes[0];
  
Label label = new Label("Select a tune: ");
  
choice = new ChoiceBox<String>();
choice.getItems().addAll(names);
choice.getSelectionModel().selectFirst();
choice.setOnAction(this::processChoice);
  
playButton = new Button("Play :) ");
stopButton = new Button("Stop :( ");
HBox buttons = new HBox(playButton, stopButton);
buttons.setSpacing(10);
buttons.setPadding(new Insets(15, 0, 0, 0));
buttons.setAlignment(Pos.CENTER);
  
playButton.setOnAction(this::processButtonPush);
stopButton.setOnAction(this::processButtonPush);
  
VBox root = new VBox(label, choice, buttons);
root.setPadding(new Insets(15, 15, 15, 25));
root.setSpacing(10);
root.setStyle("-fx-background-color: skyblue");
  
Scene scene = new Scene(root, 300, 150);
  
primaryStage.setTitle("Java Jupiter JukeBox");
primaryStage.setScene(scene);
primaryStage.show();
  
  
}
  
  
  
// -------------------------
// When a choice box selection is made3, stops current clip
// if one was playing, and sets the current tune
//--------------------------
public void processChoice(ActionEvent event)
{
current.stop();
current = tunes[choice.getSelectionModel().getSelectedIndex()];
  
}
  
//----------------------------
// handles the play and stop buttons. Stops the current clip
// in either case. If the play button was pressed. , restarts the
// current clip
//----------------------------
public void processButtonPush(ActionEvent event)
{
current.stop();
  
if(event.getSource() == playButton)
{
current.play();
}
}
  
/*
public static void main(String [] args)
{
launch(args);
}
*/
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code on Net Beans IDE using JDK8 and found two errors:

  • on line[25] one should use annotation Override
  • on line[63] one should not mention String inside<>.
  • IMPORTANT: The files mentioned in the code should be present in the source code directory.

The compete corrected code is below:

  1. import static javafx.application.Application.launch;
  2. import java.io.File;
  3. import javafx.application.Application;
  4. import javafx.event.ActionEvent;
  5. import javafx.geometry.Insets;
  6. import javafx.geometry.Pos;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.ChoiceBox;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.layout.HBox;
  12. import javafx.scene.layout.VBox;
  13. import javafx.scene.media.AudioClip;
  14. import javafx.stage.Stage;
  15. public class JukeBox extends Application
  16. {
  17. private ChoiceBox<String> choice;
  18. private AudioClip[] tunes;
  19. private AudioClip current;
  20. private Button playButton, stopButton;
  21.   
  22. //-----------------------
  23. // presents an interface that allows the user to select and play
  24. // a tune from a drop down box
  25. //-----------------------
  26.   
  27. @Override
  28. public void start(Stage primaryStage)
  29. {
  30.   
  31. String [] names = { "Western Beats", "Classical Melody",
  32. "Jeapordy Theme", "eightiesJam",
  33. "New Age Rhythm", "lullaby",
  34. "hitchcock"
  35. };
  36.   
  37.   
  38.   
  39. // need to populate this list with the name of the songs
  40. // that you want to use in this jukebox
  41. // works with .wav and .mp3 file extensions
  42.   
  43.   
  44. File [] audioFiles = {new File("westernBeat.mp3"),
  45. new File("classical.mp3"),
  46. new File("jeapordy.mp3"),
  47. new File("eightiesJam.mp3"),
  48. new File("New Age Rhythm.mp3"),
  49. new File("lullaby.mp3"),
  50. new File("hitchcock.mp3")
  51. };
  52.   
  53. tunes = new AudioClip[audioFiles.length];
  54. for(int i = 0; i < audioFiles.length; i++)
  55. {
  56. tunes[i] = new AudioClip(audioFiles[i].toURI().toString());
  57. }
  58.   
  59. current = tunes[0];
  60.   
  61. Label label = new Label("Select a tune: ");
  62.   
  63. choice = new ChoiceBox<>();
  64. choice.getItems().addAll(names);
  65. choice.getSelectionModel().selectFirst();
  66. choice.setOnAction(this::processChoice);
  67.   
  68. playButton = new Button("Play :) ");
  69. stopButton = new Button("Stop :( ");
  70. HBox buttons = new HBox(playButton, stopButton);
  71. buttons.setSpacing(10);
  72. buttons.setPadding(new Insets(15, 0, 0, 0));
  73. buttons.setAlignment(Pos.CENTER);
  74.   
  75. playButton.setOnAction(this::processButtonPush);
  76. stopButton.setOnAction(this::processButtonPush);
  77.   
  78. VBox root = new VBox(label, choice, buttons);
  79. root.setPadding(new Insets(15, 15, 15, 25));
  80. root.setSpacing(10);
  81. root.setStyle("-fx-background-color: skyblue");
  82.   
  83. Scene scene = new Scene(root, 300, 150);
  84.   
  85. primaryStage.setTitle("Java Jupiter JukeBox");
  86. primaryStage.setScene(scene);
  87. primaryStage.show();
  88.   
  89.   
  90. }
  91.   
  92.   
  93.   
  94. // -------------------------
  95. // When a choice box selection is made3, stops current clip
  96. // if one was playing, and sets the current tune
  97. //--------------------------
  98. public void processChoice(ActionEvent event)
  99. {
  100. current.stop();
  101. current = tunes[choice.getSelectionModel().getSelectedIndex()];
  102.   
  103. }
  104.   
  105. //----------------------------
  106. // handles the play and stop buttons. Stops the current clip
  107. // in either case. If the play button was pressed. , restarts the
  108. // current clip
  109. //----------------------------
  110. public void processButtonPush(ActionEvent event)
  111. {
  112. current.stop();
  113.   
  114. if(event.getSource() == playButton)
  115. {
  116. current.play();
  117. }
  118. }
  119.   
  120. /*
  121. public static void main(String [] args)
  122. {
  123. launch(args);
  124. }
  125. */
  126. }

This is the output window after running it successfully:

Java Jupiter JukeBox Select a tune: Classical Melody Play : Stop :(

Add a comment
Know the answer?
Add Answer to:
/ Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • use this code of converting Km to miles , to create Temperature converter by using java...

    use this code of converting Km to miles , to create Temperature converter by using java FX import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.geometry.Pos; import javafx.geometry.Insets; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.event.EventHandler; import javafx.event.ActionEvent; /** * Kilometer Converter application */ public class KiloConverter extends Application { // Fields private TextField kiloTextField; private Label resultLabel; public static void main(String[] args) { // Launch the application. launch(args); } @Override public void start(Stage primaryStage) { //...

  • (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import...

    (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK"); Button btCancel = new Button("Cancel"); EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { System.out.println("The OK button is clicked"); } }; btOK.setOnAction(handler); btCancel.setOnAction(handler); HBox pane = new HBox(5); pane.getChildren().addAll(btOK, btCancel); Scene...

  • Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets;...

    Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class NtoThePowerOfN extends Application{ @Override public void start(Stage primaryStage) throws Exception { TextField inputField; TextField outputField; VBox base = new VBox(10); base.setPadding(new Insets(10)); base.setAlignment(Pos.CENTER); // // A: input components - label and text field // // // B: button - to compute the value // // //...

  • Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees...

    Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees Celsius ((F - 32)*5/9). It needs to be JavaFX 1 import javafx.application. Application; 2 import javafx.stage. Stage; 3 import javafx.scene. Scene; 4 import javafx.scene.layout.HBox; 5 import javafx.scene.layout. VBox; 6 import javafx.geometry.Pos; 7 import javafx.geometry.Insets; 8 import javafx.scene.control.Label; 9 import javafx.scene.control. TextField; 10 import javafx.scene.control.Button; 11 import javafx.event. EventHandler; 12 import javafx.event. ActionEvent; 13 14 ** 15 * Kilometer Converter application 16 17 18 public...

  • You may adjust the code as you want. Thank you! CODING HERE: import javafx.application.Application; import javafx.event.*;...

    You may adjust the code as you want. Thank you! CODING HERE: import javafx.application.Application; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.geometry.*; public class OrderSystem extends Application implements EventHandler<ActionEvent> { // Attributes for GUI private Stage stage; // The entire window, including title bar and borders private Scene scene; // Interior of window private BorderPane layout; // Add four labels private Label itemLabel = new Label("Item Name:"); private Label numberLabel = new Label("Number:"); private Label costLabel...

  • This is my code. I need the UML for this code My code: public class Main...

    This is my code. I need the UML for this code My code: public class Main extends FlightManager {    Stage window;    Scene scene;    Button button;    HBox layout = new HBox(20);    Label dateTxt, airlineTxt, originTxt, destTxt, clssTxt, scopeTxt, adultsTxt, childTxt;    @Override public void start(Stage stage) { // create the UI and show it here    window = stage; window.setTitle("Reservations 2020"); button = new Button("New Flight");    VBox group3 = new VBox(10); dateTxt = Text("", group3);...

  • Can someone please comment this on what the javafx do and how they are implemented? import...

    Can someone please comment this on what the javafx do and how they are implemented? import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Paint; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class TextPanel extends Application { GridPane grid = null; Text scenetitle = null; Label tDrawLabel = null; Label bsc345Label = null; HBox THBox = null; VBox mVBox = new VBox(2); Stage primaryStage = null; @Override public void start(Stage...

  • Java Programming Assignment (JavaFX required). You will modify the SudokuCheckApplication program that is listed below. Start...

    Java Programming Assignment (JavaFX required). You will modify the SudokuCheckApplication program that is listed below. Start with the bolded comment section in the code below. Create a class that will take string input and process it as a multidimensional array You will modify the program to use a multi-dimensional array to check the input text. SudokuCheckApplication.java import javafx.application.*; import javafx.event.*; import javafx.geometry.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class SudokuCheckApplication extends Application { public void start(Stage primaryStage)...

  • Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13...

    Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13 Assignment 13 Preparation This assignment will focus on the use of object methods during event handling. Assignment 13 Assignment 13 Submission Follow the directions below to submit Assignment 13: This assignment will be a modification of the Assignment 12 program (see EncryptionApplication11.java below). Replace the use of String concatenation operations in the methods with StringBuilder or StringBuffer objects and their methods. EncryptTextMethods.java ====================== package...

  • Using JAVA FX how would I combine the two programs below into one interface that allows...

    Using JAVA FX how would I combine the two programs below into one interface that allows the user to pick which specific program they would like to play. They should be able to choose which one they want to play and then switch between them if necesary. All of this must be done in JAVA FX code Black jack javafx - first game program package application; import java.util.Arrays; import java.util.ArrayList; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.application.Application; import javafx.geometry.Pos; import javafx.geometry.Insets;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT