public PenApp() {
super("PenApp");
pack();
setVisible(true);
setVisible(false);
pack();
setResizable(false);
pack();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - CANVAS_SIZE) / 2, (d.height - CANVAS_SIZE) / 2);
setSize(CANVAS_SIZE + getInsets().left + getInsets().right,
CANVAS_SIZE + getInsets().top + getInsets().bottom);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
img = Toolkit.getDefaultToolkit().getImage
(getClass().getResource("penguin.gif"));
img2 = Toolkit.getDefaultToolkit().getImage
(getClass().getResource("kuwa.gif"));
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
x = getInsets().left;
y = getInsets().top;
setVisible(true);
}
a piece of code like this. And I'm just wondering why using several pack() and setVisible() here. Thank you :)
We place items inside a container and we can set their sizes via setSize or pack. pack is preferrable since it sizes the frame so all its contents are at or above their preferred sizes; pack leaves frame layout manager in charge of the frame size.
Since we are setting some GUI elements as invisible we don't want blank space there so perhaps that's why it's being used again and again. I would run that application by commenting all the packs in between and verify if it's indeed necessary.