Anda di halaman 1dari 3

MODULE 8

JAVA SWING

A. OBJECTIVE
Understanding swing as alternative to implementing window programming. Student
understand components of swing and capable to create a window application use swings
components.
B. BASIC THEORY
Swing does not use components exist in AWT, even though implemented based on AWT,
that why the components appearance does not depend on window local system. So that
the swings graphics appearance for all operating system is alike.
There are lot of swings component such as: jFrame, jPanel, jLabel, jMenu, jMenuitem
etc.
C. TOOLS
1. Computer.
2. NetBeans application
D. EXPERIMENT
WORK STEP
1. Open NetBeans.
2. Right-click on project and create new jFrame Form.
3. Next add the jPanel component in the created Form.

4. Add menu component and change the name as Ubah Warna


5. Next switch to menu item component on menu so when the menu is clicked there will

appeare dropdown menu.


6. On inspector panel, select a menu item that will be given action so when the menu
item is clicked it will change the windows background color.
Right-click on menu item-select Even-action Performed.
7. Add following code below the actionPerformed.
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1.setBackground(Color.red);
jLabel1.setText("Warna Background Merah");
}
8. The code will change the background color into red and in center of window will

appear text Warna Background Merah.


EXERCISES
1. Add two other colors those are Green and Yellow.
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1.setBackground(Color.red);
jLabel1.setForeground(Color.black);
jLabel1.setText("Background color is red");
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1.setBackground(Color.black);
jLabel1.setForeground(Color.white);
jLabel1.setText("Background color is black");
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1.setBackground(Color.yellow);
jLabel1.setForeground(Color.black);
jLabel1.setText("Background color is yellow");
}
2. Add icon symbol next to menu item.

Anda mungkin juga menyukai