使用JavaFX FXML调整新窗口大小的问题

我无法自动将舞台大小调整为新窗口的大小。我在StackOverflow上搜索了一些相关问题,但找不到我的问题的答案。我正在使用NetBeans 8.2。

有一个窗口,AcmeInitial

控制器:

public class AcmeInitialController implements Initializable{

    @FXML
    private BorderPane rootPane;

    @FXML
    private Button bReclamacao;

    @FXML
    private Button bLigacao;

    @FXML
    private Button bSair;

    public void handleButtonLigacao(actionEvent e) throws IOException{

        try{

           BorderPane pane = FXMLLoader.load(getclass().getResource("AcmeSAC.fxml"));           
           rootPane.getchildren().setall(pane);

        }catch (IOException ex){

            System.out.println("IO - Erro ao migrar para a tela AcmeReclam.fxml.");
            System.out.println(ex);
            System.out.println(ex.getcause());

        }

    }

    public void handleButtonReclamacao(actionEvent e) throws IOException{

         try{

           //Stage stage = (Stage) rootPane.getParent().getScene().getWindow();
          // Scene scene = rootPane.getParent().getScene();

           BorderPane pane = FXMLLoader.load(getclass().getResource("AcmeReclam.fxml"));
           //stage.setTitle("Tela de Reclamação");

           //stage.setMinHeight(pane.getPrefHeight());
           //stage.setMinHeight(pane.getPrefWidth());

           rootPane.getchildren().setall(pane);

        }catch (IOException ex){

            System.out.println("IO - Erro ao migrar para a tela AcmeReclam.fxml.");
            System.out.println(ex);
            System.out.println(ex.getcause());

        }


    }

    public void handleButtonSair(actionEvent e) throws IOException{

        rootPane.getchildren().removeAll(rootPane);
        system.exit(0);

    }

    @Override
    public void initialize(URL url,ResourceBundle rb) {
        // TODO
    }  

}

handleButtonReclamacao方法将调用窗口AcmeReclam.fxml

FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>

<BorderPane fx:id="realPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="560.0" prefWidth="482.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="acmetelas.AcmeReclamController">
    <AnchorPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="560.0" prefWidth="482.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <Label layoutX="57.0" layoutY="36.0" text="Email:">
             <font>
                <Font size="16.0" />
             </font>
          </Label>
          <Label layoutX="57.0" layoutY="73.0" prefHeight="20.0" prefWidth="80.0" text="Telefone:">
             <font>
                <Font size="16.0" />
             </font>
          </Label>
          <Label layoutX="57.0" layoutY="219.0" text="Natureza do problema:">
             <font>
                <Font size="16.0" />
             </font>
          </Label>
          <Label layoutX="57.0" layoutY="141.0" text="Data da compra:">
             <font>
                <Font size="16.0" />
             </font>
          </Label>
          <Label layoutX="57.0" layoutY="176.0" text="Data da reclamação:">
             <font>
                <Font size="16.0" />
             </font>
          </Label>
          <Label layoutX="59.0" layoutY="348.0" text="Efeitos colaterais:" translateX="-2.0">
             <font>
                <Font size="16.0" />
             </font>
          </Label>
          <TextField fx:id="tfEmail" layoutX="113.0" layoutY="33.0" onaction="#handleTextFieldEmail" prefHeight="28.0" prefWidth="308.0" />
          <TextField fx:id="tfTelefone" layoutX="137.0" layoutY="69.0" onaction="#handleTextFieldTelefone" prefHeight="28.0" prefWidth="284.0" />
          <CheckBox fx:id="cbAtraso" layoutX="57.0" layoutY="251.0" mnemonicParsing="false" onaction="#handleCheckBoxAtraso" text="Atraso na entrega" />
          <CheckBox fx:id="cbDefeito" layoutX="212.0" layoutY="251.0" mnemonicParsing="false" onaction="#handleCheckBoxDefeito" text="Produto com defeito" />
          <CheckBox fx:id="cberro" layoutX="57.0" layoutY="283.0" mnemonicParsing="false" onaction="#handleCheckBoxErro" text="Erro na entrega" />
          <CheckBox fx:id="cbOutroN" layoutX="212.0" layoutY="283.0" mnemonicParsing="false" onaction="#handleCheckBoxOutroN" text="Outro" />
          <DatePicker fx:id="dpDataCompra" layoutX="230.0" layoutY="138.0" onaction="#handleDatePickerDataCompra" prefHeight="26.0" prefWidth="191.0" />
          <DatePicker fx:id="dpDataReclam" layoutX="231.0" layoutY="173.0" onaction="#handleDatePickerDataReclam" prefHeight="26.0" prefWidth="191.0" />
          <CheckBox fx:id="cbFerimentos" layoutX="57.0" layoutY="378.0" mnemonicParsing="false" onaction="#handleCheckBoxFerimentos" text="Ferimentos" />
          <CheckBox fx:id="cbAcidentes" layoutX="178.0" layoutY="378.0" mnemonicParsing="false" onaction="#handleCheckBoxAcidentes" text="Acidentes" />
          <CheckBox fx:id="cbDestruicao" layoutX="278.0" layoutY="378.0" mnemonicParsing="false" onaction="#handleCheckBoxDestruicao" text="Destruição" />
          <CheckBox fx:id="cbFinalidade" layoutX="57.0" layoutY="409.0" mnemonicParsing="false" onaction="#handleCheckBoxFinalidade" text="Finalidade não atingida" />
          <CheckBox fx:id="cbOutroE" layoutX="278.0" layoutY="409.0" mnemonicParsing="false" onaction="#handleCheckBoxOutroE" text="Outro" />
          <TextField fx:id="tfOutroN" disable="${!cbOutroN.selected}" layoutX="57.0" layoutY="312.0" onaction="#handleTextFieldOutroNatureza" prefHeight="28.0" prefWidth="367.0" />
          <TextField fx:id="tfOutroE" disable="${!cbOutroE.selected}" layoutX="57.0" layoutY="441.0" onaction="#handleTextFieldOutroEfeito" prefHeight="28.0" prefWidth="367.0" />
          <Label layoutX="57.0" layoutY="109.0" text="Produto:">
             <font>
                <Font size="16.0" />
             </font>
          </Label>
          <ComboBox fx:id="cbxProduto" layoutX="137.0" layoutY="106.0" onaction="#handleComboBoxProduto" prefHeight="26.0" prefWidth="284.0" />
          <Button fx:id="bEnviar" layoutX="57.0" layoutY="494.0" mnemonicParsing="false" onaction="#handleButtonEnviar" prefHeight="26.0" prefWidth="185.0" text="Enviar" />
          <Button fx:id="bVoltar" layoutX="247.0" layoutY="494.0" mnemonicParsing="false" onaction="#handleButtonVoltar" prefHeight="26.0" prefWidth="177.0" text="Voltar" />
       </children>
    </AnchorPane>
</BorderPane>

AcmeReclamController.java

/*
 * To change this license header,choose License Headers in Project Properties.
 * To change this template file,choose Tools | Templates
 * and open the template in the editor.
 */
package acmetelas;

import acmebank.ProdutoData;
import acmebank.ReclamacaoData;
import ihcacme.Produto;
import ihcacme.Reclamacao;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.actionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import java.sql.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.ObservableList;

/**
 *
 * @author joao
 */
public class AcmeReclamController implements Initializable{

    @FXML
    private AnchorPane rootPane;

    @FXML
    private BorderPane realPane;

    @FXML
    private TextField tfEmail;

    @FXML
    private TextField tfTelefone;

    @FXML
    private ComboBox cbxProduto;

    @FXML
    private DatePicker dpDataCompra;

    @FXML
    private DatePicker dpDataReclam;


    @FXML
    private CheckBox cbFerimentos;

    @FXML
    private CheckBox cbAcidentes;

    @FXML
    private CheckBox cbDestruicao;

    @FXML
    private CheckBox cbFinalidade;

    @FXML
    private CheckBox cbOutroE;

    ////////////////////////////

    @FXML
    private CheckBox cbAtraso;

    @FXML
    private CheckBox cbDefeito;

    @FXML
    private CheckBox cberro;

    @FXML
    private CheckBox cbOutroN;

    @FXML
    private TextField tfOutroN;

    ////////////////////////////

    @FXML
    private TextField tfOutroE;

    @FXML
    private Button bEnviar;

    @FXML
    private Button bVoltar;

    private ReclamacaoData reclamacaoData;
    private Reclamacao reclamacao;
    private List<Reclamacao> reclamacaoList;

    private Produto produto;

    public List<Produto> listaProduto;
    public ProdutoData produtoData;

    public List<Produto> readListaProduto() throws SQLException,ClassnotFoundException{

        produtoData = new ProdutoData("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/acme","root","root123!");

        listaProduto = produtoData.getProdutoList();

        produtoData.shutdown();

        return listaProduto;
    }

    public int readCheckBoxProblema(){

        if(!cbAtraso.isDisabled()){

            return 1;

        }

        if(!cberro.isDisabled()){

            return 3;

        }

        if(!cbDefeito.isDisabled()){

            return 2;

        }

        if(!cbOutroN.isDisabled()){

            return 4;

        }

        return 0;
    }

    public int readCheckBoxEfeitos(){

        if(!cbFerimentos.isDisabled()){

            return 1;

        }

        if(!cbAcidentes.isDisabled()){

            return 2;
        }

        if(!cbDestruicao.isDisabled()){

            return 3;

        }

        if(!cbFinalidade.isDisabled()){

            return 4;

        }

         if(!cbOutroE.isDisabled()){

            return 5;

        }

        return 0;
    }

    public Reclamacao registerReclam() throws SQLException,ClassnotFoundException{

        reclamacao = null;        
        reclamacaoData = new ReclamacaoData("com.mysql.jdbc.Driver","root123!");

        String email = tfEmail.getText();
        String telefone = tfTelefone.getText();

        String problema = tfOutroN.getText();
        String efeitos = tfOutroE.getText();

        LocalDate localdate = dpDataCompra.getvalue();
        Instant instant = Instant.from(localdate.atStartOfDay(ZoneId.systemDefault()));
        Date data_compra = (Date) Date.from(instant);

        localdate = dpDataReclam.getvalue();
        instant = Instant.from(localdate.atStartOfDay(ZoneId.systemDefault()));
        Date data_reclam = (Date) Date.from(instant);

        produto = (Produto) cbxProduto.getButtonCell().getItem();

        String idProduto = produto.getIdProduto();

        int problema_cat = readCheckBoxProblema();
        int efeitos_cat = readCheckBoxEfeitos();

        reclamacao = reclamacaoData.createReclamacao(email,problema,efeitos,problema_cat,telefone,data_compra,data_reclam,efeitos_cat,idProduto);

        return reclamacao;

    }


    public void handleButtonEnviar(actionEvent e) throws IOException,SQLException,ClassnotFoundException{  

       try{           

            if(registerReclam() != null)
            {
                try{

                    BorderPane pane = FXMLLoader.load(getclass().getResource("AcmeSolucao.fxml"));
                    realPane.getchildren().setall(pane);

                }catch(IOException ex){

                    System.out.println("IO - Erro ao migrar para a tela AcmeReclam.fxml.");
                    System.out.println(ex);
                    System.out.println(ex.getcause());

                }

            }

       }catch(SQLException sql){

           System.out.println("Erro ao conectar ao banco de dados.");
                    System.out.println(sql);
                    System.out.println(sql.getcause());

       }catch(ClassnotFoundException ce){

           System.out.println("Erro ao encontrar driver.");
                    System.out.println(ce);
                    System.out.println(ce.getcause());
       }

    }

    public void handleComboBoxProduto(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleTextFieldEmail(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleTextFieldTelefone(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleDatePickerDataCompra(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleDatePickerDataReclam(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleButtonVoltar(actionEvent e) throws IOException{

        BorderPane pane = FXMLLoader.load(getclass().getResource("AcmeInitial.fxml"));
        realPane.getchildren().setall(pane);

    }

    public void handleCheckBoxAtraso(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxDefeito(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxErro(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxOutroN(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxOutroE(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxFerimentos(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxAcidentes(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxDestruicao(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleCheckBoxFinalidade(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleTextFieldOutroEfeito(actionEvent e) throws IOException{

       // TO-DO

    }

    public void handleTextFieldOutroNatureza(actionEvent e) throws IOException{

       // TO-DO

    }

    @Override
    public void initialize(URL url,ResourceBundle rb) {

        try {

            List<Produto> lp = readListaProduto();
            cbxProduto.setItems((ObservableList) lp);


        } catch (SQLException ex) {

            System.out.println("Falha ao conectar ao banco de dados.");

        } catch (ClassnotFoundException ex) {

            System.out.println("Falha ao encontrar a classe do driver.");

        }


    }  

}

它保持与窗口AcmeInitial相同的大小。我该如何解决?

TOTTI128 回答:使用JavaFX FXML调整新窗口大小的问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3001417.html

大家都在问