Java Reference
In-Depth Information
(continued)
cl ass
used as
looks like
JComponent component =
new JTextField("Text field");
JTextField
JComponent component =
new JTextArea("Text area");
JTextArea
JComponent component =
new JComboBox<String>(
new String[]{
"---1---",
"---2---"
});
JComboBox
JComponent component =
new JCheckBox("Check boxes");
JCheckBox
JRadioButton JComponent component =
new JRadioButton(
"And radio buttons");
Apart from components, there is also a second GUI element you should be aware of, called a “con-
tainer.” Containers hold components together in a specific layout and can also contain sub‐contain-
ers. Therefore, a container can be seen as a special kind of component that holds other components
and organizes them in a specific manner.
You should be aware of the following Swing container classes: JApplet , JFrame , JDialog , JWindow ,
and JPanel ; all of them are derived from the AWT java.awt.Container class (which is subclassed
in a number of AWT containers you can ignore, as you will only be using the Swing ones). You're
probably getting anxious to start coding by now, so why not introduce one of the containers by
means of a Try It Out?
Writing Your First GUI application
try it out
In this Try It Out, you will construct a simple GUI application using the JFrame container class.
1.
As always, feel free to create a new project in Eclipse.
2.
Create a class called MyFirstFrame with the following content:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyFirstFrame {
public static void main(String[] args) {
JFrame frame new JFrame();
Search WWH ::




Custom Search