如何将 JLabel 添加到我的 ScrollPane 中的特定位置?

how do I add a JLabel to a specific location into my ScrollPane?

我正在尝试在我的 JPanel 中显示图像 and/or 标签。问题是,我想在自定义位置显示许多 JLabel,但目前我只能显示 4 个标签。我知道我需要使用 ScrollPane。我试过使用它,但它不允许我将它们与海关位置联系起来。 (我所说的自定义位置是指使用坐标来放置我的标签。)

这是不使用 ScrollPane 的代码:

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class Images extends JFrame{
    private JPanel contentPane;
    public static void main(String[] args) {
        Images image=new Images();
    }
    public Images(){
        setVisible(true);
        setTitle("myTitle");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 800);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblNombreequipo = new JLabel("trying: ");
        lblNombreequipo.setBounds(147, 110, 145, 14);
        contentPane.add(lblNombreequipo);
        int coordY=150;
        for(int i=0;i<10;i++){
            JLabel myLabel = new JLabel("attempt: "+i);
            myLabel.setBounds(147,coordY, 145, 14);
            contentPane.add(myLabel);
            coordY=coordY+150;
        }

    }

}

Java GUI 必须在不同的 OS'、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them along with layout padding and borders for white space.

这是一个例子。调整布局填充和组件边框参数以提供 'exact' 要求并注意代码中的进一步注释。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Images extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        new Images();
    }

    public Images() {
        setTitle("myTitle");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setBounds(100, 100, 800, 800); call pack() instead
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        //contentPane.setLayout(null); // never do that!
        JLabel lblNombreequipo = new JLabel("trying: ");
        lblNombreequipo.setBorder(new EmptyBorder(10, 40, 10, 10));
        //lblNombreequipo.setBounds(147, 110, 145, 14);
        contentPane.add(lblNombreequipo, BorderLayout.PAGE_START);
        //int coordY = 150;
        JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
        contentPane.add(new JScrollPane(scrollPanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
        for (int i = 0; i < 10; i++) {
            JLabel myLabel = new JLabel("attempt: " + i);
            myLabel.setBorder(new EmptyBorder(20, 140, 20, 140));
            //myLabel.setBounds(147, coordY, 145, 14);
            scrollPanel.add(myLabel);
            //coordY = coordY + 150;
        }
        pack();
        // now the preferred size has been calculated, we can shorten the height
        Dimension d = getSize();
        setSize(new Dimension(d.width, 250));
        setLocationByPlatform(true);
        setMinimumSize(getSize());
        setVisible(true); //should be last
    }
}