无法垂直添加按钮。

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

unable to add buttons vertically

问题

我正在编写一个程序其中的JButton以垂直方式动态添加到JPanel中按钮存储在ArrayList中)。我尝试了以下代码通过将JPanel设置为GridBagLayout布局

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

结果如下所示:

无法垂直添加按钮。

添加按钮后的效果如下:

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

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

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

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

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

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


[1]: https://i.stack.imgur.com/lui0I.png
[2]: https://i.stack.imgur.com/VAz9H.png
[3]: https://i.stack.imgur.com/EpWJA.png
[4]: https://i.stack.imgur.com/t6BE3.png

<details>
<summary>英文:</summary>

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.

	for(int i = 0; i&lt;listOfButtons.size();i++) {
		c.gridx=0;
		c.gridy=i;
		leftButtonPanel.add(listOfButtons.get(i));
	}

the result is the following

[![enter image description here][1]][1]

and after adding the buttons

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


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));
}

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


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


  [1]: https://i.stack.imgur.com/lui0I.png
  [2]: https://i.stack.imgur.com/VAz9H.png
  [3]: https://i.stack.imgur.com/EpWJA.png
  [4]: https://i.stack.imgur.com/t6BE3.png

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.



</details>


# 答案1
**得分**: 1

对于 `GridBagLayout`,不要忘记,你需要同时提供 `GridBagConstraints`,否则它的行为会很像 `FlowLayout`。

```java
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SoTest {

    public static void main(String[] args) {
        new SoTest();
    }

    public SoTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            List<JButton> listOfButtons = new ArrayList<>(5);
            for (int i = 0; i < 10; i++) {
                listOfButtons.add(new JButton(Integer.toString(i)));
            }

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

    }
}

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

英文:

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

无法垂直添加按钮。

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SoTest {

    public static void main(String[] args) {
        new SoTest();
    }

    public SoTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            List&lt;JButton&gt; listOfButtons = new ArrayList&lt;&gt;(5);
            for (int i = 0; i &lt; 10; i++) {
                listOfButtons.add(new JButton(Integer.toString(i)));
            }

            for (int i = 0; i &lt; listOfButtons.size(); i++) {
                add(listOfButtons.get(i), gbc);
            }
        }

    }
}

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:

确定