Java 我需要在添加新组件时动态调整 JPanel 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18491408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I need to make my JPanel resize dynamically as new components are being added to it
提问by Ivan ?eli?anin
I need to let users add more text fields to my JFrame so once the size of the frame has exceeded its original value a scroll pane would step in. Since I cannot add JScrollPane to JFrame in order to enable scrolling I decided to put the JPanel on the JFrame and pass the JPanel object into the JScrollPane constructor. Scrolling now works fine but only until it has reached the borders of the JPanel. The thing is the size of JPanel stays as is and is not stretching dynamically. What happens is the buttons in my code are using up all the space of the JPanel being the size of 300x300 but what I want to do is have JPanel stretch once these controls have used up its original space. Please advise.
我需要让用户向我的 JFrame 添加更多文本字段,因此一旦框架的大小超过其原始值,滚动窗格就会介入。由于我无法将 JScrollPane 添加到 JFrame 以启用滚动,因此我决定将 JPanel 放在JFrame 并将 JPanel 对象传递给 JScrollPane 构造函数。滚动现在可以正常工作,但只能直到它到达 JPanel 的边界。问题是 JPanel 的大小保持原样,不会动态拉伸。发生的情况是我的代码中的按钮用完了 300x300 大小的 JPanel 的所有空间,但是我想要做的是在这些控件用完其原始空间后让 JPanel 拉伸。请指教。
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
public class Skrol {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(400,400));
JScrollPane jsp = new JScrollPane(p);
jsp.setPreferredSize(new Dimension(300,300));
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
for(int i=0;i<100;i++)
{
JButton b = new JButton("Button "+i);
p.add(b);
}
f.add(jsp);
f.setSize(new Dimension(600,600));
f.setLocation(300, 300);
f.setVisible(true);
}
}
采纳答案by Flo
I changed the Layout in your JPanel
to GridLayout, so the Size of it is just handeld by the Layoutmanager depending on the components on the panel.
我将您的 Layout 更改JPanel
为GridLayout,因此它的大小仅由 Layoutmanager 根据面板上的组件处理。
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new GridLayout(0, 5));
JScrollPane jsp = new JScrollPane(p);
jsp.setPreferredSize(new Dimension(300,300));
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
for (int i = 0; i < 100; i++) {
JButton b = new JButton("Button " + i);
p.add(b);
}
f.add(jsp, BorderLayout.CENTER);
f.setLocation(300, 300);
f.setVisible(true);
f.pack();
回答by cswine
Choose the Miglayout option under Layouts. i found this to be the easiest way
选择布局下的 Miglayout 选项。我发现这是最简单的方法