0

我使用 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 的部分。我希望 JPanel 在它们的位置保持静止,并且如果有可用空间则不对齐。如果我不清楚,我可以上传一些快照。谢谢!

4

1 回答 1

2

GridBagLayout围绕容器的中心布局其组件,这是它(有时是烦人的)默认功能。

您可以尝试在容器中其他组件的右侧和下方添加一个空的“填充”组件(例如 a JLabel) 。这将迫使它们到容器的上/左角...GridBagConstraintsweightx=1weighty=1

更新...

居中/默认...

在此处输入图像描述

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

对齐...

在此处输入图像描述

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);
        }
    }        
}
于 2013-08-27T06:58:09.827 回答