// This program demonstrates opening multiple windows via JRadioButtons import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SelectPopUp { public static void main(String[] args) { JFrame f=new JFrame(); f.setSize(200,200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GuiPanel gp=new GuiPanel(); f.add(gp); f.setVisible(true); } public static class GuiPanel extends JPanel implements ItemListener // we will use ItemListener (we could use ActionListener) { private JRadioButton b1, b2, b3, b4, b5; // our JRadioButtons control which popup window to open private JFrame f2, f3, f4, f5; // 4 more JFrames which we will control by setting visible and invisible public GuiPanel() { b1=new JRadioButton("Figure 1"); // create our imaginatively titled JRadioButtons b2=new JRadioButton("Figure 2"); b3=new JRadioButton("Figure 3"); b4=new JRadioButton("Figure 4"); b5=new JRadioButton("Close"); b1.addItemListener(this); // this class will handle the ItemListener b2.addItemListener(this); b3.addItemListener(this); b4.addItemListener(this); b5.addItemListener(this); ButtonGroup bg=new ButtonGroup(); // group all 5 radio buttons into one group so only one can be selected bg.add(b1); bg.add(b2); bg.add(b3); bg.add(b4); bg.add(b5); add(b1); // add the 5 buttons to this JPanel add(b2); add(b3); add(b4); add(b5); f2=new JFrame(); // each button will control a JFrame popping up, we create the JFrames here f2.setSize(400,400); f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Fig1 p2=new Fig1(); // we use 4 inner class JPanels, each of which presents a different Graphics image into its JFrame f2.add(p2); // note that while we add the JPanel to the JFrame, we don't make the JFrame visible yet f3=new JFrame(); f3.setSize(400,400); f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Fig2 p3=new Fig2(); f3.add(p3); f4=new JFrame(); f4.setSize(400,400); f4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Fig3 p4=new Fig3(); f4.add(p4); f5=new JFrame(); f5.setSize(400,400); f5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Fig4 p5=new Fig4(); f5.add(p5); } public void itemStateChanged(ItemEvent e) { if(b1.isSelected()) { f2.setVisible(true); // select button1? Make frame2 (Fig1) visible // we could also do f3.setVisible(false);f4.setVisible(false);f5.setVisible(false); so that we } // hide any other visible JFrames else if(b2.isSelected()) { Fig2 p3=new Fig2(); f3.add(p3); f3.setVisible(true); } else if(b3.isSelected()) { Fig3 p4=new Fig3(); f4.add(p4); f4.setVisible(true); } else if(b4.isSelected()) { Fig4 p5=new Fig4(); f5.add(p5); f5.setVisible(true); } else if(b5.isSelected()) { f2.setVisible(false); // the close button causes all JFrames to be invisible (removes them) f3.setVisible(false); f4.setVisible(false); f5.setVisible(false); } } // now we define the JPanel classes for each of the 4 figures, all just have a paintComponent method public class Fig1 extends JPanel { public void paintComponent(Graphics g) { g.fillRect(50,50,250,250); // for simplicity, each only provides a minimal drawing } } public class Fig2 extends JPanel { public void paintComponent(Graphics g) { g.fillOval(50,50,250,250); } } public class Fig3 extends JPanel { public void paintComponent(Graphics g) { g.drawString("figure 3", 50,50); } } public class Fig4 extends JPanel { public void paintComponent(Graphics g) { g.drawLine(50,50,250,250); g.drawLine(50,250,250,50); } } } }