JLabel 调整图像大小

问题描述 投票:0回答:0

当我设置 72 px 宽度和 108 px 高度的图像时,它在 JLabel 中变大,为 135 px x 167 px。 我需要它在滚动窗格中的图块中设置多个图像。 这是代码:

公共类 TileViewPanel 扩展 JPanel {

//Panels and containers
private JPanel tileContainer;
private JScrollPane scrollPane;
private Dimension dim;

//Data Management
private File parentFile;
private ArrayList<File> imageFiles;
private ArrayList<JLabel> imageLabels;
private ArrayList<JPanel> tiles;
private boolean[] viewed;
private int lastSelection;
private Color panelColor;

/** 
 * The quantity of columns that the panel can hold according to its width. 
 */
private int wop;

/** 
 * The quantity of rows that the panel will hold. 
 */
private int hop;

/**
 * The quantity of images already opened when the constructor is called.
 */
private int imgOpened;

//Temporary data to test
private final ImageIcon defThumb;

public TileViewPanel(int width, int height, File parentFile) {
    this.dim = new Dimension(width, height);
    this.parentFile = parentFile;
    this.imageFiles = ImageGetter.getImages(parentFile);
    this.imageLabels = new ArrayList();
    this.tiles = new ArrayList();
    this.lastSelection = -1;
    
    this.defThumb = new ImageIcon("D:\\Projects\\Remi Viewer 2\\icons\\thumb2.jpg"); //This is just a test image
    
    initComponent();
}

/**
 * Initializes the basic settings of this component.
 */
private void initComponent() {
    
    //Setting the basic settings of the panel.
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    this.setPreferredSize(dim);
    this.setName("EXPLORER");
    this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
    this.panelColor = this.getBackground();
    
    //Setting the quantifiers
    wop = Math.round(dim.width / 120);
    hop = (int) imageFiles.size() / wop + 1;
    imgOpened = imgOpened = ((int)dim.height / 140 + 1) * wop;
    
    tileContainer = new JPanel();
    tileContainer.setPreferredSize(new Dimension(wop * 120, hop * 140));
    tileContainer.setLayout(new GridLayout(0, wop, 2, 2));
    tileContainer.setBackground(Color.BLUE);
    
    //Setting the files to be shown
    int ifas = imageFiles.size();
    
    viewed = new boolean[ifas];
    
    for (int i = 0; i < ifas; i ++) {
        
        JPanel tile = new JPanel();
        tile.setPreferredSize(new Dimension(110, 140));
        tile.setLayout(new BoxLayout(tile, BoxLayout.Y_AXIS));
        tile.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
        
        //Image Panel
        JPanel imagePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel imageLabel = new JLabel();
        
        imageLabel.setIcon(defThumb);
        imageLabel.setMaximumSize(new Dimension(108, 108));
        imagePanel.add(imageLabel);
        
        JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel label = new JLabel(imageFiles.get(i).getName());
        label.setAlignmentY(Component.TOP_ALIGNMENT);
        namePanel.add(label);
        
        tile.add(imagePanel);
        tile.add(namePanel);
        
        tileContainer.add(tile);
    }
    
    scrollPane = new JScrollPane();
    scrollPane.setViewportView(tileContainer);
    scrollPane.getVerticalScrollBar().addAdjustmentListener(new ScrollEvent());
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    this.add(scrollPane);
}

/**
 * Changes the tiles once the scroll is moved.
 */
class ScrollEvent implements AdjustmentListener {

    @Override
    public void adjustmentValueChanged(AdjustmentEvent evt) {
         
        int pg = scrollPane.getVerticalScrollBar().getValue(); //Pixels gotten;
        int rowSent = (int)(pg / 169);
        //setImageTiles(rowSent);
    }
}

}

我不想使用图像缩放器,假设tile中的图像已经缩放了。

image jlabel image-resizing imageicon layoutpanels
© www.soinside.com 2019 - 2024. All rights reserved.