GridBagLayout中的JPanel位置不正确

时间:2013-08-27 06:54:39

标签: java swing jpanel gridbaglayout scrollpane

我正在使用GridBag在JScrollPane中显示一些带有图像的JPanel。 当有3个或更多图像时,GridBagConstraints工作正常但是当我有1或2时,它们与JScrollPane的中心对齐而不是在顶部的位置(如在图库中) 这是我的代码:

JPanel jPanel1 = new JPanel();
GridBagLayout layout = new GridBagLayout();
jPanel1.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();

JPanel photo = new JPanel();
Dimension d = new Dimension(100,100);
photo.setPreferredSize(d);
gbc.insets = new Insets(0,3,3,3);

gbc.gridx = i;
gbc.gridy = j;
jPanel1.add(photo, gbc);


jScrollPane1.setViewportView(jPanel1);

我省略了将图像指定给照片Jpanel的部分。 我希望JPanels在他们的位置保持静态,如果有空闲空间则不对齐。 如果我不清楚我可以上传几个快照。 谢谢!

1 个答案:

答案 0 :(得分:2)

GridBagLayout将其组件布局在容器的中心周围,这是(有时令人讨厌的)默认功能。

您可以尝试添加一个空的“填充”组件(例如JLabel),GridBagConstraints weightx=1weighty=1位于另一个右下方的位置容器中的组件。这将迫使他们到容器的顶部/左角......

<强>更新...

<强>居中/默认...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GridBagLayoutTest {

    public static void main(String[] args) {
        new GridBagLayoutTest();
    }

    public GridBagLayoutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(600, 600);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;

            JLabel picture = new JLabel();
            try {
                picture.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/your/picture"))));
            } catch (IOException ex) {
                ex.printStackTrace();
                picture.setText("Bad picture");
            }
            add(picture, gbc);
        }
    }        
}

<强>对齐...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GridBagLayoutTest {

    public static void main(String[] args) {
        new GridBagLayoutTest();
    }

    public GridBagLayoutTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.setSize(600, 600);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;

            JLabel picture = new JLabel();
            try {
                picture.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/your/picture"))));
            } catch (IOException ex) {
                ex.printStackTrace();
                picture.setText("Bad picture");
            }
            add(picture, gbc);

            JLabel filler = new JLabel();
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            add(filler, gbc);
        }
    }        
}