Swing
- Swing is the set of Java classes for components used in GUI development.
- Rumor has that the name does come from the music - they were listening to it at a Java conference.
- The Swing classes are part of the Java Foundation Library (the javax package).
- The swing classes start with J - for instance JButton.
Graphical User Interfaces
To create a graphical user interface (GUI, pronounced goo-ee), you will:
- Either write an Applet, or create a Frame.
- Decide on the components (buttons, labels, check boxes, ...) you want.
- Decide on the layout manager, which controls how components are shown on the screen (more below).
- Set the layout manager.
- Create each component (with new).
- Add each component to the Panel or Frame using add().
- Handle the event associated with the components in your GUI (more below).
- HelloWorldSwing.java example from Oracle.
Components
Components are the basic building-blocks for creating a Gui.
Layout Managers
Layout Managers control how Components are laid out on the screen.
The basic ones are:
- FlowLayout - adds como
- Grid
- Border
You may review the Swing notes from 04-330 Basic Swing
and the online documentation.
Gui2 Applet
The
Gui2.java, which runs the
LookAndFeel.java
example doesn't do anything. It just demonstrates using the layout manager to add components.
You can run this example with 3 different "look and feels":
- Metal - run java Gui2 metal
- Motif - run java Gui2 motif
- Windows - run java Gui2 windows
Farenheit/Celsius Converter
- A layout manager is fairly limited by itself.
- A common technique is to combine them in the same GUI by using JPanels.
- The Faren2Cel Applet demonstrates this technique.
- Run it with appletviewer and you can see what's going on with the layout.
- Here's the code: Faren2CelGui.java
and Faren2Cel1.java
- So, what needs to be added to complete this program?
Swing Event Handling
- Update the Faren2Cel Applet to do the conversion when the user presses the button.
- Can you easily update it to do the conversion when the user enters the value and presses enter? How about when a radio button is selected?
- We will start this as in-class exercises.