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