Question

Java (8th Edition) Chapter 6, Problem 12PGP Looking for a solution to this question. Edit: I...

Java (8th Edition)

Chapter 6, Problem 12PGP
Looking for a solution to this question.

Edit: I was missing the question..

Create a GUI or JavaFX application that displays a button with the text "Button 1". Underneath the button add a label with the text "label 1". Repeat with and additional "Button 2" and "Label 2". Add an image icon of your choice to the first button and the first label.

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

NOTE: make sure that you have icon1.png and icon2.png in the root directory of your project. if you are using netbeans, after copying the images, right click on project name and click paste, these images should appear along with other files in root directory like build.xml

// ButtonsAndLabels.java

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.ContentDisplay;

import javafx.scene.control.Label;

import javafx.scene.image.ImageView;

import javafx.scene.layout.Background;

import javafx.scene.layout.Pane;

import javafx.scene.layout.VBox;

import javafx.scene.text.TextAlignment;

import javafx.stage.Stage;

import javax.swing.ImageIcon;

public class ButtonsAndLabels extends Application{

    @Override

    public void start(Stage primaryStage) {

        //creating two buttons and two Labels

        Button button1=new Button("Button 1");

        Label label1=new Label("Label 1");

        Button button2=new Button("Button 2");

        Label label2=new Label("Label 2");

       

        //setting icon1.png as the icon of button1

        /**

         * make sure that you have icon1.png and icon2.png in the root directory

         * of your project. if you are using netbeans, after copying the images,

         * right click on project name and click paste, these images should appear

         * along with other files in root directory like build.xml

         */

        button1.setGraphic(new ImageView("file:icon1.png"));

        //aligning icon at the center

        button1.setContentDisplay(ContentDisplay.CENTER);

        //removing background of button, remove this line if you dont want to

        //remove the bounding box of the button.

        button1.setBackground(Background.EMPTY);

        //setting icon2.png as the icon for label1

        label1.setGraphic(new ImageView("file:icon2.png"));

        //aligning icon at the center

        label1.setContentDisplay(ContentDisplay.CENTER);

        //creating a VBox with all components arranged vertically

        VBox root=new VBox(button1,label1,button2,label2);

        //aligning center

        root.setAlignment(Pos.CENTER);

        //setting spacing between components

        root.setSpacing(10);

        //creating and displaying scene

        Scene scene=new Scene(root);

        primaryStage.setScene(scene);

        primaryStage.setTitle("");

        primaryStage.show();

    }

   

    public static void main(String[] args) {

        launch(args);

    }

   

}

/*OUTPUT*/

X Button 1 Label 1 Button 2 Label 2

//icon1.png

//icon2.png

Add a comment
Know the answer?
Add Answer to:
Java (8th Edition) Chapter 6, Problem 12PGP Looking for a solution to this question. Edit: I...
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
  • Create a GUI or JavaFX application with two buttons and two labels. Add an Image Icon of your choice to the first button...

    Create a GUI or JavaFX application with two buttons and two labels. Add an Image Icon of your choice to the first button and the first label.

  • What is the solution for chapter 15 3e for java programming 8th edition Create a JFrame...

    What is the solution for chapter 15 3e for java programming 8th edition Create a JFrame that holds fjive buttons with the names of five different fonts. Include a sexth button that the user can click to make a font larger or smaller. Display a demonstration JLabel usig the font and size that th user selects.

  • For this question you will need to complete the methods to create a JavaFX GUI application...

    For this question you will need to complete the methods to create a JavaFX GUI application that implements two String analysis algorithms. Each algorithm is activated when its associated button is pressed. They both take their input from the text typed by the user in a TextField and they both display their output via a Text component at the bottom of the GUI, which is initialized to “Choose a string methods as indicated in the partially completed class shown after...

  • Please provide a solution to "Data Structures and Problem Solving Using Java (4th Edition)," Chapter 6,...

    Please provide a solution to "Data Structures and Problem Solving Using Java (4th Edition)," Chapter 6, Problem 32E: https://www.chegg.com/homework-help/maintaining-invariant-elements-priority-queue-sorted-non-inc-chapter-6-problem-32E-solution-9780133001679-exc

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • Java FX Application Purpose The purpose of this assignment is to get you familiar with the...

    Java FX Application Purpose The purpose of this assignment is to get you familiar with the basics of the JavaFX GUI interface components. This assignment will cover Labels, Fonts, Basic Images, and Layouts. You will use a StackPane and a BorderPane to construct the layout to the right. In addition you will use the Random class to randomly load an image when the application loads Introduction The application sets the root layout to a BorderPane. The BorderPane can be divided...

  • Hi, I don't quite understand the solution to Chapter 10, Problem 62C of Data Structures and...

    Hi, I don't quite understand the solution to Chapter 10, Problem 62C of Data Structures and Algorithms in Java (6th Edition). The question is to design a variation of binary search for performing the get(k) operation on a sorted search table that includes duplicates. But what happens in step 1? is it a search for the first key value of k? In step 2, I don't understand what is being backtraced? The table? And why? In step 3, why are...

  • JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming...

    JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming and image handling using JavaFX. Create an application that responds to the user clicking command buttons. Initially, it will display one of two graphic images and, based upon the button clicked by the user, will switch the image displayed. If the user clicks the mouse button to display the same image as is currently displayed, the image is not changed, but a message at...

  • JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window....

    JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window. Create an event when a button is pushed. Create an event listener that will respond to the button being pushed event. Task 1 – Create a Window For Java: Please ensure that the following import statements are used: import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.GridPane; import javafx.scene.text.Text; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; Next, ensure that there is a main method which is...

  • It's important project. I need a help. Will give up vote for helping! Please use Java !!!!! Need ...

    It's important project. I need a help. Will give up vote for helping! Please use Java !!!!! Need clearly written java program code and sample output!!!!!! The popular social network Facebook TM was founded by Mark Zuckerberg and his classmates at Harvard University in 2004. At the time, he was a sophomore studying computer science. Design and implement an application that maintains the data for a simple social network. Each person in the network should have a profile that contains...

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