Professional Documents
Culture Documents
Class JFrame
javax.swing
is
from
the
package
Syntax :
setSize (width, height);
Syntax :
setVisible (true);
Syntax :
first
statement
Syntax :
of
the
Syntax:
Container <object> = getContentPane ( );
Syntax:
Syntax:
JLabel object_name;
// declaration
object_name = new JLabel (String to display);
JLabel label1;
label1 = new JLabel (Enter a value);
Syntax:
Object of Container. add(object of JLabel);
Example, if c is the content pane, to add the new JLabel
label1, use:
c.add (label1);
Syntax:
JTextField object_name;
// declaration
object_name = new JTextField (size);
JTextField field1;
field1 = new JTextField (10);
Syntax:
Object of Container. add(object of JTextField);
Example, if c is the content pane, to add the new
JTextField field1, use:
c.add (field1);
Syntax:
JTextField_object.setEditable (true/false);
Example:
field1.setEditable (false);
Syntax:
JButton object_name;
// declaration
object_name = new JButton(Text on Button);
JButton button1;
button1 = new JButton (Click here);
Syntax:
Object of Container. add(object of JButton);
Example, if c is the content pane, to add the new
JButton button1, use:
c.add (button1);
Example:
button1.addActionListener (this);
When the user press the button, an event is sent to the applet
indicating that an action was performed by the user and calls
the method actionPerformed ( ) to process user interaction.
Syntax :
Object of GUI .setToolTipText(Text);
Example:
labek1.setToolTipText("DLSU - Dasmarias");
Syntax:
Example:
object of JTextField.getText()
JTextField field1;
int num;
num = Integer.parseInt (field1.getText ( ));
Note:
Any input from a JTextField is a String.
Syntax:
object of JTextField.setText (String to display);
Example:
Note:
Example:
num = 10;
field1.setText(Integer.toString (num));
Example:
num = 10;
field1.setText(String.valueOf (num));
From java.awt
Used to define the background color and
foreground color that will be used by the GUI
component.
Syntax 1:
object.setBackground(Color.predefined color);
Examples:
c.setBackground(Color.orange);
field1.setBackground(Color.green);
black
blue
cyan
darkGray
gray
green
lightGray
magenta
pink
orange
red
white
yellow
Syntax 2:
object.setBackground(new Color (R,G,B));
Examples:
c.setBackground(new Color(0,128,64));
field1.setBackground(new Color(0,128,64));
Note:
RGB values are the RED, GREEN and BLUE
components of a Color (values are from 0 to 255)
Defines the
components.
foreground
color
of
the
GUI
Syntax 1:
object.setForeground(Color.predefined color);
Examples:
c. setForeground(Color.yellow);
field1. setForeground(Color.blue);
Syntax 2:
object.setForeground(new Color (R,G,B));
Examples:
c.setForeground(new Color(0,128,64));
field1.setForeground(new Color(0,128,64));
Note:
0,0,0 is black
255, 255, 255 is white
From java.awt.
Use to define the font name, font style and font size of the text
that will be used by a GUI component.
Syntax:
Font <object> = new Font ( "Font Name", Font Style, Font Size);
Example:
Font font = new Font ("Trebuchet MS", Font.BOLD, 24);
Note:
Available Font Style are: Font.BOLD, Font.ITALIC, Font.PLAIN
Font.BOLD
Font.ITALIC 2
Font.PLAIN
Font.BOLD + Font.ITALIC 3
Syntax:
Object of GUI.setFont(object of Font Class);
Example:
Font font = new Font ("Trebuchet MS", Font.BOLD, 24);
Label1.setFont(font);
import
import
import
import
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JTextField;
javax.swing.JButton;
import
import
import
import
java.awt.FlowLayout;
java.awt.Container;
java.awt.Font;
java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
Package: javax.swing
Consists of a label positioned beside a circle;
you can click the cirlce to select or deselect
Syntax 1:
Example:
Syntax 2:
Example:
Syntax 3:
Example:
Package: javax.swing
Consists of a label positioned beside a
square; you can click the square to display or
remove check mark
Syntax 1:
JCheckBox object= new JCheckBox();
Example:
Syntax 2:
Example:
Syntax 3:
Example:
Package: javax.swing
When the status of a JCheckBox changes from
unchecked to checked( or from checked to
unchecked), an ItemEvent is generated and
the itemStateChanged() method executes.
Package: javax.swing
Used to groups several components, such as
JCheckBoxes, so a user can select only one at
a time.
When you groupd JCheckBox objects, all of
the other JCheckBoxes are automatically
turned off when the user selects any one
checkbox.
Syntax:
ButtonGroup object = new ButtonGroup();
GUI_object.add(buttonGroup_object);
Example:
Example:
colorList.setVisibleRowCount(5);
1)
2)
3)
SINGLE_SELECTION
SINGLE_INTERVAL_SELECTION
MULTIPLE_INTERVAL_SELCTION
Example:
colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
From javax.swing:
import javax.swing.JScrollPane;
Example:
c.add(new JScrollPane(colorList));
Example:
colorList.addListSelectionListener(this);
c.setBackground(Color.blue);
-OR-
c.setBackground(color[colorList.getSelectedIndex()]);
}
Example:
public void actionPerformed(ActionEvent e)
{
list2.setListData(list1.getSelectedValues());
}
From java.awt
Import java.awt.Toolkit;
Example:
setIconImage(Toolkit.getDefaultToolkit().getImage(lasalle.jpg"));