【问题标题】:Incorrect JPanel position in GridBagLayoutGridBagLayout 中的 JPanel 位置不正确
【发布时间】:2013-08-27 06:54:38
【问题描述】:

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

【问题讨论】:

  • 确保至少一张照片有重量(或者更好,所有照片的重量都相同!= 0),然后使用 GridBagConstraints.anchor = PAGE_START;或 FIRST_LINE_START。

标签: java swing jpanel gridbaglayout scrollpane


【解决方案1】:

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

您可以尝试在容器中其他组件的右侧和下方添加一个空的“填充”组件(例如 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);
        }
    }        
}

【讨论】:

  • 我无法让它工作。另一个问题是我动态添加了 JPanel。
  • 不应该有任何区别(动态添加 JPanel)。您可以简单地移除填充物,添加任何新面板并将填充物放在最后一个位置(右侧/底部)位置
猜你喜欢
  • 2018-02-24
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多