无法垂直添加按钮。

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

unable to add buttons vertically

问题

  1. 我正在编写一个程序其中的JButton以垂直方式动态添加到JPanel按钮存储在ArrayList)。我尝试了以下代码通过将JPanel设置为GridBagLayout布局
  2. for(int i = 0; i < listOfButtons.size(); i++) {
  3. c.gridx = 0;
  4. c.gridy = i;
  5. leftButtonPanel.add(listOfButtons.get(i));
  6. }

结果如下所示:

无法垂直添加按钮。

添加按钮后的效果如下:

[![enter image description here][2]][2]

我还尝试了将JPanel设置为GridLayout布局:

  1. leftButtonPanel.setLayout(new GridLayout(listOfButtons.size(), 1));
  1. for(int i = 0; i < listOfButtons.size(); i++) {
  2. leftButtonPanel.add(listOfButtons.get(i));
  3. }

[![before adding][3]][3]

[![enter image description here][4]][4]

按钮"see all"和"add"都在同一个listOfButtons数组列表中。唯一的方法将按钮添加到面板中是通过该for循环。由于某些原因,按钮仍然水平排列。

  1. [1]: https://i.stack.imgur.com/lui0I.png
  2. [2]: https://i.stack.imgur.com/VAz9H.png
  3. [3]: https://i.stack.imgur.com/EpWJA.png
  4. [4]: https://i.stack.imgur.com/t6BE3.png
  5. <details>
  6. <summary>英文:</summary>
  7. I am writing a program where JButtons are added dynamically to a JPanel in a vertical fashion. (the buttons are stored in an arraylist)I have tried the following code by setting the JPanel to gridbaglayout.
  1. for(int i = 0; i&lt;listOfButtons.size();i++) {
  2. c.gridx=0;
  3. c.gridy=i;
  4. leftButtonPanel.add(listOfButtons.get(i));
  5. }
  1. the result is the following
  2. [![enter image description here][1]][1]
  3. and after adding the buttons
  4. [![enter image description here][2]][2]
  5. I have also tried setting the JPanel to a gridlayout

leftButtonPanel.setLayout(new GridLayout(listOfButtons.size(),1));

for(int i = 0; i<listOfButtons.size();i++) {
leftButtonPanel.add(listOfButtons.get(i));
}

  1. [![before adding][3]][3]
  2. [![enter image description here][4]][4]
  3. [1]: https://i.stack.imgur.com/lui0I.png
  4. [2]: https://i.stack.imgur.com/VAz9H.png
  5. [3]: https://i.stack.imgur.com/EpWJA.png
  6. [4]: https://i.stack.imgur.com/t6BE3.png
  7. The buttons &quot;see all&quot; and &quot;add&quot; are all in the same listOfButtons arraylist. and the only way to add buttons into the panel is through that forloop. for some reasons the buttons still start off horizontally.
  8. </details>
  9. # 答案1
  10. **得分**: 1
  11. 对于 `GridBagLayout`,不要忘记,你需要同时提供 `GridBagConstraints`,否则它的行为会很像 `FlowLayout`
  12. ```java
  13. import java.awt.EventQueue;
  14. import java.awt.GridBagConstraints;
  15. import java.awt.GridBagLayout;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JPanel;
  21. public class SoTest {
  22. public static void main(String[] args) {
  23. new SoTest();
  24. }
  25. public SoTest() {
  26. EventQueue.invokeLater(new Runnable() {
  27. @Override
  28. public void run() {
  29. JFrame frame = new JFrame();
  30. frame.add(new TestPane());
  31. frame.pack();
  32. frame.setLocationRelativeTo(null);
  33. frame.setVisible(true);
  34. }
  35. });
  36. }
  37. public class TestPane extends JPanel {
  38. public TestPane() {
  39. setLayout(new GridBagLayout());
  40. GridBagConstraints gbc = new GridBagConstraints();
  41. gbc.gridwidth = GridBagConstraints.REMAINDER;
  42. gbc.fill = GridBagConstraints.HORIZONTAL;
  43. List<JButton> listOfButtons = new ArrayList<>(5);
  44. for (int i = 0; i < 10; i++) {
  45. listOfButtons.add(new JButton(Integer.toString(i)));
  46. }
  47. for (int i = 0; i < listOfButtons.size(); i++) {
  48. add(listOfButtons.get(i), gbc);
  49. }
  50. }
  51. }
  52. }

此时,我好奇你是否应该考虑使用 JList

英文:

For GridBagLayout, don't forget, you need to supply the GridBagConstraints as well, otherwise it act a lot like a FlowLayout

无法垂直添加按钮。

  1. import java.awt.EventQueue;
  2. import java.awt.GridBagConstraints;
  3. import java.awt.GridBagLayout;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. public class SoTest {
  10. public static void main(String[] args) {
  11. new SoTest();
  12. }
  13. public SoTest() {
  14. EventQueue.invokeLater(new Runnable() {
  15. @Override
  16. public void run() {
  17. JFrame frame = new JFrame();
  18. frame.add(new TestPane());
  19. frame.pack();
  20. frame.setLocationRelativeTo(null);
  21. frame.setVisible(true);
  22. }
  23. });
  24. }
  25. public class TestPane extends JPanel {
  26. public TestPane() {
  27. setLayout(new GridBagLayout());
  28. GridBagConstraints gbc = new GridBagConstraints();
  29. gbc.gridwidth = GridBagConstraints.REMAINDER;
  30. gbc.fill = GridBagConstraints.HORIZONTAL;
  31. List&lt;JButton&gt; listOfButtons = new ArrayList&lt;&gt;(5);
  32. for (int i = 0; i &lt; 10; i++) {
  33. listOfButtons.add(new JButton(Integer.toString(i)));
  34. }
  35. for (int i = 0; i &lt; listOfButtons.size(); i++) {
  36. add(listOfButtons.get(i), gbc);
  37. }
  38. }
  39. }
  40. }

At this point, I'm curious as to if you should be considering a JList instead

huangapple
  • 本文由 发表于 2020年9月5日 09:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63749798.html
匿名

发表评论

匿名网友

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

确定