FXML Dokument:Wie füge ich der Szene in JavaFX eine Gruppe hinzu?
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.URL?>
<AnchorPane id="AnchorPane" fx:id="apMain" prefHeight="600" prefWidth="600" xmlns:fx="http://javafx.com/fxml/1" fx:controller="funkcje.FXMLDocumentController">
<stylesheets>
<URL value="@style.css" />
</stylesheets>
<children>
<Button prefHeight="20" prefWidth="70" layoutX="265" layoutY="550" text="Narysuj" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="30" layoutY="500" minHeight="16" minWidth="69" text="Podaj A:" fx:id="labelA" />
<TextField layoutX="105" layoutY="503" prefHeight="20" prefWidth="90" fx:id="textfieldA" />
<Label layoutX="200" layoutY="500" minHeight="16" minWidth="69" text="Podaj B:" fx:id="labelB" />
<TextField layoutX="275" layoutY="503" prefHeight="20" prefWidth="90" fx:id="textfieldB" />
<Label layoutX="370" layoutY="500" minHeight="16" minWidth="69" text="Podaj miare:" fx:id="labelC" />
<TextField layoutX="483" layoutY="503" prefHeight="20" prefWidth="90" fx:id="textfieldC" />
</children>
</AnchorPane>
FXML Controller:
package funkcje;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
/**
*
*
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
private AnchorPane apMain;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
Group group = new Group();
Rectangle r2 = new Rectangle(250, 25, 100, 140);
r2.setStroke(Color.BLACK);
r2.setFill(null);
r2.setStrokeWidth(3);
r2.setArcWidth(25);
r2.setArcHeight(25);
group.getChildren().add(r2);
apMain.getChildren().add(group);
}
}
main app:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package funkcje;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
*
*/
public class Funkcje extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setResizable(false);
stage.setTitle("Funkcje");
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Hier ist sie. Hilf mir. Da ist ein Text, den ich nicht posten kann, weil ich zu viel Code und weniger Text habe. Hier ist es. Hilf mir. Da ist ein Text, den ich nicht posten kann, weil ich zu viel Code und weniger Text habe. Hier ist es. Hilf mir. Da ist ein Text, den ich nicht posten kann, weil ich zu viel Code und weniger Text habe. Hier ist es. Hilf mir. Da ist ein Text, den ich nicht posten kann, weil ich zu viel Code und weniger Text habe.
http://stackoverflow.com/questions/18152639/javafx-adding-a-new-node-to -scene-in-java-code-wenn-scene-is-initial-loaded – Misys
Eine Szene hat nur einen Knoten: die Wurzel. Wenn Sie weitere Knoten hinzufügen möchten, fügen Sie sie zum Stamm hinzu. (Sie müssen den root als 'Pane' oder 'Group' verwenden, um 'getChildren(). Add (canva) '.) –
aufzurufen. Ich habe die obige Link-Verknüpfung durchgeführt, aber das Rechteck wird nicht angezeigt. schau dir den Code in der Post an – Sleeper