Error in logic control TV - Java

4

I'm a beginner in java and I'm having a hard time making the btnVolumeUp button increase the counter.

First I have this TV class in which I created the methods.

public class Televisor {
int canal = 0; 
int volume = 0;
boolean ligar = false;

public int diminuiVolume(){ //Diminui o volume da TV      
    //Se o volume estiver no 0 Exibe msg e não diminui mais
    if (volume == 0){
        JOptionPane.showMessageDialog(null, "O volume está no minimo");
    }
    else{
        volume--;
    }
    return this.volume;
}

public int aumentaVolume(){ //Aumenta o volume da TV       
    //Se o volume estiver no 10 Exibe msg e não aumenta mais
    if (volume == 10){
        JOptionPane.showMessageDialog(null, "O volume está no máximo");
    }
    else{
        volume++;
    }    
    return this.volume;
}

And then I have a jFrame with the volume buttons and a Label to display the current volume. But the button only works the first time. Example: When you click on the btnVolumeUp the counter goes from 0 to 1, but after that nothing else happens.

 private void btnVolumeUpActionPerformed(java.awt.event.ActionEvent evt) {                                            
          Televisor controle = new Televisor();          
          String exibeVolume;          
          exibeVolume = String.valueOf(controle.aumentaVolume());//Converte valor Int para String          
          lblVolume.setText(exibeVolume);//Altera o volume exibido              
    }                                           

    private void btnVolumeDownActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Televisor controle = new Televisor();          
          String exibeVolume;          
          //controle.aumentaVolume();          
          exibeVolume = String.valueOf(controle.diminuiVolume());//Converte valor Int para String
          lblVolume.setText(exibeVolume);//Altera o volume exibido
    }  

Is this my logic wrong?

    
asked by anonymous 20.04.2015 / 20:57

1 answer

3

Solved with help from @ramaral.

Now I am instantiating Televisor controle = new Televisor(); out of the buttons, so it is not initialized every time I click the buttons, avoiding returning the volume value to 0.

In this way:

 Televisor controle = new Televisor();
 private void btnVolumeUpActionPerformed(java.awt.event.ActionEvent evt) {                                            
          String exibeVolume;          
          exibeVolume = String.valueOf(controle.aumentaVolume());//Converte valor Int para String          
          lblVolume.setText(exibeVolume);//Altera o volume exibido              
    }                                           

    private void btnVolumeDownActionPerformed(java.awt.event.ActionEvent evt) {            
          String exibeVolume;          
          //controle.aumentaVolume();          
          exibeVolume = String.valueOf(controle.diminuiVolume());//Converte valor Int para String
          lblVolume.setText(exibeVolume);//Altera o volume exibido
    } 
    
20.04.2015 / 21:33