0

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

截屏

截屏

4

1 回答 1

4

永远不推荐使用空布局。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;
}
于 2013-05-04T14:30:33.567 回答