如何在Java中的MigLayout中强制组件增长

发布于 2024-08-07 18:46:03 字数 1457 浏览 8 评论 0原文

我有一个由各种组件组成的组件,例如复选框、滑块和一些按钮。我想将其添加到滚动窗格并让滑块增长以填充所有剩余空间。正如此代码所示,这没有问题:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵性 2024-08-14 18:46:03

尝试将 JPanel a 声明更改为:

JPanel a = new JPanel(new MigLayout("wrap 1", "[grow,fill]"));

Try changing the JPanel a declaration to:

JPanel a = new JPanel(new MigLayout("wrap 1", "[grow,fill]"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文