Java Reference
In-Depth Information
p
. It must be the case that the border layout is selected as the layout for the panel. The
method can also be applied on objects of type
JFrame
. Note that only one component
can be added to each of the five areas. For example, if a second button is added to
the west part of the window, it will be placed on top of the first button.
p.setLayout(new GridLayout(3,5,2,1));
Creates a new grid layout. The layout
will have 3 rows, 5 columns, a 2-pixel horizontal gap, and 1-pixel vertical gap.
⇒
String s = new ScriptEngineManager().getEngineByName("JavaScript").
eval(e);
⇒
Computes the value of the expression
e
. For example, if
e
is the string
"2+2"
, then the value of
s
will become the string
"4"
.
JLabel l = new JLabel("This is a label");
⇒
Creates a new label with text
“Thisisalabel”.
JTextField f = new JTextField("",20);
⇒
Creates a text field of size 20 charac-
ters. The first parameter is the initial text in the text field.
⇒
Gets the text in the text field.
String s = f.getText();
f.setText("new text");
⇒
Sets the text of the text field.
f.setFont(new Font("SansSerif", Font.BOLD,10));
⇒
Changes the font of the
text field
f
. The method also applies to text areas. The new font will be Sans Serif,
bold, and point size 10.
⇒
f.getDocument().addDocumentListener(a);
Adds a document listener to the
text field. The document listener interface is called
DocumentListener
and contains
the
insertUpdate
,
removeUpdate
,and
changeUpdate
methods.
s = s.trim();
⇒
Removes leading and trailing spaces from the string
s
.
JTextArea a = new JTextArea(20,30);
⇒
Creates a text area with 30 lines. Every
line has 20 characters.
String s = a.getText();
⇒
Gets the text from the text area.
⇒
Sets the text of the text area.
a.setText("new text");
Adds the text area to the panel. Scroll
bars will appear around the text area when the text becomes too big to fit inside.
⇒
p.add(new JScrollPane(textArea));
JComboBox fontComboBox = new JComboBox(fontNames);
⇒
Creates a new combo
box. The variable
fontNames
is the area of strings that will be displayed in the combo
box.
⇒
String s = (String)fontComboBox.getSelectedItem();
Retrieves the selected
item from the combo box as
String
.
fontComboBox.setEditable(false);
⇒
Makes the combo box not editable.
Adds an action listener to the combo box.
When the value of the combo box is changed, the
actionPerformed
method of the
a
object will be called.
⇒
fontComboBox.addActionListener(a);
JCheckBox jb = new JCheckBox("Bold");
⇒
Creates a check box with label
Bold
.
⇒
Returns
true
if the check box is selected.
jb.isSelected()