简体   繁体   中英

JApplet, JPanel, JButton, have to resize the applet to repaint()

I have a JButton, in a JPanel, in a JApplet, that when clicked, does some stuff, changes the contents of the JPanel, then should repaint. However, it doesn't seem like it's repainting anything more than the button its self and the JPanel is repainting it's self too. However, I think I need to have the JApplet repaint itsself. Essentially, Ill click one of the buttons, the buttons will change, but the JLabels won't show up. Once I rezise the applet, or in the browser make it so small that the sides of the browser touch the applet, it all resets and looks great! One of the buttons code is as follows:

reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                //resets the vars
            questions = 0;
            wrong = 0;
            correct = 0;
            //clears the JPanel
            overallJP.removeAll();
            //adds new question, and buttons
            rand = generator.nextInt(10);
            JLabel tmp = new JLabel("Answer the Question: "+"\n"+qArr.get(rand).getQuestion());
            overallJP.setSize(new Dimension(500,700));
            overallJP.add(tmp);
            overallJP.add(buttonArea);
            overallJP.repaint();
            setVisible(true);
            repaint();
            }
        });

And the other buttons look similar. I think the problem is on the repaint() at the bottom. Is there a way to call the JApplet's repaint from inside this anonymous function? The reset button is declared in the init() of the applet. If you have more questions or need more code, just ask!

Since you're removing components from the panel, you may need to revalidate it. Try adding: validate();

EDIT:

Container.validate() triggers container's re-layout. It should be invoked when container is modified - components added, removed, etc after the container has been displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM