简体   繁体   English

Java在线程循环中将Click侦听器添加到JButton

[英]Java Add Click Listener to JButton in Loop in Thread

I have a loop, in which several JLabel's are created, containing a link to an image. 我有一个循环,其中创建了几个JLabel,其中包含指向图像的链接。

For each JLabel, there is a JButton created. 对于每个JLabel,都会创建一个JButton。

The behaviour I'm looking for is that for each JButton, to add a click listener, which fires a method. 我正在寻找的行为是为每个JButton添加一个单击侦听器,从而触发一个方法。

The first issue is that it seems as though I am unable to add a Listener within the thread. 第一个问题是好像我无法在线程中添加侦听器。

The second issue is that I don't see how I can specify each JButton to fire the click listener with a different value. 第二个问题是,我看不到如何指定每个JButton来触发具有不同值的Click侦听器。

I'm looking for something similar to the following (obviously onClick method doesn't exist): 我正在寻找类似于以下内容(显然onClick方法不存在):

Thread thread = new Thread(new Runnable()
{
    public void run()
    {
        JPanel mainPanel = new JPanel();
        for (int counter = 0; counter < imageSources.size(); counter++)
        {
            JLabel imageSource = imageSources.get(counter);
            // JButton saveToDisk = new JButton("Save Image");
            // saveToDisk.onClick(saveFavourite(imageSources.get(counter)));
            mainPanel.add(imageSource);
            // mainPanel.add(saveToDisk);
        }

.
.
.

public void saveFavourite(String imageUrl)
{
    BufferedImage image = null;
    try
    {
        URL url = new URL(imageUrl);
        image = ImageIO.read(url);
        ImageIO.write(image, "jpg", new File("/Users/USERNAME/Desktop/" + webPage.getMemeId(imageUrl) + ".jpg"));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

This is a simple example of creating buttons and adding to them ActionListeners that take String arguments during a loop 这是创建按钮并向其添加在循环期间采用String参数的ActionListener的简单示例

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

    private final JLabel[] label = new JLabel[5];
    private final JButton[] button = new JButton[5];

    public Test() {
        JPanel mainPanel = new JPanel(new GridLayout(0,2));
        for (int counter = 0; counter < label.length; counter++) {
            label[counter] = new JLabel("Label " + counter);
            button[counter] = new JButton("Button " + counter);
            button[counter].addActionListener(new MyActionListener("Row " + counter));
            mainPanel.add(label[counter]);
            mainPanel.add(button[counter]);
        }
        this.add(mainPanel);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Test run = new Test();
                run.pack();
                run.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                run.setVisible(true);
            }
        });
    }

    private class MyActionListener implements ActionListener {

        String text;

        public MyActionListener(String text) {
            this.text = text;
        }

        public void actionPerformed(ActionEvent e) {
            System.out.println(text);
        }
    }
}

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

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