简体   繁体   中英

How can I create an expandable grid of Swing buttons?

I am trying to make an A* Pathfinding Visualizer but right now I am stuck on creating the grid. What is the best way of creating a grid like what is seen below? For instance, should I just use a bunch of JButton components or is there some other way?

我想做什么

在此处输入图像描述

Put JButton components in a GridLayout , using removeAll() on the panel before setting a new grid layout to change the number of columns and/or rows. Lastly pack() the top level container (in this case a JFrame ) to fit the number of rows and columns.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;

public class ChangableButtonGrid {

    private JComponent ui = null;
    JPanel gridArea = new JPanel();

    public static final int SIZE = 600;
    JToolBar tools = new JToolBar("Tools");
    SpinnerNumberModel colModel = new SpinnerNumberModel(20, 5, 50, 1);
    SpinnerNumberModel rowModel = new SpinnerNumberModel(5, 5, 50, 1);
    ChangeListener changeListener = (ChangeEvent e) -> {
        refresh();
    };

    public ChangableButtonGrid() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        ui.add(gridArea);

        ui.add(tools,BorderLayout.PAGE_START);
        tools.setLayout(new FlowLayout(FlowLayout.LEADING));
        addModelToToolbar("Cols", colModel);
        addModelToToolbar("Rows", rowModel);

        ui.add(gridArea);
    }

    private void refresh() {
        int cols = colModel.getNumber().intValue();
        int rows = rowModel.getNumber().intValue();
        gridArea.removeAll();
        gridArea.setLayout(new GridLayout(rows, cols));
        for (int rr=0; rr<rows; rr++) {
            for (int cc=0; cc<cols; cc++) {
                JButton b = new JButton(cc + "," + rr);
                gridArea.add(b);
            }
        }
        Container c = gridArea.getTopLevelAncestor();
        JFrame f = (JFrame)c;
        f.pack();
    }

    private void addModelToToolbar(String label, SpinnerNumberModel model) {
        tools.add(new JLabel(label));
        JSpinner spinner = new JSpinner(model);
        spinner.addChangeListener(changeListener);
        tools.add(spinner);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            ChangableButtonGrid o = new ChangableButtonGrid();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            o.refresh();

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

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