简体   繁体   中英

How do I switch between different layouts for java swing using a menu?

I want to make an app where I have a JPanel with no default layout. I want to be able to change the layout using options from the menu. For instance, if I have a control to add three image icons to the JPanel , the size and position of these icons should be determined by the frame's current layout manager. So if I add 3 image icons to a Border Layout (adding them in the South, East and Center positions), switching the layout to a flow layout should make them appear in right to left order with no resizing.

I am confused as to how to go about that. Is there a way to switch layouts within the same JPanel like this?

To dynamically change layouts after each menu action, set the new layout with its constraints and call the revalidate() method. Check the actionPerformed() method of below code.

public class LayoutController extends JFrame implements ActionListener {

    JMenuItem flowLayout;
    JMenuItem borderLayout;
    JMenuItem gridLayout;

    JPanel panel;

    JButton button1;
    JButton button2;
    JButton button3;

    public LayoutController() {
        setSize(600, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(null); // no default layout
        panel.setSize(600, 600);

        flowLayout = new JMenuItem("Flow Layout");
        flowLayout.addActionListener(this);
        borderLayout = new JMenuItem("Border Layout");
        borderLayout.addActionListener(this);
        gridLayout = new JMenuItem("Grid Layout");
        gridLayout.addActionListener(this);

        // menu to change layout dynamically
        JMenu fileMenu = new JMenu("Layout");
        fileMenu.add(flowLayout);
        fileMenu.add(borderLayout);
        fileMenu.add(gridLayout);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        // Customize your components
        button1 = new JButton("IMAGE1");
        button2 = new JButton("IMAGE2");
        button3 = new JButton("IMAGE3");

        panel.add(button1);
        panel.add(button2);
        panel.add(button3);

        add(panel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LayoutController().setVisible(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //Customize your code here
        if (e.getSource() == flowLayout) {
            //flow layout with right alignment
            panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        } else if (e.getSource() == borderLayout) {
            // border layout with constraints
            BorderLayout layout = new BorderLayout();
            layout.addLayoutComponent(button1, BorderLayout.SOUTH);
            layout.addLayoutComponent(button2, BorderLayout.EAST);
            layout.addLayoutComponent(button3, BorderLayout.CENTER);
            panel.setLayout(layout);
        } else if (e.getSource() == gridLayout) {
            // grid layout with 2 rows and 2 columns
            panel.setLayout(new GridLayout(2, 2));
        }
        panel.revalidate();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM