问题
im using GridBag to display some JPanels with images inside a JScrollPane. When there are 3 or more images the GridBagConstraints work ok but when i have 1 or 2, they get aligned to the center of the JScrollPane instead of being in their position in the top (like in a gallery) Here is my code:
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);
I have omitted the part where i assign an image to the photo Jpanel. I want the JPanels to stay static in their places and do not align if there is free space. If im not being clear i can upload a few snaps. Thanks!
回答1:
GridBagLayout
layouts its components out around the center of the container, this is it's (and sometimes annoying) default functionality.
You could try adding an empty "filler" component (say a JLabel
) with the GridBagConstraints
of weightx=1
and weighty=1
at a position right of and below the other components in the container. This will force them to the top/left corner of the container...
Updated...
Centered/default...
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);
}
}
}
Aligned...
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);
}
}
}
来源:https://stackoverflow.com/questions/18458887/incorrect-jpanel-position-in-gridbaglayout