有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法正确获取JLabels allign

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

  1. # 1 楼答案

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