java - FlowLayout添加在之前的组件下

标签 java swing jpanel flowlayout jseparator

我正在尝试将一些组件添加到 flowLayout 中的 JPanel

JPanel panelPrincipal = new JPanel();
principale.getContentPane().add(panelPrincipal, BorderLayout.CENTER);
panelPrincipal.setLayout(new BorderLayout(0, 0));

JPanel panelDynamique = new JPanel();
JScrollPane jScrollPane = new JScrollPane(panelDynamique);
panelPrincipal.add(jScrollPane, BorderLayout.CENTER);

if(this.getNbAppFor(champs.get(6)) > 0) {
    JLabel lblMAJ = new JLabel(champs.get(6));
    panelDynamique.add(lblMAJ);
    panelDynamique.add(new JSeparator());

    JPanel panelMAJ = new JPanel();
    panelMAJ.setLayout(new GridLayout(this.getNbAppFor(champs.get(6))/2, 2));
    panelDynamique.add(panelMAJ);
}

if(this.getNbAppFor(champs.get(7)) > 0) {
    JLabel lblDispo = new JLabel(champs.get(7));
    panelDynamique.add(new JSeparator());

    JPanel panelDispo = new JPanel();
    panelDispo.setLayout(new GridLayout(this.getNbAppFor(champs.get(7))/2, 2));
    panelDynamique.add(panelDispo);
}

这是核心构建,但它看起来并不像我预期的那样。

我举例说明了我正在尝试做的事情:

Result Expected

如何将组件添加到上一个组件下的下一个“行”中的 FlowLayout 中?如何让分隔符填充 Container 的宽度?

最佳答案

按照你的图画,但没有使用FlowLayout。

package testp;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class LayTest {

    public static void main(String[] args) {

        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST;
        c.weightx = 1;
        p.add(new JLabel("Mide a jour"), c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(new JSeparator(), c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.weighty = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(new JPanel(), c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(new JSeparator(), c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 4;
        c.anchor = GridBagConstraints.WEST;
        p.add(new JLabel("Disponsible"), c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 5;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(new JSeparator(), c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 6;
        c.weighty = 1;
        p.add(new JPanel(), c);


        JFrame f = new JFrame();
        f.setSize(300, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(p);
        f.setVisible(true);

    }

}

你可能会发现有用 oracle

关于java - FlowLayout添加在之前的组件下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34709887/

相关文章:

java - 文本字段内的文本字段

java - JTextField 在 BoxLayout 中垂直扩展

java - 如何在 JPanel 中设置 JButton 的大小

java - 在java swing中调整窗口大小时如何使按钮粘在一个地方?

java - 自动选择下一个面板 JTabbedPane

Java 7 Web 启动问题与 list 文件

java - 我可以将 Swing 对象设置为与其底层容器的大小成比例吗?

java - 将 JPanel 转换为 JScrollPane 中的图像

java - jboss + Metro + ejb3= java.lang.NoClassDefFoundError : Ljavax/servlet/http/HttpServletRequest

java - 如何修复此错误 "GC overhead limit exceeded in Eclipse"