-2
 long totalMilliSeconds = System.currentTimeMillis();
    long totalSeconds = totalMilliSeconds / 1000;
    long currentSecond = totalSeconds % 60;
    long totalMinutes = totalSeconds / 60;
    long currentMinute = totalMinutes % 60;
    long totalHours = totalMinutes / 60;
    long currentHour = totalHours % 24;
    System.out.println("Current time is:" + currentHour + ":" + currentMinute + ":" + currentSecond);


    if(currentHour <= 9 && currentHour >=18)
        readFile.setForeground(YELLOW);
        readFile.setBackground(BLACK);
        graph.setForeground(YELLOW);
        graph.setBackground(BLACK);
        search.setForeground(YELLOW);
        search.setBackground(BLACK);
        snippet.setForeground(YELLOW);
        snippet.setBackground(BLACK);
        prediction.setForeground(YELLOW);
        prediction.setBackground(BLACK);
        export.setForeground(YELLOW);
        export.setBackground(BLACK);
        back.setForeground(YELLOW);
        back.setBackground(BLACK);

The program is supposed to change the colours of buttons from 6pm up until 9am but im not sure why its still changing before the set time. Any ideas would be appreciated.

1 Answers1

-1

I see two problems with your code:

  • The if-condition will never evaluate to true. For this, the value has to be less than or equal to 9 and at the same time greater than or equal to 18.
  • Missing curly braces for the whole (containing all the color changing statements) body of the if-statement lead to

    if(currentHour <= 9 && currentHour >=18) {
      readFile.setForeground(YELLOW);
    }
    readFile.setBackground(BLACK);
    graph.setForeground(YELLOW);
    graph.setBackground(BLACK);
    

So a lot of the colors are set independent of your if-condition.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33