Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package com.mycompany.kalkulator;
  6.  
  7. import java.awt.Color;
  8. import javax.swing.*;
  9. import org.apache.commons.lang3.math.NumberUtils;
  10.  
  11. /**
  12.  *
  13.  * @author Marcin
  14.  */
  15. public class SprawdzLiczbe extends InputVerifier {
  16.  
  17.     @Override
  18.     public boolean verify(JComponent input) {
  19.         String text = ((JTextField) input).getText();
  20.         if (NumberUtils.isNumber(text) || text.equals("")) { // czy podana wartosc to liczba lub puste pole
  21.             double liczba = NumberUtils.toDouble(text);
  22.             if (liczba >= 0 && liczba <= 2000) { // czy liczba miesci sie w zakresie
  23.                 input.setBackground(Color.WHITE); // kolor domyslny
  24.                 return true;
  25.             }
  26.             input.setBackground(Color.red); // kolor czerwony
  27.             return false;
  28.         }
  29.         input.setBackground(Color.red);
  30.         return false;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement