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

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

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

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.event.*;
  4. import javax.swing.border.EmptyBorder;
  5. public class ChangableButtonGrid {
  6. private JComponent ui = null;
  7. JPanel gridArea = new JPanel();
  8. public static final int SIZE = 600;
  9. JToolBar tools = new JToolBar("Tools");
  10. SpinnerNumberModel colModel = new SpinnerNumberModel(20, 5, 50, 1);
  11. SpinnerNumberModel rowModel = new SpinnerNumberModel(5, 5, 50, 1);
  12. ChangeListener changeListener = (ChangeEvent e) -> {
  13. refresh();
  14. };
  15. public ChangableButtonGrid() {
  16. initUI();
  17. }
  18. public final void initUI() {
  19. if (ui != null) return;
  20. ui = new JPanel(new BorderLayout(4, 4));
  21. ui.setBorder(new EmptyBorder(4, 4, 4, 4));
  22. ui.add(gridArea);
  23. ui.add(tools, BorderLayout.PAGE_START);
  24. tools.setLayout(new FlowLayout(FlowLayout.LEADING));
  25. addModelToToolbar("Cols", colModel);
  26. addModelToToolbar("Rows", rowModel);
  27. ui.add(gridArea);
  28. }
  29. private void refresh() {
  30. int cols = colModel.getNumber().intValue();
  31. int rows = rowModel.getNumber().intValue();
  32. gridArea.removeAll();
  33. gridArea.setLayout(new GridLayout(rows, cols));
  34. for (int rr = 0; rr < rows; rr++) {
  35. for (int cc = 0; cc < cols; cc++) {
  36. JButton b = new JButton(cc + "," + rr);
  37. gridArea.add(b);
  38. }
  39. }
  40. Container c = gridArea.getTopLevelAncestor();
  41. JFrame f = (JFrame) c;
  42. f.pack();
  43. }
  44. private void addModelToToolbar(String label, SpinnerNumberModel model) {
  45. tools.add(new JLabel(label));
  46. JSpinner spinner = new JSpinner(model);
  47. spinner.addChangeListener(changeListener);
  48. tools.add(spinner);
  49. }
  50. public JComponent getUI() {
  51. return ui;
  52. }
  53. public static void main(String[] args) {
  54. Runnable r = () -> {
  55. try {
  56. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  57. } catch (Exception ex) {
  58. ex.printStackTrace();
  59. }
  60. ChangableButtonGrid o = new ChangableButtonGrid();
  61. JFrame f = new JFrame(o.getClass().getSimpleName());
  62. f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  63. f.setLocationByPlatform(true);
  64. f.setContentPane(o.getUI());
  65. f.pack();
  66. o.refresh();
  67. f.setVisible(true);
  68. };
  69. SwingUtilities.invokeLater(r);
  70. }
  71. }
英文:

如何创建一个可展开的 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.

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.event.*;
  4. import javax.swing.border.EmptyBorder;
  5. public class ChangableButtonGrid {
  6. private JComponent ui = null;
  7. JPanel gridArea = new JPanel();
  8. public static final int SIZE = 600;
  9. JToolBar tools = new JToolBar(&quot;Tools&quot;);
  10. SpinnerNumberModel colModel = new SpinnerNumberModel(20, 5, 50, 1);
  11. SpinnerNumberModel rowModel = new SpinnerNumberModel(5, 5, 50, 1);
  12. ChangeListener changeListener = (ChangeEvent e) -&gt; {
  13. refresh();
  14. };
  15. public ChangableButtonGrid() {
  16. initUI();
  17. }
  18. public final void initUI() {
  19. if (ui!=null) return;
  20. ui = new JPanel(new BorderLayout(4,4));
  21. ui.setBorder(new EmptyBorder(4,4,4,4));
  22. ui.add(gridArea);
  23. ui.add(tools,BorderLayout.PAGE_START);
  24. tools.setLayout(new FlowLayout(FlowLayout.LEADING));
  25. addModelToToolbar(&quot;Cols&quot;, colModel);
  26. addModelToToolbar(&quot;Rows&quot;, rowModel);
  27. ui.add(gridArea);
  28. }
  29. private void refresh() {
  30. int cols = colModel.getNumber().intValue();
  31. int rows = rowModel.getNumber().intValue();
  32. gridArea.removeAll();
  33. gridArea.setLayout(new GridLayout(rows, cols));
  34. for (int rr=0; rr&lt;rows; rr++) {
  35. for (int cc=0; cc&lt;cols; cc++) {
  36. JButton b = new JButton(cc + &quot;,&quot; + rr);
  37. gridArea.add(b);
  38. }
  39. }
  40. Container c = gridArea.getTopLevelAncestor();
  41. JFrame f = (JFrame)c;
  42. f.pack();
  43. }
  44. private void addModelToToolbar(String label, SpinnerNumberModel model) {
  45. tools.add(new JLabel(label));
  46. JSpinner spinner = new JSpinner(model);
  47. spinner.addChangeListener(changeListener);
  48. tools.add(spinner);
  49. }
  50. public JComponent getUI() {
  51. return ui;
  52. }
  53. public static void main(String[] args) {
  54. Runnable r = () -&gt; {
  55. try {
  56. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  57. } catch (Exception ex) {
  58. ex.printStackTrace();
  59. }
  60. ChangableButtonGrid o = new ChangableButtonGrid();
  61. JFrame f = new JFrame(o.getClass().getSimpleName());
  62. f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  63. f.setLocationByPlatform(true);
  64. f.setContentPane(o.getUI());
  65. f.pack();
  66. o.refresh();
  67. f.setVisible(true);
  68. };
  69. SwingUtilities.invokeLater(r);
  70. }
  71. }

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:

确定