簡體   English   中英

Java可調整大小的JLabel圖標不斷變大

[英]Java Resizable JLabel icon just keeps getting bigger

(編輯2)大家好,我沒有很多時間,所以我將快速發布,我在一個較小的程序中發生了問題:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JToolBar;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.Color;
import javax.swing.border.LineBorder;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class Main extends JFrame {

private JPanel contentPane;
private JTextField txtWhatTheHell;

private static ArrayList<ImageIcon> thumbslist = new ArrayList<ImageIcon>();
public static DefaultListModel model = new DefaultListModel();


private static void initialize_pix() throws IOException
{
    File filefolder = new File("pix");
    File[] pictures = filefolder.listFiles();


    for(File a: pictures)
    {
        Image tempimage = ImageIO.read(a);
        ImageIcon perimage = new ImageIcon(tempimage);

        thumbslist.add(perimage);
    }
    model.addElement(filefolder.toString());
}

public static ImageIcon resize(ImageIcon source, JLabel label)
{

    int height = label.getHeight();
    int width = label.getWidth();

    Image original = source.getImage();

    BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = resized.createGraphics();

    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics.drawImage(original, 0, 0, width, height, null);
    graphics.dispose();

    ImageIcon result = new ImageIcon(resized);

    return result;
}







public static void main(String[] args) {
    try {
        initialize_pix();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Main() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 731, 563);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JScrollPane scrollPane = new JScrollPane();

    txtWhatTheHell = new JTextField();
    txtWhatTheHell.setText("Text");
    txtWhatTheHell.setEditable(false);
    txtWhatTheHell.setColumns(10);

    JToolBar toolBar = new JToolBar();
    toolBar.setFloatable(false);

    JLabel label = new JLabel("");
    label.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent a) {


            label.setIcon(resize(thumbslist.get(0), label));
        }
    });
    label.setBorder(new LineBorder(new Color(0, 0, 0)));
    label.setBackground(Color.GRAY);
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                    .addComponent(toolBar, GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
                    .addGroup(gl_contentPane.createSequentialGroup()
                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 491, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                            .addComponent(label, GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(txtWhatTheHell, GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))))
                .addContainerGap())
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.TRAILING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                    .addGroup(gl_contentPane.createSequentialGroup()
                        .addComponent(txtWhatTheHell, GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 239, GroupLayout.PREFERRED_SIZE))
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE))
                .addContainerGap())
    );

    JList list = new JList(model);
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent a) {

            if(list.isSelectionEmpty() == true)
            {
                return;
            }
            else
            {
                label.setIcon(thumbslist.get(0));
            }


        }
    });
    scrollPane.setViewportView(list);
    contentPane.setLayout(gl_contentPane);
}
}

開頭的文件是圖片文件,我真的不確定這是怎么回事

(編輯3)在每個實例調用調整大小偵聽器之后,我都做了一個延遲,並且第一次,它正確地調整了大小,然后看來它開始失去控制了

我認為@HovercraftFullOfEels就是這么想的,但是我只是不知道該怎么做才能在每次調整框架大小時只調整一次

您已經在組件偵聽器中進行了某種遞歸。 由於您在JLabel自己的ComponentListener中增加了JLabel圖標的大小,因此偵聽器將收到大小變化的通知,這會增加圖標的大小,從而將大小更改通知偵聽器。

一種可能的解決方案:考慮關閉自身內部的偵聽器,以免發生這種遞歸,然后再次將其重新打開。 一種方法是簡單地刪除監聽器,然后在其內部重新添加監聽器。

例如,

Label thumbchanger = new JLabel();
thumbchanger.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent a) {
        // remove the *this* component listener before resizing
        thumbchanger.removeComponentListener(this);
        Mod current_mod = Globals.modList.get(list.getSelectedIndex());
        ImageIcon new_icon = Globals.resize(current_mod.get_current(), thumbchanger);
        thumbchanger.setIcon(new_icon);
        // re-add the component listener after done resizing
        thumbchanger.addComponentListener(this);
    }
});

另一種方法是使用在其中設置的偵聽器本地的布爾字段。

thumbchanger.addComponentListener(new ComponentAdapter() {
    private boolean resizing = false;

    @Override
    public void componentResized(ComponentEvent a) {
        if (resizing) {
            return;
        }

        resizing = true;

        Mod current_mod = Globals.modList.get(list.getSelectedIndex());
        ImageIcon new_icon = Globals.resize(current_mod.get_current(), thumbchanger);
        thumbchanger.setIcon(new_icon);

        resizing = false;
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM