1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class Exercise16_2 extends JFrame 6 implements ComponentListener { 7 public Exercise16_2() { 8 // Set the window title 9 setTitle("Exercise16_2"); 10 11 // Register the frame as a listener for component events 12 this.addComponentListener(this); 13 } 14 15 /** Main method */ 16 public static void main(String[] args) { 17 Exercise16_2 frame = new Exercise16_2(); 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setSize(100, 80); 20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21 frame.setLocationRelativeTo(null); // Center the frame 22 frame.setVisible(true); 23 } 24 25 public void componentMoved(ComponentEvent e) { 26 System.out.println("Component moved"); 27 } 28 29 public void componentHidden(ComponentEvent e) { 30 System.out.println("Component hidden"); 31 } 32 33 public void componentResized(ComponentEvent e) { 34 System.out.println("Component resized"); 35 } 36 37 public void componentShown(ComponentEvent e) { 38 System.out.println("Component shown"); 39 } 40 }
效果: