javax.swing is the library built into Java for creating graphic user interfaces (GUIs). It is more challenging to use than stdlib, but significantly more powerful. For instance, in buttons, sliders, and pop-up menus.
The best way to learn Swing is probably to read chapters 10 and 11 of Horstmann's Core Java. The second best is to work through the code for these chapters from his website; I do this in the first couple of weeks of Software Development.
One could make a reasonable argument that there are better libraries (such as JavaFX) or more modern ways of creating GUIs (such as creating web-based interfaces). Following Hortsmann's lead, I have chosen to stick with Swing so as not to overload students with too many installations, tools, and languages.
Look up individual components as you need details. For quick reference, here are some of the most widely-used classes:
- Layout
- GridBagLayout
- JPanel
- JScrollPane
- Text
- JLabel
- JTextField
- JPasswordField
- JTextArea
- Choice
- JButton
- JCheckBox
- ButtonGroup / JRadioButton
- JComboBox
- JSlider
- Menus
- JMenuBar / JMenu / JMenuItem
- JPopupMenu
- JToolBar
- Dialogs
- JOptionPane
- JDialog
- JFileChooser
- Horstmann, Core Java, Volume I: Fundamentals, 11th Edition, Chapters 10-11
- Core Java website
- Swing API
- ⭐ Which class corresponds to a visible window on the screen?
- ⭐ Where is 0, 0 in the coordinate system used by Swing?
- ⭐⭐ If some back-end information in your program has changed and you need to re-draw something, what method should you call?
- ⭐⭐ Why can't you create an instance of
Rectangle2D
?
javax.swing.JFrame
.- At the upper left. The first coordinate is the
x
coordinate from left to right (in pixels). The second is they
coordinate from top to bottom. java.awt.Component.repaint
(usually on aJFrame
, which descends fromComponent
).- It is an abstract class. You must instead create an instance of
Rectangle2D.Double
(or, if you're creating a huge number of objects,Rectangle2D.Float
).