簡體   English   中英

GridBagLayout中的JPanel位置不正確

[英]Incorrect JPanel position in GridBagLayout

我使用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在他們的位置保持靜態,如果有空閑空間則不對齊。 如果我不清楚我可以上傳幾個快照。 謝謝!

GridBagLayout其組件布局在容器的中心周圍,這是它(有時令人討厭的)默認功能。

您可以嘗試在GridBagConstraints中其他組件的右下方位置添加一個空的“填充”組件(比如一個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);
        }
    }        
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM