JavaFX

2016-04-06 41 views
0

düğmelerini kullanarak bir dikdörtgenin yeniden boyutlandırılmasıyla ilgili ev ödevi yardımı Öyleyse ödevlerim bu controlCircle programını alıp bir daire yerine bir dikdörtgen çizen bir programa dönüştürmektir. Program gerekir: kullanıcı tepki dört düğmeyi kullanarakJavaFX

  • kontrol bir dikdörtgen boyut ve dikdörtgen geniş hale yapar, düğme ile ilgili olarak daha dar, daha uzun ya da daha kısa

herhangi bir yardım ve Bir dikdörtgenin uzunluğuna, genişliğine, uzunluğuna ve kısalıklarına nasıl baktıklarını, en büyük sorunum olacağını düşündüğümden büyük bir mutluluk duyacağım. İşte ControlCircle.java'nın kodu:

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.Circle; 
import javafx.stage.Stage; 

public class controlCircle 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("Enlarge"); 
    Button btShrink = new Button("Shrink"); 
    hBox.getChildren().add(btEnlarge); 
    hBox.getChildren().add(btShrink); 
    // Create and register the handler 
    btEnlarge.setOnAction(e -> { 
     circlePane.enlarge(); 
    }); 
    btShrink.setOnAction(new ShrinkHandler()); 

    BorderPane borderPane = new BorderPane(); 
    borderPane.setCenter(circlePane); 
    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("ControlCircle"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage 
    } 

    class ShrinkHandler implements EventHandler<ActionEvent> { 
     @Override // Override the handle method 
     public void handle(ActionEvent e) { 
      circlePane.shrink(); 
     } 
     } 

    public static void main(String[] args) { 
    launch(args); 
    } 
} 

class CirclePane extends StackPane { 
    private Circle circle = new Circle(50); 

    public CirclePane() { 
    getChildren().add(circle); 
    circle.setStroke(Color.BLACK); 
    circle.setFill(Color.WHITE); 
    } 

    public void enlarge() { 
    circle.setRadius(circle.getRadius() + 2); 
    } 

    public void shrink() { 
    circle.setRadius(circle.getRadius() > 2 ? 
     circle.getRadius() - 2 : circle.getRadius()); 
    } 
} 
+0

Bu [metin kutuları yerleştirerek ve boyutlandırma için kodu] (https://gist.github.com/jewelsea/2724651) ihtiyacınız olandan daha karmaşıktır ama durumunuzla ilgili bir şeyler öğrenerek öğrenebilirsiniz. – jewelsea

cevap

0

Basittir. Aşağıdaki kodu kullanın ve anlayın. Ben size yardımcı olacaktır umut

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 

public class ControlRectangle extends Application { 
    private RectanglePane rectanglePane = new RectanglePane(); 

    @Override 
    public void start(Stage primaryStage) { 
    HBox hBox = new HBox(); 
    hBox.setSpacing(5); 
    hBox.setAlignment(Pos.CENTER); 
    Button btTaller = new Button("Taller"); 
    Button btSmaller = new Button("Smaller"); 
    Button btWider = new Button("Wider"); 
    Button btNarrower = new Button("Narrower"); 
    hBox.getChildren().add(btTaller); 
    hBox.getChildren().add(btSmaller); 
    hBox.getChildren().add(btWider); 
    hBox.getChildren().add(btNarrower); 
    // Create and register the handler 
    btTaller.setOnAction(e -> rectanglePane.taller()); 
    btSmaller.setOnAction(e -> rectanglePane.smaller()); 
    btWider.setOnAction(e -> rectanglePane.wider()); 
    btNarrower.setOnAction(e -> rectanglePane.narrower()); 

    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, 250, 200); 
    primaryStage.setTitle("ControlRectangle"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage 
    } 


    public static void main(String[] args) { 
    launch(args); 
    } 
} 

class RectanglePane extends StackPane { 
    private Rectangle rectangle = new Rectangle(150,75); 

    public RectanglePane() { 
    getChildren().add(rectangle); 
    rectangle.setStroke(Color.BLACK); 
    rectangle.setFill(Color.WHITE); 
    } 

    public void taller() { 
     rectangle.setHeight(rectangle.getHeight() + 2); 
    } 

    public void smaller() { 
     rectangle.setHeight(rectangle.getHeight() - 2); 
    } 

    public void wider() { 
     rectangle.setWidth(rectangle.getWidth() + 2); 
    } 

    public void narrower() { 
     rectangle.setWidth(rectangle.getWidth() - 2); 
    } 

} 

..