Top-level Containers: JFrame |
| w = | new JFrame(); |
Constructor |
| w.setTitle(t); |
Sets titlebar text to t |
| w.setDefaultCloseOperation(opt); |
Use opt JFrame.EXIT_ON_CLOSE to terminate program when close box clicked. |
| w.setVisible(true/false); |
Make visible or hide. Also start GUI thread. |
| w.pack(); |
Calculates layout on all inner containers and sets size of JFrame. |
| w.setContentPane(cont); |
Sets the content pane - common to pass a JPanel here. |
| cont = | w.getContentPane(); |
Returns the window's content pane. |
| w.setJMenuBar(mb); |
Adds a JMenuBar. |
| w.setResizable(false); |
Prevent user from resizing window. |
| w.setLocation(x, y); |
Positions window's top left corner at screen coordinates (x, y). |
| w.show(); |
Use w.setVisible(true). |
| w.hide(); |
Use w.setVisible(false). |
| w.setSize(w, h); |
Sets window size, but should use layouts and pack() instead. |
Top-level Container: JApplet |
Top-level Container: Dialogs: JOptionPane, JFileChooser, JColorChooser, JDialog |
Low-level Containers |
| p = | new JPanel(); |
Creates new JPanel |
| p.setLayout(layout); |
Sets the panel's layout. |
| p.add(widget); |
Adds the widget to next position in Layout (eg, Flow, Grid, or Box layouts). |
| p.add(widget, constraint); |
Adds the widget to position defined by constraint (eg, Border or Gridbag layouts). |
| scr = | new JScrollPane(textarea); |
Surrounds textarea with scrollbars as needed. |