how can I fix this error ControlRectanglejava66 error invali
how can I fix this error ControlRectangle.java:66: error: invalid method declaration; return type required public RectanglePane() {
code :
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ControlRectangle extends Application {
private CirclePane circlePane = new CirclePane();
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Hold two buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btEnlarge = new Button(\"EnlargeTop\");
Button btEnlarge = new Button(\"EnlargeBottom\");
Button btShrink = new Button(\"ShrinkRight\");
Button btShrink = new Button(\"ShrinkLeft\");
hBox.getChildren().add(btEnlargeTop);
hBox.getChildren().add(btEnlargeBottom);
hBox.getChildren().add(btShrinkRight);
hBox.getChildren().add(btShrinkLeft);
// Create and register the handler
btEnlarge.setOnAction(new EnlargeHandler());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(rectanglePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
// Create a scene and place it in the stage
Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle(\"ControlRectangle\"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
class EnlargeHandler implements EventHandler {
@Override // Override the handle method
public void handle(ActionEvent e) {
rectanglePane.enlarge();
}
class ShrinkeHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {
rectanglePane.enlarge();
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
class CirclePane extends StackPane {
private Rectangle rectangle = new rectangle(25, 10, 60, 30);
public RectanglePane() {
getChildren().add(rectangle);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(Color.WHITE);
}
public void enlargeTop() {
rectangle.setHight(rectangle.getHight() + 2);
}
public void enlargeBottom() {
rectangle.setHight(rectangle.getHight() + 2);
}
public void shrinkRight() {
circle.setWidth(rectangle.getWidth() > 2 ?
circle.getWidth() - 2 : circle.getWidth());
}
public void shrinkLeft() {
circle.setWidth(rectangle.getWidth() > 2 ?
circle.getWidth() - 2 : circle.getWidth());
}
}
}
And could you please check if there are any errors that need to fix?
Solution
how can I fix this error ControlRectangle.java:66: error: invalid method declaration; return type required public RectanglePane() {
The problem with this is that, in the method definition part of RectanglePane(), the function return type should be specified. As you didn\'t specified its showing up this error. So, just change the method definition from
public RectanglePane() {
to: public void RectanglePane() {...
which means the return type of the method RectanglePane() is void, i.e., it doesn\'t return anything.
Hope this will work for you.
If you still have any problem, just get back to me with the whole code.
Here is the modified version for you:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ControlRectangle extends Application {
private CirclePane circlePane = new CirclePane();
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Hold two buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btEnlarge = new Button(\"EnlargeTop\");
Button btEnlarge = new Button(\"EnlargeBottom\");
Button btShrink = new Button(\"ShrinkRight\");
Button btShrink = new Button(\"ShrinkLeft\");
hBox.getChildren().add(btEnlargeTop);
hBox.getChildren().add(btEnlargeBottom);
hBox.getChildren().add(btShrinkRight);
hBox.getChildren().add(btShrinkLeft);
// Create and register the handler
btEnlarge.setOnAction(new EnlargeHandler());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(rectanglePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
// Create a scene and place it in the stage
Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle(\"ControlRectangle\"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
class EnlargeHandler implements EventHandler {
@Override // Override the handle method
public void handle(ActionEvent e) {
rectanglePane.enlarge();
}
class ShrinkeHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {
rectanglePane.enlarge();
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
class CirclePane extends StackPane {
private Rectangle rectangle = new rectangle(25, 10, 60, 30);
public void RectanglePane() {
getChildren().add(rectangle);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(Color.WHITE);
}
public void enlargeTop() {
rectangle.setHight(rectangle.getHight() + 2);
}
public void enlargeBottom() {
rectangle.setHight(rectangle.getHight() + 2);
}
public void shrinkRight() {
circle.setWidth(rectangle.getWidth() > 2 ?
circle.getWidth() - 2 : circle.getWidth());
}
public void shrinkLeft() {
circle.setWidth(rectangle.getWidth() > 2 ?
circle.getWidth() - 2 : circle.getWidth());
}
}
}



