pertemuan 03 swing componen

17
1 Mata Kuliah : OOP II (Pertemuan ke-3) Dosen Pengasuh : Surya PERTEMUAN 3 SWING COMPONENT 1. Tujuan • Mengetahui dan Mengenal komponen-komponen dasar didalam Swing. 2. JLabel. Label merupakan komponen GUI yang berfungsi untuk menampilkan suatu teks. Teks tersebut biasanya bersifat read-only. Umumnya programmer jarang sekali mengubah isi suatu label. Kelas untuk menampilkan label di GUI berbasis Java bernama JLabel. Kelas ini diturunkan dari kelas JComponent. JLabel Constructor JLabel() Creates a JLabel instance with no image and with an empty string for the JLabel(Icon image) Creates a JLabel instance with the specified image. JLabel(Icon image, int horizontalAlignment) Creates a JLabel instance with the specified image and horizontal alignment. JLabel(String text) Creates a JLabel instance with the specified text. JLabel(String text, Icon icon, int horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment. JLabel(String text, int Creates a JLabel instance with the specified text and horizontal alignment. Perhatikan dan pahami contoh sederhana pembuatan object JLabel berikut ini : //Contoh1JLabel.java 1 import javax.swing.*; 2 public class Contoh1JLabel { public static void main(String[]args){ JFrame fr; fr=new JFrame("Menampilkan JLabel"); fr.setBounds(10, 10, 300, 100); JLabel lbl; lbl=new JLabel("Label Pertamaku"); fr.getContentPane().add(lbl); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 3 4 5 6 7 8 9 10 11

Upload: heru-prambadi

Post on 26-Oct-2014

127 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PERTEMUAN 03 Swing Componen

1

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

PERTEMUAN 3SWING COMPONENT

1. Tujuan• Mengetahui dan Mengenal komponen-komponen dasar didalam Swing.

2. JLabel.

Label merupakan komponen GUI yang berfungsi untuk menampilkan suatu teks. Teks tersebut biasanya bersifat read-only. Umumnya programmer jarang sekali mengubah isi suatu label. Kelas untuk menampilkan label di GUI berbasis Java bernama JLabel. Kelas ini diturunkan dari kelas JComponent.

JLabel ConstructorJLabel() Creates a JLabel instance with no image and with an empty string for the

title.JLabel(Icon image) Creates a JLabel instance with the specified image.JLabel(Icon image, inthorizontalAlignment)

Creates a JLabel instance with the specified image and horizontal alignment.

JLabel(String text) Creates a JLabel instance with the specified text.JLabel(String text, Icon icon, inthorizontalAlignment)

Creates a JLabel instance with the specified text, image, and horizontalalignment.

JLabel(String text, inthorizontalAlignment)

Creates a JLabel instance with the specified text and horizontal alignment.

Perhatikan dan pahami contoh sederhana pembuatan object JLabel berikut ini ://Contoh1JLabel.java1 import javax.swing.*;2 public class Contoh1JLabel {

public static void main(String[]args){JFrame fr;fr=new JFrame("Menampilkan JLabel");fr.setBounds(10, 10, 300, 100);JLabel lbl;lbl=new JLabel("Label Pertamaku");fr.getContentPane().add(lbl);fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fr.show();// sama fungsinya dengan fr.setVisible(true);

}}

345678910111213Output :

Page 2: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

2

Beberapa method yang sering digunakan pada JLabel :1 setText(String text) Menampilkan text2 getText() Mengambil text3 setToolTipText(String text) Menampilkan tool tip ketika mouse berada tepat diatas Label4 setFont(Font font) Set Font (font face, jenis tulisan, dan font size)5 setForeground(Color fg) Setting warna tulisan.6 setIcon(Icon icon) Menampilkan gambar

//Contoh2JLabel.java1 import javax.swing.*;2 import java.awt.*;public class Contoh2JLabel {private JFrame fr;private JLabel lblNama,lblNamaAnda;private void tampilGUI(){

lblNama=new JLabel("Nama ");lblNama.setToolTipText( "Ini adalah object lblNama" );lblNama.setFont(new Font("Courier New", Font.ITALIC, 30));

lblNamaAnda= new JLabel(lblNama.getText()+" saya Surya ");lblNamaAnda.setForeground(Color.BLUE);

//lblNamaAnda.setIcon(new ImageIcon("icunabra.jpg"));

fr=new JFrame("Menampilkan LABEL"); fr.setBounds(100, 100, 200, 150); fr.setLayout(new FlowLayout(FlowLayout.LEFT));fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fr.setVisible(true);fr.getContentPane().add(lblNama);fr.getContentPane().add(lblNamaAnda);}public static void main (String[]args){new Contoh2JLabel().tampilGUI();}

}

34567891011121314151617181920212223242526

Output :

Page 3: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

3

3. JTextField dan JPassword.

JTextFields dan JPasswordField adalah area yang digunakan untuk menampilkan, mengedit, atau menuliskan suatu teks. JTextField diturunkan dari JTextComponent sedangkan JpasswordField diturunkan dari JTextField. Berbeda dengan JTextField, JPasswordField menampilkan teks dalam format asterisk *.

Hirarchy JTextField :

JTextField ConstructorJTextField() Constructs a new TextField.JTextField(Document doc, Stringtext, int columns)

Constructs a new JTextField that uses the given text storage model and thegiven number of columns.

JTextField(int columns) Constructs a new empty TextField with the specified number of columns.

JTextField(String text) Constructs a new TextField initialized with the specified text.JTextField(String text, intcolumns)

Constructs a new TextField initialized with the specified text and columns.

Hirarchy JPasswordField

JPasswordField ConstructorJPasswordField() Constructs a new JPasswordField, with a default document, null starting

text string, and 0 column width.JPasswordField(Document doc,String txt, int columns)

Constructs a new JPasswordField that uses the given text storage modeland the given number of columns.

JPasswordField(int columns) Constructs a new empty JPasswordField with the specified number ofcolumns.

JPasswordField(String text) Constructs a new JPasswordField initialized with the specified text.JPasswordField(String text, intcolumns)

Constructs a new JPasswordField initialized with the specified text andcolumns.

Page 4: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

//HalamanLogin.java1 import javax.swing.*;public class HalamanLogin {

private JLabel lblUser,lblPass;private JTextField tF;private JPasswordField pF; private JButton btn; private JFrame fr;

void tampil() {lblUser = new JLabel("User : "); lblUser.setBounds(10, 10,100,20); tF = new JTextField(10); tF.setBounds(100, 10,100,20);

lblPass = new JLabel("Password : ");lblPass.setBounds(10, 30,100,20);pF = new JPasswordField(10);pF.setBounds(100, 30,100,20);btn = new JButton("Proses");

2345678910111213141516171819

4

Beberapa method yang biasa digunakan pada JTextField dan JPasswordField :setEditable(boolean) Bisa atau Tidaknya Mengedit teks pada JTextField.getPassword() mengembalikan teks password pada JPasswordField berupa array suatu karakter.getActionCommand() mengembalikan teks yang tertulis pada JTextFieldgetSource() mengembalikan sebuah referensi Component.getSelectedText() mengembalikan teks terpilihsetText(string s) menuliskan teks

4. JButton

Button merupakan komponen mirip tombol. Button terdiri dari beberapa tipe, yaitu command buttons, toggle buttons, check boxes, dan radio buttons. Command button mengaktifkan ActionEvent ketika diklik. Command button diturunkan dari kelas AbstractButton dan dibuat bersama dengan kelas JButton. Di atas JButton dapat diletakkan suatu label teks, atau lebih dikenal sebagai Button label, dan suatu gambar icon.

JButton ConstructorJButton() Creates a button with no set text or icon.JButton(Action a) Creates a button where properties are taken from the Action supplied.JButton(Icon icon) Creates a button with an icon.JButton(String text) Creates a button with text.JButton(String text, Icon icon) Creates a button with initial text and an icon.

Latihan 2. Membuat Halaman Login Menggunakan JFrame Tanpa Layout Manager

Page 5: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

5

20 btn.setBounds(10, 60, 100, 20);

fr = new JFrame("Halaman login :"); fr.getContentPane().setLayout(null); fr.setSize(500,200); fr.getContentPane().add(lblUser); fr.getContentPane().add(tF); fr.getContentPane().add(lblPass); fr.getContentPane().add(pF); fr.getContentPane().add(btn); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fr.setVisible(true);}public static void main (String[]args){

new HalamanLogin().tampil();}

}

21222324252627282930313233343536

Output :

5. JTextArea

JTextArea merupakan komponen yang biasanya digunakan untuk memanipulasi barisan teks. Seperti halnyaJTextField, JtextArea diturunkan dari JTextComponent dan memiliki method yang sama.

JTextArea ConstructorJTextArea() Constructs a new TextArea.JTextArea(Document doc) Constructs a new JTextArea with the given document model, and defaults

for all of the other arguments (null, 0, 0).JTextArea(Document doc, Stringtext, int rows, int columns)

Constructs a new JTextArea with the specified number of rows andcolumns, and the given model.

JTextArea(int rows, int columns) Constructs a new empty TextArea with the specified number of rows andcolumns.

JTextArea(String text) Constructs a new TextArea with the specified text displayed.JTextArea(String text, int rows,int columns)

Constructs a new TextArea with the specified text and number of rows andcolumns.

Page 6: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

6

Bentuk sederhana untuk membuat TextArea : fr = new JFrame("Menampilkan Text Area"); tA = new JTextArea("text area", 4, 30); fr.getContentPane().add(tA);

Latihan 1. Tambahkan Object dari clas JtextArea kedalam latihan class HalamanLogin sehingga menghasilkan tampilan seperti berikut :

JTextArea tidak memiliki scrolling otomatis. Untuk itu, kita memerlukan obyek JScrollPane untuk menambahkan scrolling bar. Langkah untuk menambah obyek JscrollPane pada JtextArea sebagai berikut :

- buat objek dari JScrollPane.- tambahkan komponen (dalam hal ini object JtextArea) kedalam scrollPane.- tambahkan scrolPane ke frame.

Fr= new JFrame(“Text Area dengan ScrollPane”);tA=new JTextArea("text area",4,30); sP= new JScrollPane(tA); fr.getContentPane().add(sP);

Latihan 2. Ubah Source pada Latihan 1 sehingga Object text area berada pada ObjectScrollPane.

Page 7: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

7

6. JComboBox

JComboBox merupakan komponen GUI untuk menampilkan daftar suatu item. Kelas JComboBox juga dapat mengaktifkan ItemEvents. Setiap item di JComboBox diberi indeks numerik. Elemen pertama diberi indeks 0 dan elemen tersebut dimunculkan sebagai item yang dipilih pada saat instan JComboBox tampil untuk pertama kalinya. Scrollbar secara otomatis dihasilkan.Hirarchy :

JComboBox ConstructorJComboBox() Creates a JComboBox with a default data model.JComboBox(ComboBoxModelaModel)

Creates a JComboBox that takes it’s items from an existing ComboBoxModel.

JComboBox(Object[] items) Creates a JComboBox that contains the elements in the specified array.JComboBox(Vector items) Creates a JComboBox that contains the elements in the specified Vector.

Method yang sering digunakan pada JComboBox :getSelectedIndex() mengembalikan indeks dari item yang sedang dipilihsetMaximumRowCount(n) menentukan jumlah maksimum elemen yang ditampilkan ketika pengguna

mengklik instan JComboBoxaddItem(String str) Menambah item kedalam JComboBoxgetItemCount() Menghitung jumlah item.getSelectedItem() Mengembalikan Item yang dipilih

Bentuk sederhana untuk membuat JComboBox fr=new JFrame("Menampilkan ComboBox"); JComboBox cB=new JComboBox(); cB.addItem("satu"); cB.addItem("dua"); fr.getContentPane().add(cB);

Latihan 3. Tambahkan object JComboBox kedalam Latihan 2 sehingga tampilannya menjadi :

Page 8: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

8

Selain cara standar diatas, JComboBox dapat juga ditampilkan dengan DefaultComboBoxModel. Untuk lebih jelasnya pahami source berikut ini :

Output :

Page 9: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

9

7. JCheckBox

JToggleButton menurunkan komponen JCheckBox dan JRadioButton. Komponen ini memiliki nilai on/off atau true/false. Pada kelas JCheckBox, teks ditampilkan di sebelah kanan checkbox. Ketika diklik, JCheckBox mengubah nilai ItemEvent. Pengubahan nilai ItemEvent ini ditangani oleh interface ItemListener yang mendefinisikan method itemStateChanged. Method getStateChange pada kelas ItemEvent mengembalikan nilai integer ItemEvent.SELECTED atau ItemEvent.DESELECTED.

JCheckBox ConstructorJChec kB ox() Creates an initially unselected check box button with no text, no iconJChec kB ox(Ac ti on a) Creates a check box where properties are taken from the Action supplied.JChec kB ox(I con icon) Creates an initially unselected check box with an icon

JChec kB ox(I c on icon,boolean selected)

Creates a check box with an icon and specifies whether or not it is initiallyselected.

JChec kB ox(Stri ng text) Creates an initially unselected check box with textJChec kB ox(Stri ng text,boolean selected)

Creates a check box with text and specifies whether or not it is initiallyselected.

JChec kB ox(Stri ng text, I c on icon) Creates an initially unselected check box with the specified text and icon.JChec kB ox(Stri ng text, I c on icon,boolean selected)

Creates a check box with text and icon, and specifies whether or not it isinitially selected.

Untuk selected awal, selain menggunakan konstruktor, bisa juga dengan method setSelected(true);Bentuk sederhana untuk membuat JCheckBox fr=new JFrame("Menampilkan CheckBox"); chkBoxHobi1=new JCheckBox(“Olah Raga”); chkBoxHobi2=new JCheckBox(“Baca”); fr.getContentPane().add(chkBoxHobi1); fr.getContentPane().add(chkBoxHobi2);

Latihan 4. Tambahkan object JCheckBox kedalam Latihan 3. Sehingga tampilannya menjadi :

Page 10: PERTEMUAN 03 Swing Componen

1

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

8. JRadioButton

JRadioButtons memiliki dua status yaitu selected dan deselected. Pada umumnya radio button ditampilkan dalam sebuah grup. Hanya satu radio button di dalam suatu grup yang dapat dipilih pada satu waktu. Pemilihan satu button menyebabkan button lain berstatus false. Jika nilai parameter selected adalah true, maka status awal JRadioButton adalah selected. Sama halnya dengan JCheckBox, JRadioButton juga dapat mengaktifkan suatu ItemEvents.

JRadioButton ConstructorJRadioButton() Creates an initially unselected radio button with no set text.JRadioButton(Action a) Creates a radiobutton where properties are taken from the Action supplied.JRadioButton(Icon icon) Creates an initially unselected radio button with the specified image but no

text.JRadioButton(Icon icon, booleanselected)

Creates a radio button with the specified image and selection state, but notext.

JRadioButton(String text) Creates an unselected radio button with the specified text.JRadioButton(String text,boolean selected)

Creates a radio button with the specified text and selection state.

JRadioButton(String text, Iconicon)

Creates a radio button that has the specified text and image, and that isinitially unselected.

JRadioButton(String text, Iconicon, boolean selected)

Creates a radio button that has the specified text, image, and selectionstate.

Bentuk sederhana untuk membuat JRadioButton fr=new JFrame("Menampilkan JRadioButton"); rBLaki=new JRadioButton("Laki Laki"); rBPerempuan=new JRadioButton("Perempuan"); ButtonGroup btnGroup = new ButtonGroup(); btnGroup.add(rBLaki); btnGroup.add(rBPerempuan); fr.getContentPane().add(rBLaki); fr.getContentPane().add(rBPerempuan);

Latihan 5. Tambahkan object JRadioButton kedalam Latihan sehingga tampilannya menjadi :

Page 11: PERTEMUAN 03 Swing Componen

Mata Kuliah : OOP II (Pertemuan ke-3)

Dosen Pengasuh : Surya Syahrani

1

Tugas dirumah : Buat Sebuah From Biodata dengan tampilan sebagaimana dibawah ini.