Initial Commit
This commit is contained in:
commit
cfc00f77ec
43
src/application/Main.java
Normal file
43
src/application/Main.java
Normal file
@ -0,0 +1,43 @@
|
||||
package application;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.ButtonType;
|
||||
|
||||
public class Main extends Application {
|
||||
public void start(Stage primaryStage) {
|
||||
try {
|
||||
RootBorderPane root = new RootBorderPane();
|
||||
Scene scene = new Scene(root, 700, 500);
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.setTitle("GUI-Uebung 4");
|
||||
primaryStage.show();
|
||||
} catch (Exception e) {
|
||||
showAlert(AlertType.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void showAlert(AlertType alertType, String message) {
|
||||
if (message != null) {
|
||||
Alert alert = new Alert(alertType, message, ButtonType.OK);
|
||||
alert.setHeaderText(null);
|
||||
if (alertType == AlertType.WARNING)
|
||||
alert.setTitle("Warnung");
|
||||
else
|
||||
alert.setTitle("Hinweis-Meldung");
|
||||
alert.showAndWait();
|
||||
} else {
|
||||
Alert alert = new Alert(alertType, "Bitte waehlen Sie \"Weiter\" aus", ButtonType.NEXT);
|
||||
alert.setHeaderText(null);
|
||||
alert.setTitle("Hinweis-Meldung");
|
||||
alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
163
src/application/RootBorderPane.java
Normal file
163
src/application/RootBorderPane.java
Normal file
@ -0,0 +1,163 @@
|
||||
package application;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.geometry.HPos;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.geometry.VPos;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.Menu;
|
||||
import javafx.scene.control.MenuBar;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.control.RadioButton;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.control.ToggleGroup;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
|
||||
public class RootBorderPane extends BorderPane {
|
||||
|
||||
private MenuBar menuBar;
|
||||
private Menu mDatei, mBearbeiten;
|
||||
private MenuItem miBeenden, miReset;
|
||||
|
||||
private GridPane gpZentrum;
|
||||
|
||||
private RadioButton rbDirekt, rbLeerzeichen, rbLeerzeile;
|
||||
|
||||
private ToggleGroup tgRadios;
|
||||
|
||||
private TextField tfText1, tfText2;
|
||||
private TextArea taErgebnis;
|
||||
|
||||
private Button btVerbinden;
|
||||
|
||||
private Label lbErgebnis;
|
||||
|
||||
public RootBorderPane() {
|
||||
initComponents();
|
||||
addComponents();
|
||||
addHandler();
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
String promptText = "Bitte einen Text eingeben...";
|
||||
|
||||
menuBar = new MenuBar();
|
||||
mDatei = new Menu("Datei");
|
||||
mBearbeiten = new Menu("Bearbeiten");
|
||||
miBeenden = new MenuItem("Beenden");
|
||||
miReset = new MenuItem("Reset");
|
||||
|
||||
gpZentrum = new GridPane();
|
||||
gpZentrum.setAlignment(Pos.CENTER);
|
||||
gpZentrum.setHgap(10);
|
||||
gpZentrum.setVgap(10);
|
||||
gpZentrum.setPadding(new Insets(10));
|
||||
|
||||
tfText1 = new TextField();
|
||||
tfText1.setPromptText(promptText);
|
||||
|
||||
tfText2 = new TextField();
|
||||
tfText2.setPromptText(promptText);
|
||||
|
||||
taErgebnis = new TextArea();
|
||||
taErgebnis.setDisable(true);
|
||||
taErgebnis.setEditable(false);
|
||||
|
||||
rbDirekt = new RadioButton("Direkt: ");
|
||||
rbLeerzeichen = new RadioButton("Leerzeichen: ");
|
||||
rbLeerzeile = new RadioButton("Leerzeile: ");
|
||||
|
||||
btVerbinden = new Button("Verbinden");
|
||||
|
||||
tgRadios = new ToggleGroup();
|
||||
|
||||
lbErgebnis = new Label("Ergebnis");
|
||||
}
|
||||
|
||||
private void addComponents() {
|
||||
mDatei.getItems().addAll(miBeenden);
|
||||
mBearbeiten.getItems().addAll(miReset);
|
||||
menuBar.getMenus().addAll(mDatei, mBearbeiten);
|
||||
|
||||
gpZentrum.add(new Label("Verbinden"), 0, 0);
|
||||
gpZentrum.add(new Label("Text 1"), 0, 3);
|
||||
gpZentrum.add(new Label("Text 2"), 0, 4);
|
||||
// gpZentrum.add(new Label("Ergebnis:"), 0, 5);
|
||||
gpZentrum.add(lbErgebnis, 0, 5);
|
||||
GridPane.setValignment(lbErgebnis, VPos.TOP);
|
||||
|
||||
gpZentrum.add(rbDirekt, 1, 0);
|
||||
gpZentrum.add(rbLeerzeichen, 1, 1);
|
||||
gpZentrum.add(rbLeerzeile, 1, 2);
|
||||
|
||||
gpZentrum.add(tfText1, 1, 3);
|
||||
gpZentrum.add(tfText2, 1, 4);
|
||||
gpZentrum.add(taErgebnis, 1, 5);
|
||||
|
||||
gpZentrum.add(btVerbinden, 1, 6);
|
||||
|
||||
GridPane.setHalignment(btVerbinden, HPos.RIGHT);
|
||||
|
||||
tgRadios.getToggles().addAll(rbDirekt, rbLeerzeichen, rbLeerzeile);
|
||||
tgRadios.selectToggle(rbDirekt);
|
||||
|
||||
setTop(menuBar);
|
||||
setCenter(gpZentrum);
|
||||
}
|
||||
|
||||
private void addHandler() {
|
||||
miBeenden.setOnAction(event -> { Platform.exit(); } );
|
||||
btVerbinden.setOnAction(event -> verbinden());
|
||||
miReset.setOnAction(event -> reset());
|
||||
}
|
||||
|
||||
// -------------------------- handler -------------------------
|
||||
|
||||
private void verbinden() {
|
||||
String text1 = tfText1.getText();
|
||||
String text2 = tfText2.getText();
|
||||
String verbinder = ""; // Direct
|
||||
|
||||
if (rbLeerzeichen.isSelected()) {
|
||||
verbinder = " "; // spacebar
|
||||
}
|
||||
|
||||
if (rbLeerzeile.isSelected()) {
|
||||
verbinder = "\n"; // NewLine
|
||||
}
|
||||
|
||||
if (!text1.isEmpty() && !text2.isEmpty()) {
|
||||
String ergebnis = text1 + verbinder + text2;
|
||||
|
||||
taErgebnis.setText(ergebnis);
|
||||
taErgebnis.setDisable(false);
|
||||
} else {
|
||||
if (text1.isEmpty() && text2.isEmpty()) {
|
||||
Main.showAlert(AlertType.ERROR, "Text1 fehlt\nText2 fehlt");
|
||||
} else {
|
||||
if (text1.isEmpty()) {
|
||||
Main.showAlert(AlertType.ERROR, "Text1 fehlt");
|
||||
} else {
|
||||
Main.showAlert(AlertType.ERROR, "Text2 fehlt");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void reset() {
|
||||
tgRadios.selectToggle(rbDirekt);
|
||||
|
||||
tfText1.clear();
|
||||
tfText2.clear();
|
||||
|
||||
taErgebnis.clear();
|
||||
taErgebnis.setDisable(true);
|
||||
}
|
||||
|
||||
}
|
12
src/model/TextException.java
Normal file
12
src/model/TextException.java
Normal file
@ -0,0 +1,12 @@
|
||||
package model;
|
||||
|
||||
public class TextException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public TextException (String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
60
src/model/Textverbinder.java
Normal file
60
src/model/Textverbinder.java
Normal file
@ -0,0 +1,60 @@
|
||||
package model;
|
||||
|
||||
public class Textverbinder {
|
||||
|
||||
public static String texteVerbindenDirekt(String text1, String text2) throws TextException
|
||||
{
|
||||
if (text1 != null && text2 != null)
|
||||
return texteVerbinden(text1, text2, "");
|
||||
else
|
||||
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
|
||||
}
|
||||
|
||||
public static String texteVerbindenLeerZeichen(String text1, String text2) throws TextException
|
||||
{
|
||||
if (text1 != null && text2 != null)
|
||||
return texteVerbinden(text1, text2, " ");
|
||||
else
|
||||
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
|
||||
}
|
||||
|
||||
public static String texteVerbindenLeerZeile(String text1, String text2) throws TextException
|
||||
{
|
||||
if (text1 != null && text2 != null)
|
||||
return texteVerbinden(text1, text2, "\n");
|
||||
else
|
||||
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
|
||||
}
|
||||
|
||||
private static String texteVerbinden(String text1, String text2, String verbinder) throws TextException
|
||||
{
|
||||
if (text1 != null && text2 != null && verbinder != null)
|
||||
if (checkTextEmpty(text1) && checkTextEmpty(text2))
|
||||
if (checkVerbinder(verbinder))
|
||||
{
|
||||
StringBuilder sb = new StringBuilder().append(text1).append(verbinder).append(text2);
|
||||
return sb.toString();
|
||||
}
|
||||
else
|
||||
throw new TextException("Fehler: verbinder ist ungueltig (verbinder: \"" + verbinder + "\")");
|
||||
else
|
||||
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
|
||||
else
|
||||
throw new TextException("Fehler: null-Referenz fuer text1, text2 oder verbinder erhalten (text1: \"" + text1 + " \", text2: \"" + text2 + "\", " + ", verbinder: \"" + verbinder);
|
||||
}
|
||||
|
||||
private static boolean checkTextEmpty(String text)
|
||||
{
|
||||
if (text != null && !text.isEmpty())
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean checkVerbinder(String text)
|
||||
{
|
||||
if (text != null && (text.equals("") || text.equals(" ") || text.equals("\n") ))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
40
src/test/TestTextverbinder.java
Normal file
40
src/test/TestTextverbinder.java
Normal file
@ -0,0 +1,40 @@
|
||||
package test;
|
||||
|
||||
import model.TextException;
|
||||
import model.Textverbinder;
|
||||
|
||||
public class TestTextverbinder {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
// System.out.println(Textverbinder.texteVerbindenDirekt(null, null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenDirekt("", null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenDirekt(" ", null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenDirekt(" ", "")); // Fehler
|
||||
// System.out.println("\"" + Textverbinder.texteVerbindenDirekt(" ", " ") + "\""); // " " (Anm.: 2x Leerzeichen)
|
||||
System.out.println("\"" + Textverbinder.texteVerbindenDirekt("Java", "FX") + "\""); // "JavaFX"
|
||||
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeichen(null, null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeichen("", null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeichen(" ", null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeichen(" ", "")); // Fehler
|
||||
// System.out.println("\"" + Textverbinder.texteVerbindenLeerZeichen(" ", " ") + "\""); // " " (Anm.: 3x Leerzeichen)
|
||||
System.out.println("\"" + Textverbinder.texteVerbindenLeerZeichen("kein", "Problem ") + "\""); // "kein Problem"
|
||||
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeile(null, null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeile("", null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeile(" ", null)); // Fehler
|
||||
// System.out.println(Textverbinder.texteVerbindenLeerZeile(" ", "")); // Fehler
|
||||
// System.out.println("\"" + Textverbinder.texteVerbindenLeerZeile(" ", " ") + "\""); // " " (Anm.: 1x Leerzeichen, 1x Leerzeile, 1x Leerzeichen)
|
||||
System.out.println("\"" + Textverbinder.texteVerbindenLeerZeile("heute", "morgen") + "\""); // "heute", dann Leerzeile, dann "morgen")
|
||||
}
|
||||
catch (TextException e)
|
||||
{
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user