Buttons with a border are not visible

I have an example that should be a Layouts example. A simple program that, when the user clicks buttons, changes the layout of the border. It worked fine until I added the last two frames, when I run it, the buttons are missing. I have checked and checked and cannot find the problem. Can anyone please help?

    public class FrameLayouts {

    public static void main (String [] args){

        final JFrame frame1 = new JFrame("FlowLayout");
        final JFrame frame2 = new JFrame("GridLayout");
        final JFrame frame3 = new JFrame("BorderLayout");

        frame1.setVisible(true);
        frame2.setVisible(false);
        frame3.setVisible(false);


        frame1.setLocationRelativeTo(null);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setSize(500,500);
        frame1.setLayout(new FlowLayout());

        JButton button1 = new JButton ("Grid Layout");
        JButton button2 = new JButton ("Border Layout");
        JButton button3 = new JButton ("Flow Layout");
        JButton button4 = new JButton ("EXTRA");
        JButton button5 = new JButton ("EXTRA");

        frame1.add(button4);
        frame1.add(button1);
        frame1.add(button2);
        frame1.add(button3);
        frame1.add(button5);
        frame1.pack();

        frame2.add(button4);
        frame2.add(button1);
        frame2.add(button2);
        frame2.add(button3);
        frame2.add(button5);
        frame2.pack();

        frame3.add(button4, BorderLayout.NORTH);
        frame3.add(button1, BorderLayout.CENTER);
        frame3.add(button2, BorderLayout.SOUTH);
        frame3.add(button3, BorderLayout.EAST);
        frame3.add(button5, BorderLayout.WEST);
        frame3.pack();


        button1.addActionListener(new ActionListener(){
            public void actionPerformed (ActionEvent e){
                frame2.setVisible(true);
                frame2.setLocationRelativeTo(null);
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame2.setSize(500,500);
                frame2.setLayout(new GridLayout(2,3));

                frame3.setVisible(false);
                frame1.setVisible(false);
            }
        });

        button2.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                frame3.setVisible(true);
                frame3.setLocationRelativeTo(null);
                frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame3.setSize(500,500);
                frame3.setLayout(new BorderLayout(5,5));

                frame1.setVisible(false);
                frame2.setVisible(false);

            }
        });

        button3.addActionListener(new ActionListener(){
            public void actionPerformed (ActionEvent e){
                frame1.setVisible(true);
                frame1.setLocationRelativeTo(null);
                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.setSize(500,500);
                frame1.setLayout(new FlowLayout());

                frame2.setVisible(false);
                frame3.setVisible(false);

            }
        });

    }


}

      

+3


source to share


1 answer


In fact, you cannot add the same button to multiple containers. When you add it to the second frame, it is removed from the first.



Just create a copy of the buttons for each instance of the frame.

+2


source







All Articles