Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

java - How can i create a counter in JFrame?

How can I create a counter and implement it to know how many times a certain frame is opened or executed

for example:

when I start my game the attempts = 1

enter image description here

later when I touch a button and validate the question (It is in another JframeForm)

enter image description here

and I go back to the previous frame I want him to add 1 to the attempts

enter image description here

This is my code:

import com.sun.media.sound.SoftAbstractResampler;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import spartacus20.coddy.logica.Logica;
import javax.swing.Timer;
import javax.xml.transform.Source;
import spartacus20.coddy.logica.Preguntas;

public class Juegos extends javax.swing.JFrame implements ActionListener{

    Preguntas p = new Preguntas(50); 
    
    Timer tiempo;
    private int count = 60; 
    private int intento; 
    private int puntosTotales;
    


    public Juegos() {
       
        initComponents(); 
        intento = 0; 
        puntosTotales = 0; 
    }

      public void actionPerformed(ActionEvent e) {
        ## HERE IS THE PROBLEM## ###
        
        intento++; 
        System.out.println(intento);
    
    }

Answer:

import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFCell;


public class Preguntas {
    
   private String pregunta; 
   private String respuesta; 
   private int puntos; 
   private int intentos=1; 
   private String opcion; 
   
   public Preguntas(int p){
       
       this.puntos = p; 
       
   }

    public int getPuntos() {
        return puntos;
    }
   
   
   
   public String getOpcion(int fila, int celda){
       
       Logica log = new Logica();
       
       List dato = log.cargarFichero(); 
       
       List datos_temporales = (List) dato.get(fila); 
       
       XSSFCell celdas = (XSSFCell) datos_temporales.get(celda); 
       
       String opcion = celdas.toString(); 
       
       return opcion; 
   }
  
   public String  getPregunta(int filas){
       
       Logica log = new Logica(); 
     
       
      
       
       List datos_celda =  log.cargarFichero();
       
       List lista_temporal = (List) datos_celda.get(filas);
        
        XSSFCell celdas = (XSSFCell) lista_temporal.get(0); 
        
        String pregunta = celdas.toString();
        
        
        return  pregunta; 
       
       
   } 
   
  
   public String getRespuesta(int filas){
       
       Logica log = new Logica(); 
       
       
       List datos_celda = log.cargarFichero(); 
       
       
            List lista_temporal = (List) datos_celda.get(filas);
        
        XSSFCell celdas = (XSSFCell) lista_temporal.get(5); 
        
        String respuesta = celdas.toString();
        
        
        return  respuesta; 
       
  }
   
}

This is where we go back to the previous frame

import spartacus20.coddy.logica.Preguntas;

public class Incorrecto extends javax.swing.JFrame {

  
    
    
    public Incorrecto() {
        initComponents();
    }
    
    private void b_exitActionPerformed(java.awt.event.ActionEvent evt) {                                       

        dispose();
        setVisible(false);
        System.out.println("Good Bye :C");

    }                                      

    private void b_voltarActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        setVisible(false);
        Juegos juego = new Juegos(); 
       juego.setVisible(true);


    }                                        

   
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...