gpt4 book ai didi

java - 将 WindowListener 添加到 JFrame(窗口打开)

转载 作者:行者123 更新时间:2023-12-02 03:33:16 25 4
gpt4 key购买 nike

这是我的 View 类。主类实际上启动了程序。下面的代码在 View 类中使用,因为它包含 GUI 和事件处理程序( Action 监听器)。

public class TheaterView extends JFrame implements WindowListener{
public void windowOpened(WindowEvent e) {
displayMessage("WindowListener method called: windowOpened.");
}
}

这就是我正在做的事情,但它给出了错误消息:TheaterView 不是抽象的,并且不会覆盖 WindowListener 中的抽象方法 windowDeactivated(WindowEvent)。

无论如何,我实现了这些方法并得到了这个(我也得到了其他方法,但这是我唯一需要的):

@Override
public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

但是,当我执行诸如使用 println 命令之类的操作时,它似乎不起作用。我希望它在程序第一次运行时(即打开窗口时)执行某些操作。

我查了很多资料,但还是没能弄清楚。任何帮助将不胜感激:)

主代码:

TheaterView theater = new TheaterView("Movie Theater");
theater.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theater.setLocation(200, 200);
theater.pack();
theater.setVisible(true);

最佳答案

您是否覆盖了监听器中的所有抽象方法?类似的东西:

public void windowClosing(WindowEvent e) {
aboutFrame.dispose();
}

public void windowClosed(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowActivated(WindowEvent e) {
}

public void windowDeactivated(WindowEvent e) {
}

关于java - 将 WindowListener 添加到 JFrame(窗口打开),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37772500/

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