Auxílio Lógico

Pessoal, bom dia!!!

Estava fazendo essa telinha básica, porém eu não estou conseguindo colocar um i++;, está dando erro.

Basicamente a pessoa seleciona uma opção e aperta o botão, quando ela apertar o botão vai armazenar o número, e queria que o i aumentasse para mudar de pergunta quando a pessoa pressionasse o botão.
Logo depois de armazenar o valor

Alguém poderia me explicar onde estou errando?

Desde já agradeço!!!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author Cabral
 */
public class NewClass extends JFrame
{
    final private JPanel contentPane;
  
    private String[] pergunta;
    private int[] resposta;
    
      public static void main(String[] args) 
      {
      try
      {
          NewClass inicial = new NewClass(0);
          inicial.setVisible(true);
          inicial.setLocationRelativeTo(null);
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }
    
    public NewClass(int i)
    {
        this.pergunta = new String[5];
        pergunta[0] = "Qual fruta é a laranja?";
        pergunta[1] = "Qual fruta é a maça?";
        pergunta[2] = "Qual fruta é a melancia?";
        pergunta[3] = "Qual fruta é a banana?";
        pergunta[4] = "Qual fruta é o mamão?";
        
        
        this.resposta = new int[5]; 
                
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setBounds(100, 100, 800, 600);
	setResizable(false);
	contentPane = new JPanel();
	setContentPane(contentPane);
	contentPane.setLayout(null); 
        
        JLabel lblPrincipal = new JLabel("Bem vindo");
        lblPrincipal.setBounds(100,100,600,100);
        contentPane.add(lblPrincipal);
        
        JLabel lblPergunta = new JLabel(pergunta[i]);
        lblPergunta.setBounds(330,350,600,40);
        contentPane.add(lblPergunta);
        
        JRadioButton rb1 = new JRadioButton("Mamão");
        rb1.setBounds(100, 400, 60, 40);
        rb1.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[i] = 1;
			}
		});
        contentPane.add(rb1);
        
        JRadioButton rb2 = new JRadioButton("Banana");
        rb2.setBounds(200,400,60,40);
        rb2.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[i] = 2;
			}
		});
        contentPane.add(rb2);
        
        JRadioButton rb3 = new JRadioButton("Melancia");
        rb3.setBounds(300,400,60,40);
        rb3.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[i] = 3;
			}
		});
        contentPane.add(rb3);
        
        JRadioButton rb4 = new JRadioButton("Laranja");
        rb4.setBounds(400,400,60,40);
        rb4.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[i] = 4;
			}
		});
        contentPane.add(rb4);
        
        JRadioButton rb5 = new JRadioButton("Pêra");
        rb5.setBounds(500,400,60,40);
        rb5.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[i] = 5;
			}
		});
        contentPane.add(rb5);
        
        JRadioButton rb6 = new JRadioButton("Maçã");
        rb6.setBounds(600,400,60,40);
        rb6.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[i] = 6;
			}
		});
        contentPane.add(rb6);
        
        JRadioButton rb7 = new JRadioButton("Tomate");
        rb7.setBounds(700,400,60,40);
        rb7.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[i] = 7;
			}
		});
        contentPane.add(rb7);
        
        ButtonGroup group = new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);
        group.add(rb5);
        group.add(rb6);
        group.add(rb7);
        
        JButton botaoSubmit = new JButton("Confirmar");
        botaoSubmit.setBounds(610,500,140,40);
        botaoSubmit.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            System.out.println(resposta[0]);
                        }
		});
        contentPane.add(botaoSubmit);
   
    }
}
rb1.addActionListener(new ActionListener() 
	{
		public void actionPerformed(ActionEvent arg0) 
		{
                        resposta[i] = 1;
		}
	});
    contentPane.add(rb1);

o “i” vem de onde? intendo que i seria uma variavel de um loop, agora se vc quer que uma posição receba o 1, faça assim:

resposta[0] = 1;

public static void main(String[] args)
{
try
{
NewClass inicial = new NewClass(0);
inicial.setVisible(true);
inicial.setLocationRelativeTo(null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public NewClass(int i)

O método construtor recebe o i e baseado nisso a pergunta e a resposta se modificam
pq se eu colocar resposta[0] = 1; quando eu mudar para pergunta[1], irá continuar armazenando na posição 0 e eu quero que armazene cada valor na posição relativa a pergunta

haaaa entendi…hehehe…e onde vc quer colocar o i++

1 curtida

no botaoSubmit

daí vc quer informar um novo valor no construtor??

Exato, meio que atualizar o valor, sei lá

botaoSubmit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(resposta[i]);
            
            int t = i;
            t++;
            NewClass inicial = new NewClass(t);
            
        }
    });

a lógica é essa, mais como vc vai atualizar é outra história…hehe

botaoSubmit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
System.out.println(resposta[i]);
group.clearSelection();
int t = i;
t++;
dispose();
NewClass inicial = new NewClass(t);
inicial.setVisible(true);
inicial.setLocationRelativeTo(null);

      if(t == 4)
       {
           int soma = resposta[0]+resposta[1];
           System.out.println(soma);
       } 
        contentPane.add(lblPrincipal);
 }}

Funcionou
porém ele está resetando os valores do array e não é possível realizar a soma depois.
Por Exemplo, cada botão tem um número, que serão armazenados no array resposta, porém como o frame inicia do zero, os valores ficam zerados e ao tentar somar retorna 0

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author Cabral
 */
public class NewClass extends JFrame
{
    final private JPanel contentPane;
  
    private String[] pergunta;
    private int[] resposta;
    int contador = 0;
    public JLabel lblPergunta;
    
      public static void main(String[] args) 
      {
      try
      {
          NewClass inicial = new NewClass(0);
          inicial.setVisible(true);
          inicial.setLocationRelativeTo(null);
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }
    
    public NewClass(int i)
    {
        this.pergunta = new String[5];
        pergunta[0] = "Qual fruta é a laranja?";
        pergunta[1] = "Qual fruta é a maça?";
        pergunta[2] = "Qual fruta é a melancia?";
        pergunta[3] = "Qual fruta é a banana?";
        pergunta[4] = "Qual fruta é o mamão?";
        
        
        this.resposta = new int[5]; 
                
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setBounds(100, 100, 800, 600);
	setResizable(false);
	contentPane = new JPanel();
	setContentPane(contentPane);
	contentPane.setLayout(null); 
        
        JLabel lblPrincipal = new JLabel("Bem vindo");
        lblPrincipal.setBounds(100,100,600,100);
        contentPane.add(lblPrincipal);
        
        lblPergunta = new JLabel(pergunta[i]);
        lblPergunta.setBounds(330,350,600,40);
        contentPane.add(lblPergunta);
        
        JRadioButton rb1 = new JRadioButton("Mamão");
        rb1.setBounds(100, 400, 60, 40);
        rb1.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[contador] = 1;
			}
		});
        contentPane.add(rb1);
        
        JRadioButton rb2 = new JRadioButton("Banana");
        rb2.setBounds(200,400,60,40);
        rb2.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[contador] = 2;
			}
		});
        contentPane.add(rb2);
        
        JRadioButton rb3 = new JRadioButton("Melancia");
        rb3.setBounds(300,400,60,40);
        rb3.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[contador] = 3;
			}
		});
        contentPane.add(rb3);
        
        JRadioButton rb4 = new JRadioButton("Laranja");
        rb4.setBounds(400,400,60,40);
        rb4.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[contador] = 4;
			}
		});
        contentPane.add(rb4);
        
        JRadioButton rb5 = new JRadioButton("Pêra");
        rb5.setBounds(500,400,60,40);
        rb5.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[contador] = 5;
			}
		});
        contentPane.add(rb5);
        
        JRadioButton rb6 = new JRadioButton("Maçã");
        rb6.setBounds(600,400,60,40);
        rb6.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[contador] = 6;
			}
		});
        contentPane.add(rb6);
        
        JRadioButton rb7 = new JRadioButton("Tomate");
        rb7.setBounds(700,400,60,40);
        rb7.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
                            resposta[contador] = 7;
			}
		});
        contentPane.add(rb7);
        
        ButtonGroup group = new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);
        group.add(rb5);
        group.add(rb6);
        group.add(rb7);
        
        JButton botaoSubmit = new JButton("Confirmar");
        botaoSubmit.setBounds(610,500,140,40);
        botaoSubmit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) 
        {                     
            Executar();
        }
    });
        contentPane.add(botaoSubmit);
   
    }
     private void Executar() 
	    { // ESTE E O METODO

	        Thread t1 = new Thread() { // instancia uma thread, isso permite que o processamento da aplicação se divide
	            @Override
	            public void run() { // inicia o metodo run()
	                super.run();
	                try {
	                	String texto = null;
	                	contador++;
	                	switch(contador)
	                	{
	                	case 1:
	                		texto = pergunta[contador];
                                        
                                        System.out.println(resposta[contador]);
	                	break;
	                	case 2:
	                		texto = pergunta[contador];
                                        System.out.println(resposta[contador]);
	                	break;
	                	case 3:
	                		texto = pergunta[contador];
                                        System.out.println(resposta[contador]);
	                       break;
	                	case 4:
	                		texto = pergunta[contador];
                                        System.out.println(resposta[contador]);
	                	break;	
                                case 5:
                                        int soma = resposta[1]+resposta[0];
                                        String Ssoma = Integer.toString(soma);
                                        texto = Ssoma;
	                	}
	                    String novoTexto = ""; 
	                    for (int i = 0; i < texto.length(); i++) { // define um for com o tamanho do texto
	                        Thread.sleep(30); 
	                        novoTexto = novoTexto + texto.charAt(i); 
	                        lblPergunta.setText(novoTexto); 
	                    }
	                } catch (Exception a) {
	                }
	            }
	        };
	        t1.start(); // Inicia a Thread
	    }
}

Consegui, criei um thread para não ter que ficar criando uma nova tela a todo momento
Muito obrigado pela ajuda