vendredi 23 décembre 2016

Java - Why does it not show an error dialog when the user gives in an unwanted input?

I am trying to color some ovals. The user types in for example color red (rood), and then the circles will be drawn and colored red, that works, but now I am trying to make it give an pop-up if the colors don't match the ones that are the user supposed to give or if the user gives in an number for example, there are three colors which the user can fill in those are rood (red), groen (green), and oranje (orange).

Someone told me I should do it with a try and catch method, which I tried below, when the user gives in for example rood (red) the circles do still work and will be drawn red, but when he gives in for example blue, nothing happens; I want it then to give an error message saying the user needs to give in the three listed colors, and not something else.

this.kleur = this.jTextFieldKleur.getText();
repaint();

if (kleur != null) {
            try {
                if (kleur.equals("rood")) {
                    g.setColor(Color.RED);
                    g.fillOval(75, 60, 35, 35); //links boven
                    g.fillOval(130, 60, 35, 35); //rechts boven
                    g.fillOval(75, 130, 35, 35); //links onder
                    g.fillOval(130, 130, 35, 35); //links onder
                } else if (kleur.equals("groen")) {
                    g.setColor(Color.GREEN);
                    g.fillOval(75, 60, 35, 35); //links boven
                    g.fillOval(130, 60, 35, 35); //rechts boven
                    g.fillOval(75, 130, 35, 35); //links onder
                    g.fillOval(130, 130, 35, 35); //links onder
                } else if (kleur.equals("oranje")) {
                    g.setColor(Color.ORANGE);
                    g.fillOval(75, 60, 35, 35); //links boven
                    g.fillOval(130, 60, 35, 35); //rechts boven
                    g.fillOval(75, 130, 35, 35); //links onder
                    g.fillOval(130, 130, 35, 35); //links onder
                }
            } catch (Exception e) {
                {
                    JOptionPane.showMessageDialog(null, "Gelieve te kiezen uit rood, oranje of groen.");
                }
            }
        }

Thanks for your time/effort!

Aucun commentaire:

Enregistrer un commentaire