如何创建一个可展开的 Swing 按钮网格?

huangapple go评论78阅读模式
英文:

How can I create an expandable grid of Swing buttons?

问题

我正在尝试制作一个A*路径规划可视化工具,但目前卡在了创建网格的步骤。创建类似下面所示的网格的最佳方法是什么呢?例如,我应该只是使用一堆JButton组件,还是有其他方法?

(图片已省略)

英文:

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?

如何创建一个可展开的 Swing 按钮网格?

答案1

得分: 3

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);
    }
}
英文:

如何创建一个可展开的 Swing 按钮网格?

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(&quot;Tools&quot;);
SpinnerNumberModel colModel = new SpinnerNumberModel(20, 5, 50, 1);
SpinnerNumberModel rowModel = new SpinnerNumberModel(5, 5, 50, 1);
ChangeListener changeListener = (ChangeEvent e) -&gt; {
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(&quot;Cols&quot;, colModel);
addModelToToolbar(&quot;Rows&quot;, 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&lt;rows; rr++) {
for (int cc=0; cc&lt;cols; cc++) {
JButton b = new JButton(cc + &quot;,&quot; + 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 = () -&gt; {
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);
}
}

huangapple
  • 本文由 发表于 2020年5月29日 14:31:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/62079954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定