英文:
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?
答案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);
}
}
英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论