有没有办法在JButton中存储数据? [英] Is there a way to store data in a JButton?

查看:158
本文介绍了有没有办法在JButton中存储数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序目前将图像添加到列表中,然后创建一个动态存储缩略图的Jbutton。喜欢发现 here 。虽然图像是按用户选择的方式添加的。



我遇到的问题是,我无法将图像更改为所选的缩略图,因为它们未链接到列表中的图像。有没有办法可以将图像的索引号存储在按钮中,所以当我点击它时,我知道要在列表中显示哪个图像?还是还有另一个更聪明的方法?谢谢。

  //创建缩略图
private void createThumbnail(ImpImage image){
Algorithms a = new算法();
ImpImage thumb = new ImpImage();
//创建缩略图
thumb.setImg(a.shrinkImage(image.getImg(),75,75));
//创建ImageIcon
ImageIcon icon = new ImageIcon(thumb.getImg());
//创建JButton
JButton iconButton =新的JButton(图标);
//创建ActionListener
iconButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
bottomBarLabel.setText(Clicked );
imagePanel.removeAll();
imagePanel.add(images.get(position)); //需要修复
imagePanel.revalidate();
}
});
//添加到previewPanel
previewPanel.add(iconButton);
previewPanel.revalidate();
previewPanel.repaint();
}

我实现这个的方式是:1)打开图像,2)添加图像到列表,3)创建图像的缩略图 - >添加到ImageIcon - >添加到Jbutton - >添加到组件w / actionListener。问题是我不将按钮存储在列表中,因此它不清楚列表中各自图像的数量。

解决方案

您可以尝试的一种方法是在按钮创建时设置动作命令。 这个java教程演示了这个概念是更详细的。



在您的情况下,您可以这样设置:

  final JButton iconButton =新的JButton(图标); 
iconButton.setActionCommand(some_unique_identifying_information);

您可以在 actionPerformed 回调如下:

  @Override 
public void actionPerformed(ActionEvent e){
String actionCommand = iconButton .getActionCommand();
// ...其他方法
}

请注意使用iconButton上的 final 从你的代码看,触发事件的按钮可以通过action listener的关闭来访问。如果您愿意或不能使用上述方法,您可以像这样访问按钮:

  @Override 
public void actionPerformed(ActionEvent e){
JButton button =(JButton)e.getSource();
String actionCommand = button.getActionCommand();
// ...其他方法
}

与Christian的建议类似在评论中,如果您愿意,您可以维护一个 Map 或其他数据结构,其中您的唯一操作命令与图像相关。您的地图可能看起来像 Map< String,ImpImage> (假设 ImpImage 是您的图标的类) p>

或者,action命令可以是索引值的字符串表示形式。虽然它必须存储在 String 表单中,一旦您通过 getActionCommand()获取它,您可以将其解析回一个使用的数字 Integer.parseInt(String)


My program currently adds an image to a List, which it then creates a Jbutton dynamically storing a thumbnail. Like found here. although the images are added as the user selects them.

The issue I am having is that I cannot get the image to change to the thumbnail that is selected as they are not linked to the images in the list. Is there a way I can store the index number of the image in the button, so when I click it I know which image to show in the List? Or is there another more intelligent way? Thanks.

 //Create thumbnail
    private void createThumbnail(ImpImage image){
        Algorithms a = new Algorithms();
        ImpImage thumb = new ImpImage();
        //Create Thumbnail
        thumb.setImg(a.shrinkImage(image.getImg(), 75, 75));
        //Create ImageIcon
        ImageIcon icon = new ImageIcon(thumb.getImg());
        //Create JButton
        JButton iconButton = new JButton(icon); 
        //Create ActionListener
        iconButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bottomBarLabel.setText("Clicked");
                imagePanel.removeAll();
                imagePanel.add(images.get(position)); //Needs Fixing
                imagePanel.revalidate();
            }
        });
        //Add to previewPanel
        previewPanel.add(iconButton);
        previewPanel.revalidate();
        previewPanel.repaint();
    }

The way I have implemented this is: 1) Open image, 2) Add image to List, 3)Create a thumbnail of image -> add to ImageIcon -> add to Jbutton -> add to component w/ actionListener. The issue is I don't store the buttons in a list so it doesn't know what number its respective image is in the List.

解决方案

One method you can try is to set an action command on the button when it is created. This java tutorial demonstrates this concept is greater detail.

In your case, you would set it like so:

final JButton iconButton = new JButton(icon);
iconButton.setActionCommand("some_unique_identifying_information");

And you can retrieve it in the actionPerformed callback like so:

@Override
public void actionPerformed(ActionEvent e) {
    String actionCommand = iconButton.getActionCommand();
    // ... rest of method
}

Note the use of final on the iconButton. From the looks of your code, the button that fires the event is accessible via the action listener's closure. If you prefer, or cannot use the above method, you can access the button like so:

@Override
public void actionPerformed(ActionEvent e) {
    JButton button = (JButton) e.getSource();
    String actionCommand = button.getActionCommand();
    // ... rest of method
}

Similar to Christian's suggestion in the comments, you can, if you wish, maintain a Map or other data structure in which your unique action command is related to an image. Your map might look like Map<String, ImpImage> (assuming ImpImage is the class of your icon).

Alternatively, the action command may be the string representation of your index value. While it must be stored in String form, once you retrieve it via getActionCommand(), you can parse it back to a number using Integer.parseInt(String).

这篇关于有没有办法在JButton中存储数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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