单击按钮时如何创建新选项卡?

时间:2017-08-09 03:56:33

标签: java javafx browser tabs fxml

我正在尝试创建一个浏览器。我有一个添加按钮,当它被点击时,它应该创建一个新选项卡。当程序刚刚打开时,按钮在第一个初始选项卡上单击时会起作用。新创建的选项卡获取初始选项卡的全部内容,因此在新选项卡中创建相同的按钮。问题是,当我单击新创建的选项卡上的按钮时,我无法创建新选项卡,只有第一个选项卡上的按钮可用。

$model = iconv('CP1255', 'UTF-8', $model);
$make  = iconv('CP1255', 'UTF-8', $make);

这是程序的FXML文件和初始第一个标签:

    @FXML public void addNewTab(ActionEvent event) {

    Tab newTab = new Tab("Untitled " + (tabPane.getTabs().size() + 1));
    try {

        tabPane.getTabs().add(newTab);
        tabPane.getSelectionModel().select(newTab);
        AnchorPane anchorPane = FXMLLoader.load(getClass().getResource("new-tab.fxml"));
        newTab.setContent(anchorPane);

    } catch(Exception e) {
        e.printStackTrace();
        System.out.println(e);
    }

}

这是新标签的FXML(“new-tab.fxml”):

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mybrowser.MyBrowserFXMLController">
   <children>
      <TabPane fx:id="tabPane" layoutY="28.0" prefHeight="691.0" prefWidth="1200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab fx:id="tab1" text="Untitled">
               <content>
                  <AnchorPane prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <Pane prefHeight="50.0" prefWidth="1200.0" style="-fx-border-width: 0px 0px 1px 0px; -fx-border-color: #FEDC01;">
                           <children>
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="12.0" mnemonicParsing="false" onAction="#addNewTab" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/add.png" />
                                       </image>
                                    </ImageView>
                                 </graphic>
                                 <padding>
                                    <Insets left="-10.0" right="-10.0" />
                                 </padding></Button>
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="40.0" layoutY="12.0" mnemonicParsing="false" onAction="#pagePrevious" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/back.png" />
                                       </image>
                                    </ImageView>
                                 </graphic>
                                 <padding>
                                    <Insets left="-12.0" right="-10.0" />
                                 </padding></Button>
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="70.0" layoutY="12.0" mnemonicParsing="false" onAction="#pageNext" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <padding>
                                    <Insets left="-10.0" right="-12.0" />
                                 </padding>
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/forward.png" />
                                       </image>
                                    </ImageView>
                                 </graphic></Button>
                              <TextField fx:id="addressBar" layoutX="110.0" layoutY="13.0" onKeyPressed="#enterURL" prefHeight="27.0" prefWidth="1050.0" promptText="Search or enter the website name" />
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="1163.0" layoutY="13.0" mnemonicParsing="false" onAction="#pageRefresh" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/refresh.png" />
                                       </image>
                                    </ImageView>
                                 </graphic>
                                 <padding>
                                    <Insets left="-12.0" right="-10.0" />
                                 </padding>
                              </Button>
                           </children>
                        </Pane>
                        <WebView fx:id="webView" layoutY="50.0" prefHeight="609.0" prefWidth="1200.0" />
                     </children>
                  </AnchorPane>
               </content>
          </Tab>
        </tabs>
      </TabPane>
      <MenuBar prefHeight="29.0" prefWidth="1200.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="New Tab" />
                  <MenuItem mnemonicParsing="false" text="Close Tab" />
                  <MenuItem mnemonicParsing="false" text="Close All Tabs" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="New Window" />
                  <MenuItem mnemonicParsing="false" text="Close Window" />
                  <MenuItem mnemonicParsing="false" text="Close All Windows" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="Close Browser" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
                  <MenuItem mnemonicParsing="false" text="Select All" />
              <MenuItem mnemonicParsing="false" text="Copy" />
                  <MenuItem mnemonicParsing="false" text="Paste" />
                  <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="View">
            <items>
              <MenuItem mnemonicParsing="false" text="Zoom In" />
                  <MenuItem mnemonicParsing="false" text="Zoom Out" />
            </items>
          </Menu>
            <Menu mnemonicParsing="false" text="History">
              <items>
                <MenuItem mnemonicParsing="false" onAction="#historyFull" text="Show Full History" />
                  <Menu mnemonicParsing="false" text="Show Recent History...">
                    <items>
                      <MenuItem mnemonicParsing="false" />
                    </items>
                  </Menu>
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="Delete History..." />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Help">
               <items>
                  <MenuItem mnemonicParsing="false" text="Find..." />
                  <MenuItem mnemonicParsing="false" text="About" />
               </items>
            </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>

如您所见,它只是第一个标签的内容。该FXML文件与最后一个FXML文件具有相同的控制器,该文件包括整个GUI的“基础”。此FXML中的按钮也已分配给控制器中的正确ActionEvent。但我继续遇到上述问题的错误:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>

<AnchorPane prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mybrowser.MyBrowserFXMLController" >
   <children>
      <Pane prefHeight="50.0" prefWidth="1200.0" style="-fx-border-width: 0px 0px 1px 0px; -fx-border-color: #FEDC01;">
         <children>
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="12.0" mnemonicParsing="false" onAction="#addNewTab" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/add.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-10.0" right="-10.0" />
               </padding>
            </Button>
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="40.0" layoutY="12.0" mnemonicParsing="false" onAction="#pagePrevious" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/back.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-12.0" right="-10.0" />
               </padding>
            </Button>
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="70.0" layoutY="12.0" mnemonicParsing="false" onAction="#pageNext" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/forward.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-10.0" right="-12.0" />
               </padding>
            </Button>
            <TextField fx:id="addressBar" layoutX="110.0" layoutY="13.0" onKeyPressed="#enterURL" prefHeight="27.0" prefWidth="1050.0" promptText="Search or enter the website name" />
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="1163.0" layoutY="13.0" mnemonicParsing="false" onAction="#pageRefresh" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/refresh.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-12.0" right="-10.0" />
               </padding>
            </Button>
         </children>
      </Pane>
      <WebView fx:id="webView" layoutY="50.0" prefHeight="609.0" prefWidth="1200.0" />
   </children>
</AnchorPane>

0 个答案:

没有答案