I\'m supposed to make this program. I sent you a picture of the page I\'m suppos
ID: 3600456 • Letter: I
Question
I'm supposed to make this program. I sent you a picture of the page I'm supposed to use. I'll also send you code-listing 15-14 the basic version of the program I'm supposed to make. I couldn't get it to run it keeps throwing a error with import javafx.scene.layout.GridPane; along with all the gridPane.add but I figured thats a problem with the import.
To see figure 15-27
https://photos.app.goo.gl/URxMfh8yv9Y75MFz2
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class GridPane extends Application{
@Override
public void start(Stage stage){
GridPane gridPane = new GridPane();
gridPane.add(new Button(" long (1,0) button "), 1,0);
gridPane.add(new Button("(1,0)"), 0,1);
gridPane.add(new Button("(3,1)"), 3,1);
gridPane.setGridLinesVisible(true);
stage.setScene(new Scene(gridPane));
stage.setTitle("GridPane Example 1");
stage.show();
}
}
Explanation / Answer
-I think following are the reasons for which you are not able to run the code,
1. You missed somw imports
2. You gave the name of your class "GridPane" , which is name of a predefined class in java so we cant use it for naming purpose. It may give errors.
3. The function main() is missing.
-Write this code & try, I think it should not give you any errors.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class GridPaneExample extends Application
{
@Override
public void start(Stage stage)
{
//Creating a Grid Pane
GridPane gridPane = new GridPane();
//Creating Buttons
Button button1 = new Button(" long (1,0) button ");
Button button2 = new Button("(1,0)");
Button button3 = new Button("(3,1)");
gridPane.add(button1 ,1,0);
gridPane.add(button2 ,0,1);
gridPane.add(button3 ,3,1);
//Creating a scene object
Scene scene = new Scene(gridPane);
stage.setScene(scene);
stage.setTitle("GridPane Example 1");
stage.show();
}
public static void main(String[] args)
{
Application.launch(args);
}
}
1) Save this code in a file with the name GridPaneExample.java.
2) Now compile and execute the saved java file from the command prompt using the following commands.
//javac GridPaneExample.java
//java GridPaneExample