问题描述
我知道这个问题已有答案,但不知何故,它无法解决我的问题。
I am aware that there are already answers for this question but somehow, it was not able to solve my problem.
当我点击IMAGE1中的文本字段时,我想要键盘FXML(IMAGE2)在IMAGE3中显示。但问题是,我似乎无法找到解决方案。我该怎么做?
When I click the textfield in IMAGE1, I want the keyboard FXML(IMAGE2) to appear as is in IMAGE3. But the thing is, I can't seem to find the solution. How do I do this?
我需要你的帮助。
IMAGE1
IMAGE2
IMAGE3
推荐答案
我正在尝试类似的东西,这是我想出的(许多可能的解决方案之一):
I am experimenting with something similar, here is what I came up with (one of many possible solutions):
您需要将主布局(IMAGE1)包装在 StackPane
中。它的用法是覆盖需要覆盖的内容(在你的情况下是键盘)。键盘放在另一个窗格中(以下示例中为 VBox
)。 VBox
:
You need to wrap your main layout (IMAGE1) in a StackPane
. Its usage is to overlay what needs be overlaid (the keyboard in your case). The keyboard is placed in another pane (a VBox
in the following example). The VBox
:
- 放在
StackPane
在主要布局之后 - 被赋予最大高度,因此它不会填满整个窗口
- 获得
translateY
等于身高 - 和底部对齐
- is placed in the
StackPane
after the main layout to sit on top of it - is given a maximum height, so that it doesn't fill the entire window
- is given a
translateY
equal to the height - and bottom alignment
相关代码n FXML:
The relevant code n FXML:
<VBox fx:id="statusContainer" maxHeight="100.0" prefHeight="100.0"
translateY="100.0" StackPane.alignment="BOTTOM_LEFT" />
这将始终位于视图之外。切换键盘需要2 TranslateTransition
s(可以用1完成,我想知道吗?),一个用于向上移动键盘,一个向下移动。
This will always be outside of the view. Toggling the keyboard requires 2 TranslateTransition
s (can it be done with 1, I wonder?), one to move the keyboard up, one down.
示例代码:
1)Java:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Test1 extends Application
{
@FXML private VBox statusContainer;
private TranslateTransition showStatus;
private TranslateTransition hideStatus;
boolean showsStatus = false;
@Override
public void start(Stage primaryStage) {
try {
StackPane page = (StackPane) FXMLLoader.load(this.getClass().getResource("test1.fxml"));
Scene scene = new Scene(page);
primaryStage.setTitle(this.getClass().getName());
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
catch (IOException e) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
}
}
@FXML void initialize() {
showStatus = new TranslateTransition(Duration.millis(250), statusContainer);
showStatus.setByY(-100.0);
showStatus.setOnFinished(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
showsStatus = true;
}
});
hideStatus = new TranslateTransition(Duration.millis(250), statusContainer);
hideStatus.setByY(100.0);
hideStatus.setOnFinished(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
showsStatus = false;
}
});
}
public void toggleStatus() {
if( showsStatus ) {
showStatus.stop();
hideStatus.play();
}
else {
hideStatus.stop();
showStatus.play();
}
}
public static void main(String[] args) {
launch(args);
}
}
2)FXML(称之为 test1.fxml
以匹配代码):
2) FXML (call it test1.fxml
to match the code):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<StackPane id="StackPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="fancymsg.FancyMsg1">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Button mnemonicParsing="false" onAction="#toggleStatus" text="Status" AnchorPane.leftAnchor="50.0" AnchorPane.topAnchor="100.0" />
</children>
</AnchorPane>
<VBox fx:id="statusContainer" maxHeight="100.0" prefHeight="100.0" translateY="100.0" StackPane.alignment="BOTTOM_LEFT" />
</children>
<stylesheets>
<URL value="@test1.css" />
</stylesheets>
</StackPane>
3) VBox
的CSS脱颖而出(称之为 test1.css
):
3) The CSS for the VBox
to stand out (call it test1.css
):
#statusContainer {
-fx-background-color: -fx-color;
}
这篇关于在舞台上调用另一个FXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!