I’m having doubts about updating a Jframe, Help Plz

Asked

Viewed 24 times

-1

I’m trying to do a quiz. So I would like to update the same "screen" or frame - as you like - that is, you have the first question, the user responds and the frame updates with a new question and so on. My idea was to do everything on the same screen, but I accept suggestions, if they recommend I create other frames (which would be easier, but more laborious). It follows the code: String[] questions = new String[3]; questions[0] = "Question 1 ?"; questions1 = "Question 2 ?" questions[2] = "Question 3 ?";

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 600, 471);
    contentPane = new JPanel();
    contentPane.setBackground(new Color(255, 153, 0));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(0, 167, 616, 265);
    panel.setBackground(new Color(0, 0, 128));
    contentPane.add(panel);
    panel.setLayout(null);

    rdA = new JRadioButton(" A");
    rdA.setBackground(new Color(0, 0, 128));
    rdA.setForeground(Color.WHITE);
    rdA.setFont(new Font("Tahoma", Font.PLAIN, 12));
    rdA.setBounds(24, 53, 48, 23);
    panel.add(rdA);

    rdB = new JRadioButton(" B");
    rdB.setForeground(Color.WHITE);
    rdB.setFont(new Font("Tahoma", Font.PLAIN, 12));
    rdB.setBackground(new Color(0, 0, 128));
    rdB.setBounds(24, 94, 48, 23);
    panel.add(rdB);

    rdC = new JRadioButton(" C");
    rdC.setForeground(Color.WHITE);
    rdC.setFont(new Font("Tahoma", Font.PLAIN, 12));
    rdC.setBackground(new Color(0, 0, 128));
    rdC.setBounds(24, 140, 48, 23);
    panel.add(rdC);

    rdD = new JRadioButton(" D");
    rdD.setForeground(Color.WHITE);
    rdD.setFont(new Font("Tahoma", Font.PLAIN, 12));
    rdD.setBackground(new Color(0, 0, 128));
    rdD.setBounds(24, 186, 48, 23);
    panel.add(rdD);

    alternativas = new ButtonGroup ();
    alternativas.add(rdA);
    alternativas.add(rdB);
    alternativas.add(rdC);
    alternativas.add(rdD);


    JButton btnAvan = new JButton("Avan\u00E7ar");
    btnAvan.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        }
    });
    btnAvan.setBounds(292, 231, 89, 23);
    panel.add(btnAvan);
    }
}

inserir a descrição da imagem aqui

1 answer

0

I believe the most effective way I can (as far as I know) do this is to create an object, Panel or Canvas, with everything organized, or call a function that reorganizes the way you want, so just do a simple manipulation of variables, putting some parameters in the method constructor of the object (or function), because thus it becomes easier to instantiate this object the way you want.

A EXAMPLE how it would be instantiated:

PainelPergunta novaPergunta = new PainelPergunta(pergunta, alternativa1, alternativa2, alternativa3, alternativa4, resposta);

Just organize your object with some simple algorithms, then in the example above, you can create a new object by passing the required parameters.

Or, you can always keep the same object, and reorganize the items and options contained in it through a function:

NovaPergunta(pergunta, alternativa1, alternativa2, alternativa3, alternativa4, resposta);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.