JLabel 没有在应该出现的时候出现

JLabel Is not showing up when its supposed to

我大概知道为什么 JLabel 没有出现。但是我不知道如何让它显示出来。它说“添加(tf1);它没有在 JFrame 中打开。提前致谢。(tf1 和 2 是标签而不是文本字段)。代码是每秒点击一次。我正在做一些简单的事情。(这个 class 是我拥有的其中之一,如果您也需要这些,请发表评论。谢谢)

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);
            
    }
    
}

代码丢失

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

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

最终代码应该是:

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);

}

}