Swing API provides lots components for crating the GUI for Java Swing applications.
In this section we are studying the Basic GUI components of Swing framework.
We will study the following GUI components:
a) JLabel
b) JTextField
c) JButton
d) JTextArea
e) JCheckBox
f) JList
Let's see all these components in detail.
| Most components have a few common methods. | ||
| cmp.requestFocus(); | Puts focus (eg, blinking cursor) in field, select button, etc. | |
| tf.setFont(f); | Sets font. | |
| JLabel - For fixed text. | ||
| lbl = | new JLabel(t) | Creates JLabel with text on it. Typically created in call to add() and not assigned. |
| JTextField - Box containing one line of text. | ||
| tf = | new JTextField(n); | Creates textfield about n columns wide. |
| s = | tf.getText(); | Returns string in textfield. |
| tf.setText(s); | Sets text to s. | |
| tf.addActionListener(lst); | Action listener lst will be called if enter typed. | |
| tf.setEditable(bool); | Don't allow user to edit text field used for output. | |
| tf | ||