这是我的视图类。主类实际启动程序。视图类中使用了以下代码,因为它包含GUI和事件处理程序(操作侦听器)。
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命令之类的操作时,它似乎不起作用。我想让它在程序第一次运行的时候,也就是当窗口打开的时候做一些事情。
我找了很多,但我还没弄明白。如有任何帮助,我们将不胜感激:)
main中的代码:
TheaterView theater = new TheaterView("Movie Theater");
theater.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theater.setLocation(200, 200);
theater.pack();
theater.setVisible(true);发布于 2016-06-12 17:29:46
您是否覆盖了侦听器中的所有抽象方法?大概是这样的:
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) {
}发布于 2016-06-12 17:34:28
编写一个扩展WindowsAdapter的类,并通过addWindowsListener将其添加到JFrame中。
有关详细信息,请参阅https://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowAdapter.html。
在Swing/AWT中处理侦听器时,总是值得尝试搜索与您想要使用的侦听器相对应的适配器。
https://stackoverflow.com/questions/37772500
复制相似问题