如何在Java中的MigLayout中强制组件增长
我有一个由各种组件组成的组件,例如复选框、滑块和一些按钮。我想将其添加到滚动窗格并让滑块增长以填充所有剩余空间。正如此代码所示,这没有问题:
public static void main(String[] args) {
JFrame f = new JFrame("Test");
JPanel c = new JPanel(new MigLayout(
"",
"[]5[]10[grow]10[]0[]0[]0[]",
"[]"
));
c.add(new JCheckBox(""));
c.add(new JLabel("Name"));
c.add(new JSlider());
c.add(new JButton("1"));
c.add(new JButton("2"));
c.add(new JButton("3"));
c.add(new JButton("4"));
f.getContentPane().add(new JScrollPane(c));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
如果运行此代码并调整框架大小,滑块将填充所有空间。我的问题在于,我想将我的组件(带有滑块的组件)添加到包含该组件的不同实例的另一个组件中。例如,它将包含 3 或 4 个组件,其中滑块一个在另一个之下。我以为这会起作用:
public static void main(String[] args) {
JFrame f = new JFrame("Test");
JPanel c = new JPanel(new MigLayout(
"",
"[]5[]10[grow]10[]0[]0[]0[]",
"[]"
));
c.add(new JCheckBox(""));
c.add(new JLabel("Name"));
c.add(new JSlider());
c.add(new JButton("1"));
c.add(new JButton("2"));
c.add(new JButton("3"));
c.add(new JButton("4"));
JPanel a = new JPanel(new MigLayout("wrap 1"));
a.add(c);
f.getContentPane().add(new JScrollPane(a));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
但事实并非如此。关于为什么以及如何解决它有什么想法吗?
I have a component that is made up of various components such as a checkbox a slider and some buttons. i want to add this to a scrollpane and have the slider grow to fill all the remaining space. This is no problem as this code demonstrates :
public static void main(String[] args) {
JFrame f = new JFrame("Test");
JPanel c = new JPanel(new MigLayout(
"",
"[]5[]10[grow]10[]0[]0[]0[]",
"[]"
));
c.add(new JCheckBox(""));
c.add(new JLabel("Name"));
c.add(new JSlider());
c.add(new JButton("1"));
c.add(new JButton("2"));
c.add(new JButton("3"));
c.add(new JButton("4"));
f.getContentPane().add(new JScrollPane(c));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
If you run this and resize the frame, the slider fills all the space. My problem lies in the fact that i want to add my component (the one with the slider) in another component that contains different instances of this one. So for instance it will contain 3 or 4 components with sliders one below the other. I thought this would work :
public static void main(String[] args) {
JFrame f = new JFrame("Test");
JPanel c = new JPanel(new MigLayout(
"",
"[]5[]10[grow]10[]0[]0[]0[]",
"[]"
));
c.add(new JCheckBox(""));
c.add(new JLabel("Name"));
c.add(new JSlider());
c.add(new JButton("1"));
c.add(new JButton("2"));
c.add(new JButton("3"));
c.add(new JButton("4"));
JPanel a = new JPanel(new MigLayout("wrap 1"));
a.add(c);
f.getContentPane().add(new JScrollPane(a));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
But it doesn't. Any thoughts as to why and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
JPanel a
声明更改为:Try changing the
JPanel a
declaration to: