从另一个包中获取价值

时间:2016-11-22 18:42:26

标签: java label fxml settext

我想从"应用程序"中获取价值打包并在"包"中使用它包,但我的方式不起作用。我希望lblCal在" packet"包装与loppkcal或lblKcal具有相同的价值在" application"包。 对不起,如果我的代码看起来像狗屎。

我还提到arvutaNuppVajutus是On Action按钮。

我在代码中添加了注释,我的意思是什么("应用程序"包的底部有注释)。

package packet;

import javafx.collections.FXCollections;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import application.MainControll;

public class Paevikcontroll {

@FXML
private HBox hbboxKast;

@FXML
private TextField lisaToit;

@FXML
private TextField lisaValk;

@FXML
private TextField lisaSysi;

@FXML
private TextField lisaRasv;

@FXML
private ComboBox cbToiduaine;

@FXML
private TextField txtKogus;

@FXML
private Label lblCal;

@FXML
private Label lblProt;

@FXML
private Label lblCarb;

@FXML
private Label lblFat;

private void initialize() {
    MainControll nr = new MainControll();
    lblCal.setText(nr.arvutaNuppVajutus.loppkcal); //Want same value for this as lblKcal or loppkcal has in "application" package.

}
}

这是"应用程序"包我想从中得到价值。

package application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class MainControll {
ObservableList<String> Sugu = FXCollections.observableArrayList("Mees", "Naine");
ObservableList<String> Aktiivsus = FXCollections.observableArrayList("Puudulik", "Madal", "Keskmine", "Kõrge");
ObservableList<String> Goal = FXCollections.observableArrayList("Kaalu langetamine", "Kaalu säilitamine",
        "Kaalu tõstmine");

@FXML
private TextField txtPikkus;

@FXML
private TextField txtKaal;

@FXML
private TextField txtVanus;

@FXML
private ComboBox cbAktiivsus;

@FXML
private ComboBox cbSugu;

@FXML
private ComboBox cbValik;

@FXML
public static Label lblKcal;


@FXML
public Label lblValk;

@FXML
public Label lblSysi;

@FXML
public Label lblRasv;

@FXML
private Label lblVigaPikk;

@FXML
private Label lblVigaKaal;

@FXML
private Label lblVigaVanus;

@FXML
private Label lblVigaAktiivsus;

@FXML
private Label lblVigaSugu;

@FXML
private Label lblVigaGoal;

@FXML
private void initialize() {
    cbSugu.setItems(Sugu);
    cbAktiivsus.setItems(Aktiivsus);
    cbValik.setItems(Goal);

}

public void arvutaNuppVajutus() {
    double pikk = 0;
    double kaal = 0;
    double vanus = 0;
    try {
        pikk = Double.parseDouble(txtPikkus.getText());
    } catch (NumberFormatException e) {
        lblVigaPikk.setText("Lisa pikkus numbrites!");
    }
    try {
        kaal = Double.parseDouble(txtKaal.getText());
    } catch (NumberFormatException e) {
        lblVigaKaal.setText("Lisa kaal numbrites!");
    }
    try {
        vanus = Double.parseDouble(txtVanus.getText());
    } catch (NumberFormatException e) {
        lblVigaVanus.setText("Lisa vanus numbrites!");
    }
    double esikcal = 0;
    double keskkcal = 0;
    int loppkcal = 0;
    int sysivesik;

    try {
        if (cbSugu.getSelectionModel().getSelectedItem().equals("Mees")) {
            esikcal = 10 * kaal + 6.25 * pikk - 5 * vanus + 5;

        }
        if (cbSugu.getSelectionModel().getSelectedItem().equals("Naine")) {
            esikcal = 10 * kaal + 6.25 * pikk - 5 * vanus - 161;

        }
    } catch (NullPointerException e) {

        lblVigaSugu.setText("Unustasid soo lisamata!");
    }

    try {

        while (cbAktiivsus.getSelectionModel().getSelectedItem().equals("Puudulik")) {
            keskkcal = 1.0 * esikcal;
            break;

        }
        while (cbAktiivsus.getSelectionModel().getSelectedItem().equals("Madal")) {
            keskkcal = 1.4 * esikcal;
            break;

        }
        while (cbAktiivsus.getSelectionModel().getSelectedItem().equals("Keskmine")) {
            keskkcal = 1.6 * esikcal;
            break;

        }
        while (cbAktiivsus.getSelectionModel().getSelectedItem().equals("Kõrge")) {
            keskkcal = 1.8 * esikcal;
            break;

        }

    } catch (NullPointerException e) {
        lblVigaAktiivsus.setText("Unustasid aktiivsuse valimata!");
    }

    try {
        if (cbValik.getSelectionModel().getSelectedItem().equals("Kaalu langetamine")) {
            loppkcal = (int) (keskkcal - (keskkcal * 0.1));
        }
        if (cbValik.getSelectionModel().getSelectedItem().equals("Kaalu säilitamine")) {
            loppkcal = (int) keskkcal;
        }
        if (cbValik.getSelectionModel().getSelectedItem().equals("Kaalu tõstmine")) {
            loppkcal = (int) (keskkcal + (keskkcal * 0.1));
        }
    } catch (NullPointerException e) {
        lblVigaGoal.setText("Vali eesmärk!");

    }
    lblKcal.setText((Double.toString(loppkcal)) + " kcal");  //Wanna do the same as i have done here, in the "packet" package to lblCal.
    lblValk.setText((Double.toString(kaal * 2)) + " g");
    lblRasv.setText((Double.toString(kaal)) + " g");
    sysivesik = (int) (loppkcal - (kaal * 8) - (kaal * 9)) / 4;
    lblSysi.setText((Double.toString(sysivesik)) + " g");
}

public void pressButton(ActionEvent event) throws Exception {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader();
        Parent root1 = (Parent) fxmlLoader.load(getClass().getResource("/packet/PaevikInterface.fxml"));
        Stage stage = new Stage();
        stage.setScene(new Scene(root1));
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
}

这里还有FXML代码,如果有任何帮助的话。

&#34;应用程序&#34;中的FXML代码包     

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

<AnchorPane prefHeight="600.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainControll">
   <children>
      <Button fx:id="btnArvuta" layoutX="305.0" layoutY="281.0" mnemonicParsing="false" onAction="#arvutaNuppVajutus" text="Arvuta" textFill="#0a0a0a">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
      <TextField fx:id="txtPikkus" layoutX="25.0" layoutY="36.0" promptText="Sisesta pikkus">
         <font>
            <Font size="18.0" />
         </font>
      </TextField>
      <TextField fx:id="txtKaal" layoutX="25.0" layoutY="92.0" promptText="Sisesta kaal">
         <font>
            <Font size="18.0" />
         </font>
      </TextField>
      <Label layoutX="35.0" layoutY="1.0" text="Arvuta päevane kaloraazh">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <ComboBox fx:id="cbValik" layoutX="23.0" layoutY="283.0" prefWidth="150.0" promptText="Eesmärk" />
      <Label layoutX="27.0" layoutY="325.0" text="Päevane soovituslik kcal ja makrotoitained on:">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label layoutX="27.0" layoutY="352.0" text="Kcal:">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label layoutX="27.0" layoutY="377.0" text="Valgud:">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label layoutX="27.0" layoutY="403.0" text="Süsivesikud:">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label layoutX="27.0" layoutY="427.0" text="Rasvad:">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label fx:id="lblKcal" layoutX="137.0" layoutY="355.0" text="0 kcal">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label fx:id="lblValk" layoutX="137.0" layoutY="377.0" text="0 g">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label fx:id="lblSysi" layoutX="137.0" layoutY="403.0" text="0 g">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label fx:id="lblRasv" layoutX="137.0" layoutY="427.0" text="0 g">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <ComboBox fx:id="cbAktiivsus" layoutX="23.0" layoutY="192.0" prefWidth="150.0" promptText="Vali aktiivsus" />
      <ComboBox fx:id="cbSugu" layoutX="23.0" layoutY="236.0" prefWidth="150.0" promptText="Sugu" />
      <Button fx:id="btnPaevik" layoutX="23.0" layoutY="489.0" mnemonicParsing="false" onAction="#pressButton" prefHeight="56.0" prefWidth="162.0" text="Toitumispäevik">
         <font>
            <Font size="20.0" />
         </font>
      </Button>
      <TextField fx:id="txtVanus" layoutX="27.0" layoutY="142.0" prefHeight="37.0" prefWidth="64.0" promptText="Vanus">
         <font>
            <Font size="16.0" />
         </font>
      </TextField>
      <Label fx:id="lblVigaPikk" layoutX="254.0" layoutY="37.0" prefHeight="37.0" prefWidth="142.0" text="-&#10;&#10;" textFill="RED" />
      <Label fx:id="lblVigaKaal" layoutX="254.0" layoutY="93.0" prefHeight="37.0" prefWidth="142.0" text="-&#10;&#10;" textFill="RED" />
      <Label fx:id="lblVigaVanus" layoutX="102.0" layoutY="142.0" prefHeight="37.0" prefWidth="217.0" text="-&#10;&#10;" textFill="RED" />
      <Label fx:id="lblVigaAktiivsus" layoutX="185.0" layoutY="186.0" prefHeight="37.0" prefWidth="205.0" text="-&#10;&#10;" textFill="RED" />
      <Label fx:id="lblVigaSugu" layoutX="183.0" layoutY="233.0" prefHeight="37.0" prefWidth="205.0" text="-&#10;&#10;" textFill="RED" />
      <Label fx:id="lblVigaGoal" layoutX="183.0" layoutY="277.0" prefHeight="37.0" prefWidth="120.0" text="-&#10;&#10;" textFill="RED" />
   </children>
</AnchorPane>

和&#34;数据包&#34;中的FXML代码封装

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox fx:id="hboxKAst" layoutX="38.0" layoutY="37.0" prefHeight="100.0" prefWidth="547.0">
         <children>
            <TextField fx:id="lisaToit" prefHeight="25.0" prefWidth="192.0" promptText="Toiduaine" />
            <TextField fx:id="lisaValk" prefHeight="25.0" prefWidth="56.0" promptText="Valgud" />
            <TextField fx:id="lisaSysi" prefHeight="25.0" prefWidth="90.0" promptText="Süsivesikud" />
            <TextField fx:id="lisaRasv" prefHeight="25.0" prefWidth="54.0" promptText="Rasvad" />
            <Button fx:id="lisaNupp" mnemonicParsing="false" prefHeight="25.0" prefWidth="131.0" text="Lisa andmebaasi" />
         </children>
      </HBox>
      <ComboBox fx:id="cbToiduaine" layoutX="38.0" layoutY="169.0" prefWidth="150.0" promptText="Vali toiduaine" />
      <TextField fx:id="txtKogus" layoutX="204.0" layoutY="169.0" prefHeight="25.0" prefWidth="112.0" promptText="Kogus grammides" />
      <TableView fx:id="tbTabel" layoutX="38.0" layoutY="227.0" prefHeight="333.0" prefWidth="555.0">
        <columns>
            <TableColumn fx:id="tbToit" prefWidth="188.0" text="Toiduaine nimetus" />
          <TableColumn fx:id="tbValk" prefWidth="85.0" text="Valgud" />
          <TableColumn fx:id="tbSysi" prefWidth="81.0" text="Süsivesikud" />
            <TableColumn fx:id="tbRasv" prefWidth="92.0" text="Rasvad" />
            <TableColumn fx:id="tbKcal" prefWidth="104.0" text="Kalorid" />
        </columns>
      </TableView>
      <Button fx:id="lisaMenyy" layoutX="339.0" layoutY="169.0" mnemonicParsing="false" text="Lisa menüüsse" />
      <Button layoutX="636.0" layoutY="37.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="150.0" text="Toiduainete andmebaas" />
      <Label layoutX="608.0" layoutY="238.0" prefHeight="45.0" prefWidth="176.0" text="Sinule vajalik kogus kaloreid&#10; ja makrotoitaineid:&#10;">
         <font>
            <Font size="15.0" />
         </font>
      </Label>
      <Label layoutX="608.0" layoutY="292.0" text="Kcal:">
         <font>
            <Font size="15.0" />
         </font>
      </Label>
      <Label layoutX="608.0" layoutY="324.0" text="Valgud:">
         <font>
            <Font size="15.0" />
         </font>
      </Label>
      <Label layoutX="608.0" layoutY="352.0" text="Süsivesikud:">
         <font>
            <Font size="15.0" />
         </font>
      </Label>
      <Label layoutX="608.0" layoutY="385.0" text="Rasvad:">
         <font>
            <Font size="15.0" />
         </font>
      </Label>
      <Label fx:id="lblCal" layoutX="658.0" layoutY="294.0" text="0 kcal" />
      <Label fx:id="lblProt" layoutX="672.0" layoutY="326.0" text="0 g" />
      <Label fx:id="lblCarb" layoutX="697.0" layoutY="354.0" text="0 g" />
      <Label fx:id="lblFat" layoutX="672.0" layoutY="387.0" text="0 g" />
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:1)

你可以改变这个:

lblCal.setText(nr.arvutaNuppVajutus.loppkcal); //Want same value for this as lblKcal or loppkcal has in "application" package.

lblCal.setText(nr.arvutaNuppVajutu());

并将arvutaNuppVajutu()更改为

public string arvutaNuppVajutu() {
  ..

  return "" + lblKcal;
}