0
javafx에서 간단한 응용 프로그램을 만들고 있습니다. 기본 화면에는 두 개의 탭이 있으며 각 탭에는 버튼이 있습니다. tab2의 (버튼) btn2를 클릭하면 장면에 직사각형이 표시됩니다. 내 코드는 다음과 같습니다. Main 클래스 :javafx 호출 버튼을 사용하여 장면에 사각형을 그립니다.
public class check extends Application
{
public Stage primaryStage;
private BorderPane rootLayout;
@Override
public void start(Stage primaryStage)
{
this.primaryStage = primaryStage;
this.primaryStage.setTitle("TabsExample");
RootLayout();
}
public void RootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(check.class.getResource("/view/tabs.fxml"));
rootLayout = (BorderPane) loader.load();
primaryStage.setScene(new Scene(rootLayout, 500, 500));
primaryStage.show();
control controller = loader.getController();
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
Control.java
public class control {
@FXML
private Tab tab1;
@FXML
private Tab tab2;
@FXML
private Button btn1;
@FXML
private Button btn2;
@FXML
private AnchorPane A2;
@FXML
private AnchorPane A1;
public control(){}
private check mainclass;
@FXML public void handleSubmitButtonAction(ActionEvent event)
{
System.out.println("button clicked");
Rectangle r = new Rectangle();
r.setX(50);
r.setY(50);
r.setWidth(200);
r.setHeight(100);
r.setArcWidth(20);
r.setArcHeight(20);
A2.getChildren().add(r);
}
public void setcheck(check ch) {
this.mainclass = ch;
}
}
**** FXML : ****
<BorderPane maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.control">
<top>
<TabPane prefHeight="1754.0" prefWidth="1374.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab id="tab1" text="Draw Circle">
<content>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Button id="btn1" layoutX="23.0" layoutY="44.0" mnemonicParsing="false" text="rectangle" />
</children>
</AnchorPane>
</content></Tab>
<Tab id="tab2" text="Draw Rectangle">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button id="btn2" layoutX="20.0" layoutY="58.0" mnemonicParsing="false" onAction="#handleSubmitButtonAction" prefHeight="15.0" prefWidth="77.0" text="circle" />
</children></AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</top>
</BorderPane>
나는 아주 긴 질문을 드려 죄송합니다. 이 코드를 사용하면 버튼 동작 수신기가 호출되고 "Button clicked"메시지가 콘솔에 인쇄됩니다. 그러나 사각형이 화면에 그려지지 않은 이유를 모르겠습니다.
감사합니다. 두 개의 앵커 창에 ID를 추가했습니다. \t ... 52 개 에 의해 발생 : 지금은 널 포인터 exception.Trace 무엇입니까 view.control.handleSubmitButtonAction (control.java:61) \t ... 61 이상에서 java.lang.NullPointerException이 \t을 .. Line 61은 A2.getChildren()에 추적합니다. add (r); –
죄송합니다. 적절한 태그는 "fx : id"입니다. 수정 된 답변 다른 ID 태그도 변경해야 할 수 있습니다. [JavaFX의 fx : id와 id의 차이점은 무엇입니까?] (http://stackoverflow.com/questions/23686325/whats-the-difference-between-fxid-and-id-in-javafx) – Justin
감사합니다. Justin 톤. 감사합니다. 저는 javafx를 배우려고하는 초보자입니다. fx : id와 id 태그의 차이점을 설명해 주시겠습니까 –