英文:
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<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 "see all" and "add" 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<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);
}
}
}
}
At this point, I'm curious as to if you should be considering a JList
instead
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论