1 | initial version |
Components can be dynamically removed and added to a JFrame by using the following steps:
Here is an example code snippet that demonstrates how to dynamically remove and add components to a JFrame:
import javax.swing.*;
public class ExampleFrame extends JFrame {
public ExampleFrame() {
setTitle("Example JFrame");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add initial components
JLabel label = new JLabel("Hello World");
add(label);
// Remove and add new component
remove(label);
JButton button = new JButton("Click me!");
add(button);
// Update layout
revalidate();
repaint();
}
public static void main(String[] args) {
ExampleFrame frame = new ExampleFrame();
frame.setVisible(true);
}
}