Skip to content
Advertisement

How to clear a JFrame?

I’m still a noob to Swing classes and I wanted to clear the screen after the user activates a button.

    JButton tes=new JButton("TEST");
    tes.setBounds(550, 375, 200, 75);
    tes.setFocusable(false);
    tes.setFont(new Font("comic sans",Font.PLAIN,20));
    tes.setBackground(Color.white);
    tes.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
    tes.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                test('T');
            }
            catch(IOException e1) {
                throw new UncheckedIOException(e1);
            }
        }
    });

    f.add(tit);f.add(sub);
    f.add(pr);f.add(tes);
    f.setVisible(true);
}

I have used the following and it didn’t really do anything.

removeAll()
revalidate();
repaint(); 

Advertisement

Answer

private JFrame frame = new JFrame();
frame.getContentPane().removeAll();
frame.repaint();

you should use it this way

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement