dh-Materialien
Java Projekte
// JavaProject InputDialog
InputDialog.java

// InputDialog.java

import java.awt.*;
import javax.swing.*;


public class InputDialog {

    Image icon; // Variablen
    JFrame jfr;

    int num = 0;
    float z = 0f; // Literale

    InputDialog() {
        jfr = new JFrame("Eingabedialog");
        icon = Toolkit.getDefaultToolkit().getImage("dh.png");
        jfr.setIconImage(icon);
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfr.setSize(349, 301);
        jfr.setResizable(false);
        jfr.setLocationRelativeTo(null);

        jfr.setVisible(true);
    }

    public String ask(String txt) { // Methoden
        return JOptionPane.showInputDialog (
            jfr,
            txt
        );
    }

    public void answer(String txt) {
        JOptionPane.showMessageDialog (
            jfr,
            txt,
            "Eingabefehler",
            JOptionPane.ERROR_MESSAGE
        );
    }

    public void show() {

        String input;

        while (true) { // Kontrollstrukturen
            try {
                input = ask("Eine ganze Zahl bitte:");
                num = Integer.parseInt(input); // Wrapper-Klassen
                break;
            }
            catch (NumberFormatException e) {
                answer("Dies ist keine ganze Zahl!");
            }
        }

        while (true) {
            try {
                input = ask("Eine Zahl vom Typ \"float\" bitte:");
                boolean b = input.endsWith("f")||input.endsWith("F");
                if (!b) input = "x";
                z = Float.parseFloat(input);
                break;
            }
            catch (NumberFormatException e) {
                answer("Die Eingabe war nicht korrekt!");
            }
        }

        System.exit(0);
    }

    public static void main(String[] args) {
        InputDialog dialog = new InputDialog();
        dialog.show();
    }
}

Download InputDialog