Java Program to illustrate - changing contents within the same JFrame object

This can be helpful when one need to make a GUI based java software. When you need that the contents of the same frame object get replaced by contents of another container/panel when a user clicks on the say, 'NEXT' button. Following program will give you hints about how you could do that..
Program Name: GUItest.java
import javax.swing.*;
public class GUItest extends JFrame
{
    GUItest()
    {
        setSize(300, 200);
        setVisible(true);
//        setLayout(new GridLayout(1,1));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add((new JPanel()).add(new JButton("Hello")));
        this.getContentPane().removeAll();
        this.add((new JPanel()).add(new JButton("World!!")));
    }
    public static void main(String args[])
    {
        GUItest frame1 = new GUItest();
    }
}

No comments:

Post a Comment