简体   繁体   English

调整窗口大小时,使用面板中设置的图标调整JLabel的大小

[英]Resizing a JLabel with an setted Icon in a Panel when resizing the Window

I have a certain question about resizing an JLabel with a setted Icon. 我有一个关于使用设置的图标调整JLabel大小的问题。

My Solution was create a Timer which resizes the Label by invoking the actionperformed()-method of an ActionListener-Interface. 我的解决方案是创建一个Timer,通过调用ActionListener接口的actionperformed()方法来调整Label的大小。

Is there any better solution? 有更好的解决方案吗?

package examples;

import images.MyImage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class examples2 {

    private static JFrame frame = new JFrame();
    private static JPanel panel = new JPanel(new BorderLayout());

    private static class ButtonHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            Image image = resizeImage(MyImage.IMAGE_BLUE_BLACKGROUND,
                    panel.getWidth(), panel.getHeight());
            label.setIcon(new ImageIcon(image));

        }

    }

    private static ActionListener buttonhandler = new ButtonHandler();
    private static Timer timer = new Timer(100, buttonhandler);

    private static JLabel label = new JLabel(new ImageIcon(resizeImage(
            MyImage.IMAGE_BLUE_BLACKGROUND, 1600, 100)));

    public static void main(String[] args) {

        JPanel northPanel = new JPanel(new BorderLayout());
        JLabel text = new JLabel("Hakan Kiyar");

        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(northPanel, BorderLayout.NORTH);
        frame.getContentPane().add(new JButton(), BorderLayout.CENTER);
        frame.setVisible(true);

        northPanel.add(Box.createVerticalStrut(10), BorderLayout.NORTH);
        northPanel.add(Box.createHorizontalStrut(10), BorderLayout.WEST);
        northPanel.add(Box.createHorizontalStrut(10), BorderLayout.EAST);
        northPanel.add(Box.createVerticalStrut(10), BorderLayout.SOUTH);
        northPanel.add(panel, BorderLayout.CENTER);

        text.setForeground(Color.YELLOW);

        label.setLayout(new GridBagLayout());
        label.add(text);

        panel.add(label);

        frame.setSize(364, 300);

        timer.start();

    }

    public static Image resizeImage(Image oldImage, int width, int height) {

        Image newImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);

        Graphics g = newImage.getGraphics();

        g.drawImage(oldImage, 0, 0, null);
        g.dispose();

        return newImage;
    }
}

You can dynamically resize the Icon by using Darryl's Stretch Icon class. 您可以使用Darryl的Stretch Icon类动态调整Icon的大小。

It will resize the Icon to fill the space available to the label. 它将调整图标的大小以填充标签可用的空间。 You can configure the class to stretch the Icon to fill the entire space or to respect the width/height ratio as space is made available. 您可以配置该类以将Icon拉伸以填充整个空间,或在空间可用时遵守宽高比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM