java - GridBagLayout 中的 JPanel 位置不正确

标签 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 的部分。 我希望 JPanel 保持静态,如果有可用空间则不对齐。 如果我不清楚,我可以上传一些快照。 谢谢!

最佳答案

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

您可以尝试使用 weightx=1GridBagConstraints 添加一个空的“填充”组件(例如 JLabel) Weighty=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);
        }
    }        
}

关于java - GridBagLayout 中的 JPanel 位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458887/

相关文章:

java - 代码正在生成灰色屏幕

java - 使用 JFrame 的图形应用程序设计模式

java - 如何在java中返回我的主面板?

java - SetVisible(false) 更改面板中组件的布局

Java/Android - 设置 DecimalFormat 以仅显示尾数

java - 正则表达式:int 不能取消引用

java - 具有多个 JsonSerializer 映射的 DefaultKafkaProducerFactory

Java泛型方法来过滤和查找列表中的第一个元素

java - 如何最好地管理 Java Swing 弹出表单?

java - 如何向 JFrame 添加多个不同大小的 JPanel