如何使JLabel成为图像图标的完整大小? [英] How to make JLabels that are the full size of the image icon?

查看:355
本文介绍了如何使JLabel成为图像图标的完整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使我的图像全尺寸而不仅仅放大中心?

我的图像名为1-12.jpg。

How do I make my images full size and not just zoomed in on the center?
My images are named 1-12.jpg.

这是我的代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class GuiApp extends JFrame{

  public static void main(String [] args){

     final JFrame shirts = new JFrame("T-shirts");

        JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));

        for (int i = 1; i < 13; i++) {
            JLabel l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);
            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            l.setFont(l.getFont().deriveFont(20f));
            panel.add(l);
        }//end of for loop

        shirts.setContentPane(panel);
        shirts.setSize(1000, 1000);
        shirts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        shirts.setVisible(true);

  }//end of method
}//end of class


推荐答案


如何使我的图像全尺寸而不是放大中心?

How do I make my images full size and not just zoomed in on the centre?

由此我假设您的意思是如何缩小图像以使其完全显示在网格上的可用空间中?

By this I assume you mean how do you shrink the image so that it is completely displayed in the space available on the grid?

查看拉伸图标(或 ShrinkIcon 取决于哪个更符合您的要求)。随着标签大小的变化,此图标将动态调整大小。 StretchIcon 允许您按比例缩放图标(默认)或填充可用空间。

Check out Stretch Icon (or ShrinkIcon depending on which meets your requirement better). This icon will dynamically resize as the size of the label changes. The StretchIcon allows you to scale the icon proportionally(default) or to fill the available space.

由于 GridLayout 使所有组件的大小相同,图标的大小也相同。

Since a GridLayout makes all components the same size the icons will also be the same size.

基本代码应为:

//JLabel l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);
StretchIcon icon = new StretchIcon("T-shirts/"+i+".jpg");
JLabel l = new JLabel(icon, JLabel.CENTER);

这篇关于如何使JLabel成为图像图标的完整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆