JavaFX:我如何获得一个特定的选项卡来执行操作



我是一个初学Java的高中生。我试图使用Java Fx创建我自己的简单web浏览器,我遇到了一个问题。我有一个专门的标签名为"+"当我按下

时,我想创建一个新的选项卡这是主类:

package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.fxml.FXMLLoader;

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
VBox root = (VBox)FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root,800,600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

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

这是主控制器:

package application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.MenuButton;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.scene.control.TabPane;
public class MainController{
@FXML
private VBox mainWindow;
@FXML
private VBox topPanel;
@FXML
private TabPane tabPane;
@FXML
private Tab addTab;
@FXML
private HBox topBrowserPane;
@FXML
private Button backBtn;
@FXML
private Button fowardBtn;
@FXML
private TextField addressBar;
@FXML
private Button refreshBtn;
@FXML
private MenuButton settingsBtn;
@FXML
private WebView web;
@FXML
void getText(ActionEvent event) {
}
@FXML
void goBack(ActionEvent event) {
}
@FXML
void goFoward(ActionEvent event) {
}
@FXML
void refresh(ActionEvent event) {
}
}

这是我的场景生成器的fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.web.WebView?>

<VBox fx:id="mainWindow" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="620.0" prefWidth="785.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<VBox fx:id="topPanel" alignment="CENTER" prefHeight="75.0" prefWidth="785.0">
<children>
<TabPane fx:id="tabPane" prefHeight="35.0" prefWidth="785.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="New Tab" />
<Tab fx:id="addTab" closable="false" text="+" />
</tabs>
</TabPane>
<HBox fx:id="topBrowserPane" alignment="CENTER" prefHeight="45.0" prefWidth="785.0">
<children>
<Button fx:id="backBtn" alignment="CENTER" mnemonicParsing="false" onAction="#goBack" text="&lt;-" />
<Button fx:id="fowardBtn" alignment="CENTER" mnemonicParsing="false" onAction="#goFoward" text="-&gt;" />
<TextField fx:id="addressBar" onAction="#getText" prefHeight="26.0" prefWidth="541.0" promptText="Enter web address" />
<Button fx:id="refreshBtn" alignment="CENTER" mnemonicParsing="false" onAction="#refresh" text="Refresh" />
<MenuButton fx:id="settingsBtn" alignment="CENTER" mnemonicParsing="false" prefHeight="26.0" prefWidth="31.0" text="...">
<items>
<MenuItem mnemonicParsing="false" text="Settings" />
<MenuItem mnemonicParsing="false" text="Track Site" />
</items>
</MenuButton>
</children>
</HBox>
</children>
</VBox>
<WebView fx:id="web" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="780.0" />
</children>
</VBox>

所有GUI应用程序都是事件驱动的。事件是一种用户操作。您需要侦听选择以+作为文本的选项卡的用户操作。参考这个问题(和答案):JavaFX TabPane:如何听选择的变化

我猜当用户选择+选项卡时,您实际上想要在+选项卡和之前的选项卡之间插入一个新选项卡。下面的代码就是这样做的。注意,您需要将下面的方法添加到MainController类。(代码后面的注释)

public void initialize() {
SingleSelectionModel<Tab> selectionModel = tabPane.getSelectionModel();
ReadOnlyObjectProperty<Tab> selectedItemProperty = selectionModel.selectedItemProperty();
selectedItemProperty.addListener(new ChangeListener<Tab>() {
@Override
public void changed(ObservableValue<? extends Tab> observable,
Tab oldTab,
Tab newTab) {
if (newTab != null) {
String text = newTab.getText();
if ("+".equals(text)) {
ObservableList<Tab> tabs = tabPane.getTabs();
int count = tabs.size();
int index = count - 1;
tabs.remove(index);
tabs.add(new Tab("New Tab"));
tabs.add(new Tab("+"));
tabPane.getSelectionModel().clearSelection();
}
}
}
});
}

请注意,代码比需要的更冗长。我希望它能使你更容易理解。

TabPane的选项卡存储在一个列表中。当您向列表中添加选项卡时,它会被添加到末尾。没有插入方法。因此,我删除了最后一个选项卡,即一个与+。然后我添加一个新标签,最后我用+重新添加标签。

我还清除了选择,因为一旦您选择了一个选项卡,它就会保持选中状态,因此当您连续两次单击+选项卡时,选择不会改变,因此侦听器不会执行。我还假设您想在每次单击+选项卡时添加一个新选项卡。

最新更新