gridbaglayout を使用して JLabel (サムネイルの数) といくつかのサムネイルを表示しています。gridx 値は正しく機能していますが、gridy 値は完全に無視されます...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
public class grid {
private JFrame frame = new JFrame();
private JLabel a = new JLabel("Welcome to PhotoAlbum55");
/**
* Create the frame.
*/
public grid() {
createAndShowGUI();
}
private void addComponentsToPane(Container pane) {
SpringLayout layout = new SpringLayout();
pane.setLayout(layout);
layout.putConstraint(SpringLayout.WEST, a, 60, SpringLayout.WEST, pane);
layout.putConstraint(SpringLayout.NORTH, a, 0, SpringLayout.NORTH, pane);
a.setFont(new Font("Segoe UI", Font.BOLD, 20));
pane.add(a);
JPanel photoPanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
photoPanel.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
JScrollPane photoScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
photoScroll.setViewportView(photoPanel);
pane.add(photoScroll);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.ipadx = 15;
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel photo = new JLabel();
Image img = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg").getImage();
Image newimg = img.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(newimg);
photo.setIcon(newIcon);
gbl.setConstraints(photo, gbc);
photoPanel.add(photo);
JLabel photo1 = new JLabel();
Image img1 = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg").getImage();
Image newimg1 = img1.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon1 = new ImageIcon(newimg1);
photo1.setIcon(newIcon1);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.ipadx = 15;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbl.setConstraints(photo1, gbc);
photoPanel.add(photo1);
// photoPanel.setPreferredSize(new Dimension(900, 900));
photoScroll.setPreferredSize(new Dimension(500, 500));
layout.putConstraint(SpringLayout.WEST, photoScroll, 60, SpringLayout.WEST, a);
layout.putConstraint(SpringLayout.NORTH, photoScroll, 100, SpringLayout.NORTH, a);
}
private void createAndShowGUI() { // Creating the GUI...
frame.getContentPane().setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("PhotoAlbum55");
// Set up the content pane.
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setSize(690, 622);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new grid();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}