![](/img/trans.png)
[英]How to make a JavaFX textfield not visible but still allow text to be entered into it
[英]How to show make a hidden form visible in javafx
我有一個設計為三明治商店系統的應用程序,並且有一個主菜單表單,這是我的主類,即該類保存我的主類,並且是執行該應用程序時打開的第一個表單。
當我單擊“創建新的標准訂單”按鈕時,我會看到它,因此它將顯示一個菜單供您選擇。 我在創建新的標准訂單按鈕后面有此代碼,因此它將隱藏第一個窗口並顯示下一個窗口。
btnStdOrder.setOnAction(e -> {
((Node)e.getSource()).getScene().getWindow().hide();
NewOrderPopUp.Display();
});
但是,如何返回第一個窗口? 我嘗試使用與上面相同的代碼,但是因為第一種形式包含我的main和start方法,所以我無法使用上述方法再次調用它們(或者也許我不知道該怎么做)。 任何幫助將不勝感激。
如果您的popUpDisplay方法以showAndWait()結尾,則可以嘗試以下操作:
((Stage)((Node)e.getSource()).getScene().getWindow()).hide();
NewOrderPopUp.Display();
((Stage)((Node)e.getSource()).getScene().getWindow()).show();
我創建了一個示例應用程序,該應用程序顯示了此行為。
主要
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication53 extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
控制器-MainScene
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.logging.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
/**
*
* @author blj0011
*/
public class FXMLDocumentController implements Initializable
{
@FXML private Button btnMain;
Stage window;
@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
btnMain.setOnAction(e -> {
((Stage)((Node)e.getSource()).getScene().getWindow()).hide();
popUpDisplay();
((Stage)((Node)e.getSource()).getScene().getWindow()).show();
});
}
public void popUpDisplay()
{
try
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SceneTwo.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
//stage.initStyle(StageStyle.UNDECORATED);
stage.setTitle("PopUp");
stage.setScene(new Scene(root1));
stage.showAndWait();
}
catch (IOException ex)
{
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
FXML-主要場景
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" maxHeight="300.0" maxWidth="300.0" minHeight="300.0" minWidth="300.0" prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication53.FXMLDocumentController">
<children>
<Button fx:id="btnMain" layoutX="124.0" layoutY="248.0" mnemonicParsing="false" text="Button" />
<Label layoutX="99.0" layoutY="135.0" text="Main Scene">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
控制器-彈出窗口
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;
/**
* FXML Controller class
*
* @author blj0011
*/
public class SceneTwoController implements Initializable
{
@FXML Button btnClosePopup;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
btnClosePopup.setOnAction(e -> {
((Stage)(((Button)e.getSource()).getScene().getWindow())).close();
});
}
}
FXML-彈出式
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication53.SceneTwoController">
<children>
<Label layoutX="257.0" layoutY="155.0" text="Popup">
<font>
<Font size="30.0" />
</font>
</Label>
<Button fx:id="btnClosePopup" layoutX="259.0" layoutY="353.0" mnemonicParsing="false" text="Close Popup" />
</children>
</AnchorPane>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.