CS 051 | Fall 2011 |
We have seen several types of GUI objects so far:
NOTE: the object obtained from our call to getContentPane is also a container, like the
JPanel objects.
To use GUI objects, our code must do several things:
Many GUI objects allow us to use something called a listener. A listener lets the program
respond immediately when the user interacts with the GUI object.
We have seen two types of listeners so far:
To use a listener, our code needs to do several things:
We have seen different ways to place our GUI objects on the screen to make them look nice:
You can find information on all of the above topics in the GUI cheat sheet located on the class web page.
The class example PanelComboBox shows two JComboBoxes added to a JPanel that uses FlowLayout.
The class example
BadButtonPanelDrawingProgram
shows three JButton objects and one JComboBox added to a
JPanel that uses FlowLayout. The result is unappealing because there is not
enough room to see the labels on the buttons.
The class example
ButtonPanelDrawingProgram
fixes this problem by adding the three JButtons to a JPanel,
adding the JComboBox to a separate JPanel, then adding both of these JPanel
objects to a third JPanel object that uses GridLayout with two rows and one column. The
effect is much more appealing.
The class example
ColorSlider
used JSliders attached to ChangeListeners to create a simple program where the user can
select any color by selecting any possible combination of red, green, and blue values, and
displays that color on the screen. Implementing the ChangeListener interface requires that
the stateChanged method be implemented.