How can I create or assign a method to temp (WindowAdapter)?
- by Doug Hauf
I want to create an instance of the WindowAdapter and put my method for windowClosing in it and then sent the temp into the f.addWindowListener(temp) can this be done.
Java will not let me create an instance of WindowAdapter like below.
WindowAdapter temp = new WindowAdapter(); <-- Does not compile
How could this be done?
Code:
public static void main(String args[]) {
setLookFeel();
JFrame f = new JFrame("Hello World Printer...");
WindowAdapter temp;
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JButton printButton = new JButton("Print Hello World");
printButton.addActionListener(new HelloWorldPrinter());
f.add("Center", printButton);
f.pack();
f.setVisible(true);
}