java布局

1aaf6o9v  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(363)

这是我的密码

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);

它只是删除了水平滚动条,但图片仍然在一行结束。
我不知道怎么了。谢谢你的帮助!

q5iwbnjs

q5iwbnjs1#

所以你把所有的图像都放在 Mekanik 面板,使用 FlowLayout . 正如java教程所说,

The FlowLayout class puts components in a row, sized at their preferred size

这显然不是你想要的。所以你必须改变面板的布局。
在我看来 GridLayout 更适合你的问题。

相关问题