我在javafx中创建一个简单的应用程序。主屏幕有两个选项卡,每个选项卡都有一个按钮。单击tab2中的(按钮)btn2时,应在场景上显示一个矩形。 这是我的代码。 主要课程:
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>
我为一个很长的问题道歉。使用此代码,将调用按钮动作侦听器并单击消息&#34;按钮&#34;打印在控制台上。但是,我不明白为什么矩形没有画在屏幕上。
答案 0 :(得分:0)
看起来你错过了FXML中AnchorPane的ID。此外,这里使用的适当标签是“fx:id”。您可以在此处阅读更多相关信息:What's the difference between fx:id and id: in JavaFX?
以下是更正后的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 fx:id="tab1" text="Draw Circle">
<content>
<AnchorPane fx:id="A1" prefHeight="200.0" prefWidth="200.0">
<children>
<Button fx:id="btn1" layoutX="23.0" layoutY="44.0" mnemonicParsing="false" text="rectangle" />
</children>
</AnchorPane>
</content></Tab>
<Tab fx:id="tab2" text="Draw Rectangle">
<content>
<AnchorPane fx:id="A2" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx: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>