Frage

I have a JFrame which consists of some Swing components. One button has an ActionListener to add an extra button to the frame (so the user can add more information). Now I want the window (jframe) to resize whenever a new component is added. Now the components get smaller whenever a new one is added, but the frame stays the same size.

Here is the code of the actionlistener:

addAnswerButtonMA.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            answerFieldsMA.add(new JTextField());
            checkBoxesMA.add(new JCheckBox());
            multipleanswerPanel.add(answerFieldsMA.get(answerFieldsMA.size() - 1));
            multipleanswerPanel.add(checkBoxesMA.get(checkBoxesMA.size() - 1));

            multipleanswerPanel.revalidate();
            validate();
            }
        });

Some background (not sure if needed): I'm making a quiz program, the administrator can add questions to the quiz by using a separate gui. If he wants to add a Multiple-answer question, he can add an extra answer by clicking the addAnswerbuttonMA to make an extra field and checkbox appear. The field represents the answer and the checkbox represents whether the answer is correct or not.

War es hilfreich?

Lösung

Packing the frame solves it.

addAnswerButtonMA.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        answerFieldsMA.add(new JTextField());
        checkBoxesMA.add(new JCheckBox());
        multipleanswerPanel.add(answerFieldsMA.get(answerFieldsMA.size() - 1));
        multipleanswerPanel.add(checkBoxesMA.get(checkBoxesMA.size() - 1));

        multipleanswerPanel.revalidate();
        validate();
        pack()
        }
    });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top