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

时间:2019-11-30 19:48:41

标签: java javafx fxml netbeans-8

我无法自动将舞台大小调整为新窗口的大小。我在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", "jdbc:mysql://localhost:3306/acme", "root", "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相同的大小。我该如何解决?

0 个答案:

没有答案