无法正确对齐JLabel

时间:2013-05-04 13:07:04

标签: java swing jlabel layout-manager

我有一个类别列表,每个类别都有一个图像,我需要一个接一个地显示这些图像(连续4个),它们之间有一些空格。我有问题显示最后一个标签,似乎setBounds方法不影响它。 我创建了一个JPanel,然后将包含图像的所有标签添加到面板中。 这是我的源代码,我还添加了截图链接

谢谢!

    JFrame frame = new JFrame("test");
    JPanel panel = new JPanel();
    panel.setBackground(Color.white);


    java.util.Iterator<Entry<Integer, Y2category>> it = configFile.categories.entrySet().iterator();

    int positionx = 50;
    int positiony = 50;
    int linecounter = 0;

    while( it.hasNext() )
    {
        Map.Entry<Integer, Y2category> pairs = (Entry<Integer, Y2category>) it.next();

        Y2category cat = (Y2category) pairs.getValue();

        JLabel label = new JLabel( new ImageIcon( "img\\main\\black.png" ), JLabel.CENTER );
        label.setBounds(positionx,positiony,115,179);
        label.setFont(new Font("Arial", Font.PLAIN, 14));

        panel.add(label);

        positionx += 220;
        linecounter++;

        if ( linecounter == 4 )
        {
            linecounter = 0;
            positiony += 200;
            positionx = 50;
        }
    }

    frame.add(panel);


    //ImageIcon icon = new ImageIcon("img\\icon.jpg");
    //frame.setIconImage(icon.getImage());
    frame.setResizable( false );

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    frame.setSize(900,900);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);


    frame.setVisible(true);

Screen shot

Screen shot

1 个答案:

答案 0 :(得分:4)

永远不推荐使用null布局。 setBounds()方法将位置设置为静态,不适用于任何动态UI。此外,当您需要在稍后阶段添加组件时,您需要更新大部分代码,即修改受影响组件的setBounds()。

我建议使用GridBagLayout,它非常灵活,您需要设置的只是组件的网格。我写了一个小示例代码来帮助您理解:

public JPanel getComponentPanel()
{
   if(null == componentPanel)
   {
       componentPanel = new JPanel();
       GridBagLayout gridBagLayout = new GridBagLayout();
       componentPanel.setLayout(gridBagLayout);

       // Create a single constraint to be reused
       GridBagConstraints constraint = new GridBagConstraints();
       // Insets is to provide spacing in the format (Top, Left, Bottom, Right)
       constraint.insets = new Insets(10, 10, 10, 10);

       // gridx for x-axis positioning and gridy for y-axis positioning
       constraint.gridx = 0;
       constraint.gridy = 0;
       label1 = new JLabel("Label 1");
       componentPanel.add(label1, constraint);

       constraint.gridx = 1;
       constraint.gridy = 0;
       label2 = new JLabel("Label 2");
       componentPanel.add(label2, constraint);

       constraint.gridx = 2;
       constraint.gridy = 0;
       label3 = new JLabel("Label 3");
       componentPanel.add(label3, constraint);

       constraint.gridx = 3;
       constraint.gridy = 0;
       label4 = new JLabel("Label 4");
       componentPanel.add(label4, constraint);

       constraint.gridx = 0;
       constraint.gridy = 1;
       label5 = new JLabel("Label 5");
       componentPanel.add(label5, constraint);

       .
       .
       .
       .

       constraint.gridx = 3;
       constraint.gridy = 3;
       labelXYZ = new JLabel("Label 5");
       componentPanel.add(labelXYZ, constraint);
   }

   return componentPanel;
}