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

时间:2014-01-29 16:41:14

标签: java actionlistener paintcomponent

Code in Question

图像缩略图没有actionListener,但点击后它们会更新图像。

From this webpage.

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

然而我使用actionListeners来改变什么时候被按下但是这不能并且无法理解代码所在的位置。

由于

Edit2:

关于重绘选项:

我有一个扩展组件的类,然后调用重绘函数。

public class Image extends Component {

private BufferedImage img;

//Print Image
    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }
}

然后我有一个包含所有Swing组件的类,它调用其他类的方法。

Image importedImage = new Image(loadimageone.openFile());
Image scaledImage = new Image();
// Save image in Buffered Image array
images.add(importedImage.getImg());
// Display image
imagePanel.removeAll();
imagePanel.add(importedImage);
imagePanel.revalidate();
imagePanel.repaint();
previewPanel.add(scaledImage);
previewPanel.revalidate();
previewPanel.repaint();

如果我删除了重新验证或重新绘制,则不会更新屏幕上的图像。

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

1 个答案:

答案 0 :(得分:2)

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

http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

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

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();
    }
}
相关问题