Search code examples
javaswingjlabel

JLabel Is not showing up when its supposed to


I have a rough idea why the JLabel is not showing up. I cannot figure out how to make it show up, however. Where it says add(tf1); it's not opening in the JFrame.

(tf1 & 2 are labels & not a text field). The code is a clicks per second. Just something simple I'm working on. (this class is one of many I have, if you need those too just comment.)

class CPS extends JFrame implements ActionListener{
     JPanel panel;
    JButton button;
    JLabel tf1, tf2;
    boolean running;
    int totalClicks = 0;
    int clicksLeft = 100-totalClicks;
      static float ars;;
    int startInt = 0;
    CPS(){
         panel=new JPanel();
          JFrame d= new JFrame("Clicks Per Second");  
          button = new JButton("Click here for 5 Seconds");

          add(button);
         button.setBounds(0,0,500,400);
           button.addActionListener(this);
           setLayout(null);
           setSize(500, 500);
           setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent a) {
         if(a.getSource()==button){  
             totalClicks++;
             
             showClicks();
            
             revalidate();    
             startInt++;
             if(startInt == 1) {
                 timer();
             }
            }
    }

    public void timer() {
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

        final Runnable runnable = new Runnable() {
        int countdownStarter = 5;
final double divInt = countdownStarter;
            public void run() {

//                System.out.println(countdownStarter);

                countdownStarter--;

                if (countdownStarter < 0) {
                    System.out.println("Times Up!");
                    scheduler.shutdown();
                    final float finalClicks = (float) (totalClicks/divInt);
                    System.out.println("Your clicks per second: " + finalClicks);
                    ars = finalClicks;
                    
                    button.setEnabled(false);
                    
                    tf1 = new JLabel("Your Total Clicks: " + totalClicks);
                  tf1.setBounds(50,410, 200, 20);
                
                  tf2 = new JLabel("Average Clicks/Second: "+ totalClicks/5);
                  tf2.setBounds(50, 440, 200, 20);
                  
                  add(tf1);
                 add(tf2);
                 revalidate();
                }
            }
        };
        scheduler.scheduleAtFixedRate(runnable, 0, 1, SECONDS);
    }
    
    public void showClicks() {
        System.out.println(totalClicks);
    }
}

Solution

  • The code is missing

    String score = String.valueOf("Total Clicks: " + totalClicks);
    tf1.setText(score);
    

    And

    String avg = String.valueOf("Average: " +finalClicks);
    tf2.setText(avg);
    

    The final code should be:

    class CPS extends JFrame implements ActionListener{
    JPanel panel;
    JButton button;
    JLabel tf1, tf2;
    boolean running;
    int totalClicks = 0;
    int clicksLeft = 100-totalClicks;
     static float ars;;
    int startInt = 0;
    CPS(){
    panel=new JPanel();
     JFrame d= new JFrame("Clicks Per Second");  
     button = new JButton("Click here for 5 Seconds");
    
     
     
    
    
     
     tf1 = new JLabel("");
     tf2 = new JLabel("");
    
       
       add(tf1);
      add(tf2);
    
     add(button);
     
     tf1.setBounds(50,400, 200, 20);
       
             
       tf2.setBounds(50, 440, 200, 20);
       
       
    button.setBounds(0,0,500,400);
      button.addActionListener(this);
      setLayout(null);
      setSize(500, 500);
      setVisible(true);
     
     
     
    
    }
    
    
    @Override
    public void actionPerformed(ActionEvent a) {
    if(a.getSource()==button){  
            totalClicks++;
           
            showClicks();
           
            revalidate();  
            startInt++;
            if(startInt == 1) {
            timer();
            }
           
           }
    
    }
    public void timer() {
    
            final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    
            final Runnable runnable = new Runnable() {
            int countdownStarter = 5;
    final double divInt = countdownStarter;
                public void run() {
    
    //                System.out.println(countdownStarter);
    
                   
                   
                    countdownStarter--;
    
                 
                   
                    if (countdownStarter < 0) {
                        System.out.println("Times Up!");
                        scheduler.shutdown();
                        final float finalClicks = (float) (totalClicks/divInt);
                        System.out.println("Your clicks per second: " + finalClicks);
                        ars = finalClicks;
                       
                        button.setEnabled(false);
                       
                       
             
             
             
               String score = String.valueOf("Total Clicks: " + totalClicks);
              tf1.setText(score);
             
              String avg = String.valueOf("Average: " +finalClicks);
            tf2.setText(avg);
    
             
               invalidate();
               validate();
               repaint();
                    }
                }
            };
           
           
            scheduler.scheduleAtFixedRate(runnable, 0, 1, SECONDS);
        }
    
    public void showClicks() {
    
    System.out.println(totalClicks);
    
    }
    
    }