Java 如何在不使用actionListener的情况下更新组件?

Java 如何在不使用actionListener的情况下更新组件?,java,actionlistener,paintcomponent,Java,Actionlistener,Paintcomponent,没有用于图像缩略图的actionListener,但单击后会更新图像 编辑:我目前正在使用JFileChooser导入图像,然后创建一个缩略图,并以与此类似的方式显示完整图像,尽管没有使用ImageIcons。但是我想使用这种方法,所以当我添加一个图像时,它会添加到列表中,并允许我单击缩略图以显示该图像 然而,我使用actionListeners在按下某个按钮时进行更改,但这并不能也不能理解它所在的代码 谢谢 编辑2: 关于重绘选项: 我有一个类,它扩展了一个组件,然后调用一个重绘函数。 然后

没有用于图像缩略图的actionListener,但单击后会更新图像

编辑:我目前正在使用JFileChooser导入图像,然后创建一个缩略图,并以与此类似的方式显示完整图像,尽管没有使用ImageIcons。但是我想使用这种方法,所以当我添加一个图像时,它会添加到列表中,并允许我单击缩略图以显示该图像

然而,我使用actionListeners在按下某个按钮时进行更改,但这并不能也不能理解它所在的代码

谢谢

编辑2:

关于重绘选项:

我有一个类,它扩展了一个组件,然后调用一个重绘函数。 然后,我有一个包含所有Swing组件的类,这些组件调用其他类中的方法。 如果我删除重新验证或重新绘制,它将不会更新屏幕上的图像

编辑3:

这是我如何实现动态按钮的代码:

//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();
    }

看起来它使用了ThumbnailAction,而ThumbnailAction扩展了AbstractAction(在代码的最底层)。Swing组件可以使用动作而不是ActionListener。操作的优点是按钮可以共享一个操作,它们将自动使用相同的键绑定等

编辑:我添加了一些代码,证明您不需要显式重新绘制()。试试看

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(2, 1));
    final JLabel iconLabel = new JLabel();
    JButton button = new JButton("Put Image");
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser fc = new JFileChooser();
            int returnVal = fc.showOpenDialog(null);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                try {
                    iconLabel.setIcon(new ImageIcon(ImageIO.read(fc.getSelectedFile())));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
    panel.add(iconLabel);
    panel.add(button);
    frame.add(panel);
    frame.setVisible(true);
}
编辑2(没有编辑2)

编辑3:试试这个

public class MyActionListener implements ActionListener {
    private JPanel imagePanel;
    private Image image;

    public MyActionListener(JPanel imagePanel, Image image) {
        this.imagePanel = imagePanel;
        this.image = image;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        System.out.println("Clicked");
        imagePanel.removeAll();
        imagePanel.add(image); //Needs Fixing
        imagePanel.revalidate();
    }
}

请把你尝试过的或者需要帮助的最基本的相关代码放在这里。不过,似乎没有用于更新标签的绘制功能,它如何显示新图像?@user2916314
photographLabel.setIcon(displayPhoto)在最末端触发重新绘制。Swing组件在修改时通常会自动刷新。这对我不起作用。我已经更新了显示我代码的原始帖子。谢谢你的帮助,我会尝试一下。虽然我不使用图标来显示图像,但我直接将其添加到Jpanel中。这就是它不起作用的原因吗?@user2916314是的,当您向布局中添加新组件时,必须重新验证。编辑:出于这个原因,除非必要,否则我建议不要更改GUI。更新组件而不是添加新组件更为复杂。
public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(2, 1));
    final JLabel iconLabel = new JLabel();
    JButton button = new JButton("Put Image");
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser fc = new JFileChooser();
            int returnVal = fc.showOpenDialog(null);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                try {
                    iconLabel.setIcon(new ImageIcon(ImageIO.read(fc.getSelectedFile())));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
    panel.add(iconLabel);
    panel.add(button);
    frame.add(panel);
    frame.setVisible(true);
}
public class MyActionListener implements ActionListener {
    private JPanel imagePanel;
    private Image image;

    public MyActionListener(JPanel imagePanel, Image image) {
        this.imagePanel = imagePanel;
        this.image = image;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        System.out.println("Clicked");
        imagePanel.removeAll();
        imagePanel.add(image); //Needs Fixing
        imagePanel.revalidate();
    }
}