gpt4 book ai didi

java - 如何在 java 中重置 JFrame

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:53 24 4
gpt4 key购买 nike

我是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);
}
}

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

最佳答案

调用repaint在您的 Action 监听器中的 ImageComponent 上。

关于java - 如何在 java 中重置 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com