Photogra :: phracker is about »
- Everything appears on screen is basically a component. JComponent is the Swing superclass of all things that draw on screen. For example, JLabel is a built-in JComponent that displays little text string. What you usually do is to create components objects and install it onto screen or their container.
- Frames (JFrame) are the outermost components. A JFrame represents a single window. Each JFrame component has a "content pane" JComponent that contains all components in the frame. You can use
frame.getContentPane()to get the content pane. Content pane uses a Layout Manager to size and position its components. Frame has a convenience
setLayout()that go to its content pane.
- Components are placed inside other components which form a nesting hierarchy from outer to inner components. And each container is behind the components it contains. Layout Manager sizes and places those components according to some 'policies' or 'intents' of the layout. Don't call
setSize()yourself, the layout manager controls that! Instead, do call
setPreferredSize(), to register a preference before the layout manager lays everything out (before calling
- Three basic layout manager:
FlowLayout, BoxLayout, BorderLayout. Flow layout arranges components left-right, top-down like text. Box layout aligns components in a line, either vertically or horizontally. Border layout puts main content in the center and decorate with 4 things around the outsize -- north, south, east, west.
- Swing thread, or Event-dispatch thread, Composite pattern and Observer/Observable patterns basically form the underlying architecture of Swing GUI system.
- A common technique in Java GUI code is to use an anonymous inner class to create a 'listener' class object and register it using
addXXXListener()method to a Observable, e.g a button, on the fly.