Mein Ziel war es eine einfache JavaFx Applikation zu erstellen. Dafür habe ich dann einen einfachen BMI Rechner programmiert und dann halt auch den SceneBuilder für die Applikation benutzt. Jetzt habe ich das Problem, dass ich um das Ergebnis auszugeben einen leeren Label benutze, welcher dann halt aber auch nicht so schön etc aussieht. Ich habe mir gedacht, dass man wie bei Swing einfach nen JOptionPane.showMessageDialog(); schreiben könnte, jedoch finde ich nichts in der Art. Gäbe es denn eine andere Lösung etwas "aufploppen zu lassen" ?
[CODE lang="java" title="HelloApplication"]package com.example.bmi_rechner;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 600, 400);
stage.setTitle("Body Mass Index Rechner");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}[/CODE]
[CODE lang="xml" title="HelloController"]package com.example.bmi_rechner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class HelloController {
@FXML
private Button berechneButton;
@FXML
private TextField gewichtFeld;
@FXML
private TextField groessenFeld;
@FXML
private CheckBox maennlichCheckbox;
@FXML
private Label secretLabel;
@FXML
private CheckBox weiblichCheckbox;
@FXML
void berechneButtonClicked(ActionEvent event) {
var result = Double.parseDouble(gewichtFeld.getText()) / ((int)Math.pow(Double.parseDouble(groessenFeld.getText()),2));
if (weiblichCheckbox.isSelected()) {
if (result < 19) secretLabel.setText("Untergewicht");
if (result >= 19 && result <= 24) secretLabel.setText("Normalgewicht");
if (result >= 25 && result <= 30) secretLabel.setText("Uebergewicht");
if (result >= 31 && result <= 40) secretLabel.setText("Adipositas");
if (result > 40) System.out.println("starkes Adipositas");
} else if (maennlichCheckbox.isSelected()) {
if (result < 20) secretLabel.setText("Untergewicht");
if (result >= 20 && result <= 25) secretLabel.setText("Normalgewicht");
if (result >= 26 && result <= 30) secretLabel.setText("Uebergewicht");
if (result >= 31 && result <= 40) secretLabel.setText("Adipositas");
if (result > 40) secretLabel.setText("starkes Adipositas");
}
}
}
[/CODE]
[CODE lang="java" title="HelloApplication"]package com.example.bmi_rechner;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 600, 400);
stage.setTitle("Body Mass Index Rechner");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}[/CODE]
[CODE lang="xml" title="HelloController"]package com.example.bmi_rechner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class HelloController {
@FXML
private Button berechneButton;
@FXML
private TextField gewichtFeld;
@FXML
private TextField groessenFeld;
@FXML
private CheckBox maennlichCheckbox;
@FXML
private Label secretLabel;
@FXML
private CheckBox weiblichCheckbox;
@FXML
void berechneButtonClicked(ActionEvent event) {
var result = Double.parseDouble(gewichtFeld.getText()) / ((int)Math.pow(Double.parseDouble(groessenFeld.getText()),2));
if (weiblichCheckbox.isSelected()) {
if (result < 19) secretLabel.setText("Untergewicht");
if (result >= 19 && result <= 24) secretLabel.setText("Normalgewicht");
if (result >= 25 && result <= 30) secretLabel.setText("Uebergewicht");
if (result >= 31 && result <= 40) secretLabel.setText("Adipositas");
if (result > 40) System.out.println("starkes Adipositas");
} else if (maennlichCheckbox.isSelected()) {
if (result < 20) secretLabel.setText("Untergewicht");
if (result >= 20 && result <= 25) secretLabel.setText("Normalgewicht");
if (result >= 26 && result <= 30) secretLabel.setText("Uebergewicht");
if (result >= 31 && result <= 40) secretLabel.setText("Adipositas");
if (result > 40) secretLabel.setText("starkes Adipositas");
}
}
}
[/CODE]
Java:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.bmi_rechner.HelloController">
<children>
<Label layoutX="179.0" layoutY="20.0" prefHeight="86.0" prefWidth="243.0" text=" Body Mass Index Rechner">
<font>
<Font size="19.0" />
</font>
</Label>
<Button fx:id="berechneButton" layoutX="244.0" layoutY="316.0" mnemonicParsing="false" onAction="#berechneButtonClicked" text="Berechne den BMI" />
<TextField fx:id="groessenFeld" layoutX="105.0" layoutY="163.0" />
<TextField fx:id="gewichtFeld" layoutX="348.0" layoutY="163.0" />
<Label layoutX="110.0" layoutY="121.0" prefHeight="25.0" prefWidth="134.0" text="Größe in X.XX">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="348.0" layoutY="121.0" prefHeight="27.0" prefWidth="149.0" text="Gewicht in XX.XX">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="261.0" layoutY="167.0" text="cm" />
<Label layoutX="504.0" layoutY="167.0" text="kg" />
<Label fx:id="secretLabel" layoutX="285.0" layoutY="270.0" text=" " />
<Label layoutX="163.0" layoutY="253.0" text="Männlich" />
<Label layoutX="348.0" layoutY="253.0" text="Weiblich" />
<CheckBox fx:id="maennlichCheckbox" layoutX="225.0" layoutY="254.0" mnemonicParsing="false" />
<CheckBox fx:id="weiblichCheckbox" layoutX="405.0" layoutY="253.0" mnemonicParsing="false" />
</children>
</AnchorPane>