3

我是java的初学者。我有 ImageFrame 类如下:

public class ImageFrame extends JFrame {
    private static final long serialVersionUID = 1L;

    public static final int DEFAULT_WIDTH = 1365;
    public static final int DEFAULT_HEIGHT = 730;

    GettingImage getimg = new GettingImage();
    private BufferedImage image = getimg.getImage();

    final ImageProcessing operation = new ImageProcessing(image);

    public ImageFrame(){
        setTitle("ImageTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        JMenu process = new JMenu("Process");

        JMenuItem greyscale = new JMenuItem("greyscale");
        process.add(greyscale);

        //adding action listener to menu items
        greyscale.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    image = operation.greyscale();
                    System.out.println("greyscale is pressed");  
                }
            }
        );

        JMenuBar bar = new JMenuBar();
        setJMenuBar(bar);
        bar.add(process);

        setSize(1365, 730);
        setVisible(true);

        ImageComponent component = new ImageComponent(image);
        add(component);
    }   
}

但是当我按下灰度子菜单时图像不会转换为灰度,但是当我最小化窗口并最大化它时,图像会变成灰度。我认为这是由于窗口没有刷新。我该如何刷新它?

4

3 回答 3

4

在你的动作监听器中调用repaint 。ImageComponent

于 2013-03-06T15:49:26.567 回答
1

尝试在你的动作监听器结束时调用 repaint()

于 2013-03-06T15:50:13.347 回答
0

Well since when minimize the window and then maximize it, you could try .dispose() it closes the window or you can write your own restart method to set the variables to their starting values.

于 2013-03-06T15:52:31.713 回答