gridbagconstraints无法正常工作

时间:2013-04-12 19:54:36

标签: java swing gridbaglayout

我正在使用gridbaglayout来显示JLabel(缩略图的数量)和一些缩略图,每当我将缩略图添加到Jpanel并使用gridbaglayout时,它会在中心显示忽略网格值。 gridx值正常工作,但网格值完全被忽略......

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();
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

两个图像位于相同的y坐标上,因此应用程序的行为符合预期。另一方面,x坐标不同,因此您需要设置

gbc.weightx = 1;

让他们定位。如果您需要将图像添加到面板顶部,则需要锚定并沿y轴设置权重:

gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;