简体   繁体   English

如何在java中重置JFrame

[英]How to reset JFrame in java

I am a beginner to java. 我是java的初学者。 I have ImageFrame class as follows: 我有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);
    }   
}

But the image is not converted into greyscale when I press the greyscale submenu but when I minimized the window and maximize it then the image is changed into greyscale. 但是当我按下灰度子菜单时,图像不会转换为灰度,但是当我最小化窗口并使其最大化时,图像会变为灰度。 I think it is due to the fact that the window is not refreshed. 我认为这是因为窗口没有刷新。 How do I refresh it? 我该如何刷新它?

在动作侦听器中的ImageComponent上调用重绘

尝试在动作侦听器的末尾调用repaint()

好吧,因为当最小化窗口然后最大化它时,您可以尝试.dispose()它关闭窗口或者您可以编写自己的重启方法来将变量设置为它们的起始值。

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

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