我有一个类别列表,每个类别都有一个图像,我需要将这些图像一个接一个地显示(连续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

最佳答案

永远不建议使用空布局。 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;
}

09-25 22:21