爲什麼這段代碼從不打印「Hello2」?爲什麼/何時調用ComponentListener.componentShown()?
public class Test4 {
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel p = new JPanel();
f.getContentPane().add(p);
JLabel x = new JLabel("Hello");
p.add(x);
p.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent arg0) {
System.err.println("Hello1");
}
public void componentMoved(ComponentEvent arg0) {
}
public void componentShown(ComponentEvent arg0) {
System.err.println("Hello2");
}
public void componentHidden(ComponentEvent arg0) {
}
});
f.setVisible(true);
f.pack();
}
}
是的,這是正確的,我重新閱讀api文檔。謝謝。 – PeterMmm 2009-09-16 15:18:05