Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JScrollPane时的Java布局_Java_Swing_Layout_Jscrollpane - Fatal编程技术网

使用JScrollPane时的Java布局

使用JScrollPane时的Java布局,java,swing,layout,jscrollpane,Java,Swing,Layout,Jscrollpane,这是我的密码 private static KeyEvent e; private static String text1 = null; private static String text = null; public fysikdel() { super("Fysikformler"); setSize(700, 502); setDefaultCloseOperation(EXIT_ON_CLOSE); init(); setResizabl

这是我的密码

private static KeyEvent e;
private static String text1 = null;
private static String text = null;

  public fysikdel() {
    super("Fysikformler");
    setSize(700, 502);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    init();
    setResizable(true);
    setVisible(true);
  }

  public void init() {
    JPanel main = new JPanel();
    JPanel p = new JPanel();
    CardLayout c1 = new CardLayout();

    JScrollPane scrollpane = new JScrollPane(p);

    JPanel Mekanik = new JPanel();
    p.setSize(700, 502);
    Mekanik.setLayout(new FlowLayout());

    //637*237
    ImageIcon likformigrorelsei = new ImageIcon();
    JLabel likformigrorelsel = new JLabel();

    ImageIcon lagesenergii = new ImageIcon();
    JLabel lagesenergil = new JLabel();

    ImageIcon a = new ImageIcon();
    JLabel aa = new JLabel();

    ImageIcon b = new ImageIcon();
    JLabel bb = new JLabel();

    try {
        likformigrorelsei = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }
    likformigrorelsel.setIcon(likformigrorelsei);

    try {
        lagesenergii = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }
    lagesenergil.setIcon(lagesenergii);

    try {
        a = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    aa.setIcon(a);

    try {
        b = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    bb.setIcon(b);

    Mekanik.add(likformigrorelsel);
    Mekanik.add(lagesenergil);
    Mekanik.add(aa);
    Mekanik.add(bb);

    JPanel Tryck = new JPanel();

    main.setLayout(new GridLayout(1,1));
    p.setLayout(c1);

    this.add(main);

    main.add(scrollpane);

    p.add(Mekanik, "1");
    p.add(Tryck, "2");
    c1.show(p, "1");
当我添加更多图片时,我希望它们从左到右填充,直到填充完一行,然后填充下一行。目前,它只是继续填充第一行

如果我加上

 scrollpane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER); 
它只是删除了水平滚动条,但图片仍以一行结束


我不知道怎么了。谢谢你的帮助

因此,您将所有图像放在
Mekanik
面板中,该面板使用
FlowLayout
。作为国家

这显然不是你想要的。因此,您必须更改面板使用的布局


在我看来,
GridLayout
更适合您的问题。

GridLayout无法解决问题,因为您需要对列数进行硬编码。随着帧宽度的改变,每行图像的数量也可能改变。注释中建议的
WrapLayout
允许组件根据需要换行到下一行。
The FlowLayout class puts components in a row, sized at their preferred size