java - javafx窗口用按钮来回切换
问题描述
我遇到了这样一个事实,即我在它们单独的 java 类中都有 2 个窗格,我想要一个按钮来打开页面上的另一个(所以使用按钮从一个窗格转到另一个窗格)但它并不是真的似乎工作。我刚开始在我的学校编码。任何帮助都会很棒。
这是我在我的主类上使用的代码,如果需要在我的主类中使用窗格的窗格名称和在我的辅助类中的根,则辅助称为 panle2.java。
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new HBox(10);
pane.setPadding(new Insets(5, 5, 5, 5));
Image image = new Image("picachu_pok__mon-logo-D361BD95C6-seeklogo.com.png");
pane.getChildren().add(new ImageView(image));
Button button = new Button();
button.setOnAction(event -> {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/A.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
});
primaryStage.setTitle("pokemon stats");
Scene value = new Scene(pane, 1000, 600);
primaryStage.setScene(value);
button.setOnAction(event -> {
value.setRoot(pane);
});
ImageView imageView2 = new ImageView(image);
imageView2.setFitHeight(100);
imageView2.setFitWidth(100);
pane.getChildren().add(imageView2);
pane.getChildren().add(button);
ImageView imageView3 = new ImageView(image);
imageView3.setRotate(90);
pane.getChildren().add(imageView3);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}`
解决方案
您的代码有多个问题。
Pane pane = new HBox(10);
-> 我猜这会导致问题。我在我的代码中尝试过它,它会产生一个错误root.setAlignment(Pos.CENTER);
。另外,我相信这会给必须回来阅读代码的人造成混乱。- 您有一个
Button
,但您为Button
. - 如果您要尝试更改
Scene
,那么第二个Scene
需要一个Button
可以让您回到第一个的Scene
。(本例不采用这种方法。)
在下面的示例中,我不尝试更改Scene
. Nodes
我只是在VBox
索引零处切换。这可能是一个更好的方法。我不知道。我还setUserData()
用来跟踪Node
当前加载到VBox
索引零中的那个。
主要的
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication269 extends Application
{
@Override
public void start(Stage primaryStage)
{
VBox root = new VBox();
Scene scene = new Scene(root, 1000, 600);
Button btnPaneChanger = new Button("Click Me!");
Label label = new Label("I am Pane 1!");
StackPane pane1 = new StackPane(label);
pane1.setPrefSize(1000, 500);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(pane1, btnPaneChanger);//Set the first pane in index zero of the VBox
btnPaneChanger.setUserData("pane1");//Use setUserData to keep up with which Pane is currently loaded
btnPaneChanger.setOnAction((event) -> {
switch (btnPaneChanger.getUserData().toString()) {
case "pane1": {
try {
FXMLLoader listViewLoader = new FXMLLoader(getClass().getResource("pane2.fxml"));
StackPane pane2 = ((StackPane) listViewLoader.load());//StackPane is the root node in the FXML
root.getChildren().set(0, pane2);
btnPaneChanger.setUserData("pane2");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
break;
case "pane2":
root.getChildren().set(0, pane1);
btnPaneChanger.setUserData("pane1");
break;
}
});
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
>Pane 2 Controller
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
/**
* FXML Controller class
*
* @author blj0011
*/
public class Pane2Controller implements Initializable
{
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
}
}
窗格 2 FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication269.Pane2Controller">
<children>
<Label text="This is Pane 2">
<font>
<Font size="37.0" />
</font>
</Label>
</children>
</StackPane>
推荐阅读
- javascript - 可以在与用户一起滚动的本机反应中做固定项目吗?
- javascript - 当用户改变位置或坐标改变时更新地图上标记的位置
- visual-c++ - 如何将 IWinHttpRequest 导入 MSVC++ 项目?
- php - 在 PHP 中的 Android facebook 应用程序中打开 facebook 登录?
- c# - AC# 项目无法在其他计算机上运行
- azure - 调试 ARM 部署脚本的技术
- c# - 如何在 LINQ 中进行选择后进行选择
- regex - 提取开头和结尾之间的单词,\G \K
- sql - 需要 Postgres 查询协助
- python - 从所有数据帧条目中减去一个值