Hafitz_Rizki 201343500823 Komponen_GUI_pada_Java

Information about Hafitz_Rizki 201343500823 Komponen_GUI_pada_Java

Published on June 11, 2016

Author: kikismsf

Source: slideshare.net

Content

1. Pemrograman Visual “Komponen GUI pada Java ” Disusun Oleh : M. Hafitz Rizki 201343500823/S6H Dosen Nahot Frastian, M.Kom. FTMIPA Indraprasta PGRI Jakarta 2015 - 2016

2. i DAFTAR ISI Cover Daftar Isi ................................................................................................................................i 1. JFrame Input ..............................................................................................................................1 Output ...........................................................................................................................1 2. JLabel Input ..............................................................................................................................2 Output ...........................................................................................................................2 3. JTextField Input ..............................................................................................................................3 Output ...........................................................................................................................3 4. JButton Input ..............................................................................................................................4 Output ...........................................................................................................................4 5. JradioButton Input ..............................................................................................................................5 Output ...........................................................................................................................5 6. JComboBox Input ..............................................................................................................................6 Output ...........................................................................................................................6 7. JTable with JscrollPane Input ..............................................................................................................................7 Output ...........................................................................................................................7 1. A) Soal Modul Halaman 10 Input ..............................................................................................................................8 Output ...........................................................................................................................9 1.B) Soal Modul Halaman 10 – Tambahan Soal Input ..............................................................................................................................10 Output ...........................................................................................................................12 2. Soal Modul Halaman 10 Input ..............................................................................................................................13 Output ...........................................................................................................................15 3. Soal Modul Halaman 10 - Tambahan soal Input ..............................................................................................................................16 Output ...........................................................................................................................18 1. Latihan Membuat Aplikasi - 1 Input ..............................................................................................................................31 Output ...........................................................................................................................36 2. Latihan Membuat Aplikasi - 2 Input ..............................................................................................................................37 Output ...........................................................................................................................43 3. Latihan Membuat Aplikasi - 3 Input ..............................................................................................................................46 Output ...........................................................................................................................54 4. Latihan Membuat Aplikasi - 4 Input ..............................................................................................................................55 Output ...........................................................................................................................59

3. 1 1. JFrame Input : import javax.swing.JFrame; class frame2 { public static void main(String [] args) { JFrame frame = new JFrame(); frame.setTitle("Pemrograman Visual"); frame.setSize(300,300); frame.setVisible(true); } } Output :

4. 2 2. JLabel Input : A. Jlabel Sederhana import javax.swing.JLabel; import javax.swing.JFrame; class Label { public static void main(String[] args) { JLabel lbl1 = new JLabel("Nama Anda :"); JFrame frame = new JFrame(); lbl1.setBounds(10, 50, 75, 25); frame.add(lbl1); frame.setSize(200, 200); frame.setLayout(null); frame.setVisible(true); } } B. JLabel lebih dari satu import javax.swing.JLabel; import javax.swing.JFrame; class Label1 { public static void main(String[] args) { JLabel lbl1 = new JLabel("Nama : M.Hafitz Rizki"); JLabel lbl2 = new JLabel("NPM : 201343500823"); JLabel lbl3 = new JLabel("Kelas : S6H"); JLabel lbl4 = new JLabel("Dosen : Nahot Frastian, M.Kom"); JLabel lbl5 = new JLabel("Mata Kuliah: Pemrograman Visual"); JFrame frame = new JFrame(); lbl1.setBounds(10, 50, 200, 50); lbl2.setBounds(10, 70, 200, 50); lbl3.setBounds(10, 90, 200, 50); lbl4.setBounds(10, 110, 200, 50); lbl5.setBounds(10, 130, 200, 50); frame.add(lbl1); frame.add(lbl2); frame.add(lbl3); frame.add(lbl4); frame.add(lbl5); frame.setSize(300, 300); frame.setLayout(null); frame.setVisible(true); } } Output : A. B.

5. 3 3. JTextField Input : import javax.swing.*; class Text { public static void main(String[] args){ JTextField txt = new JTextField(); JFrame frame = new JFrame(); frame.setSize(200, 200); frame.add(txt); txt.setBounds(50, 10, 100, 25); frame.setLayout(null); frame.setVisible(true); } } Output :

6. 4 4. JButton Input : import javax.swing.*; public class button{ public static void main(String [] args){ JFrame frame = new JFrame(); JButton bttn = new JButton("Oke"); frame.setSize(300,300); bttn.setBounds(50, 100, 150, 25); frame.add(bttn); frame.setLayout(null); frame.setVisible(true); } } Output :

7. 5 5. JRadioButton Input : A. JRadioButton sederhana import javax.swing.*; class radioButton{ public static void main (String [] args){ JRadioButton r1=new JRadioButton("Pilihan 1"); JFrame frame = new JFrame(); frame.setSize(300,300); r1.setBounds(50, 100, 150, 25); frame.add(r1); frame.setLayout(null); frame.setVisible(true); } } B. JRadioButton lebih dari satu import javax.swing.*; class radioButton_pilihan{ public static void main (String [] args){ JRadioButton r1=new JRadioButton("Pilihan 1"); JRadioButton r2=new JRadioButton("Pilihan 2"); JRadioButton r3=new JRadioButton("Pilihan 3"); JFrame frame = new JFrame(); frame.setSize(300,300); r1.setBounds(50, 100, 150, 25); r2.setBounds(70, 120, 150, 25); r3.setBounds(90, 140, 150, 25); frame.add(r1); frame.add(r2); frame.add(r3); frame.setLayout(null); frame.setVisible(true); } } Output : A. B.

8. 6 6. JComboBox Input : A. JComboBox sederhana import javax.swing.*; class combo{ public static void main (String [] args){ String[] abc={"1","2","3","4","5","6","7"}; JComboBox cb=new JComboBox(abc); JFrame frame = new JFrame(); frame.setSize(300,300); cb.setBounds(10, 10, 150, 25); frame.add(cb); frame.setLayout(null); frame.setVisible(true); } } B. JcomboBox dengan JLabel import javax.swing.*; class combo1{ public static void main (String [] args){ String[] abc={"Jakarta","Bogor","Depok","Tangerang ","Bekasi","Bali","Yogyakarta","Bandung"," Lampung","Medan"}; JComboBox cb=new JComboBox(abc); JFrame frame = new JFrame(); JLabel lbl = new JLabel("Kota"); frame.setSize(300,300); cb.setBounds(80, 10, 150, 25); lbl.setBounds(10, 10, 150, 25); frame.add(cb); frame.add(lbl); frame.setLayout(null); frame.setVisible(true); } } Output : A. B.

9. 7 7. JTable with JScrollPane Input : import javax.swing.*; class FrameTabel1 { public static void main(String args[]){ JFrame f= new JFrame(); String[] header={"Senin","Selasa","Rabu"}; String[][] data = { {"100","300","150"}, {"500","600","450"},{"290","690","360"} }; JTable tabel1=new JTable(data,header); JScrollPane scrollPane = new JScrollPane(tabel1); f.setTitle("Frame Tabel"); f.setLocation(200,100); f.setSize(300,100); f.add(scrollPane); f.setVisible(true); } } Output :

10. 8 1. A) Soal Modul Halaman 10 Input : import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JFrame; class Tugas_no_1_hal10 { public static void main (String [] args){ JLabel label1=new JLabel ("DATA MAHASISWA"); JLabel label2=new JLabel ("NPM"); JLabel label3=new JLabel ("Nama"); JLabel label4=new JLabel ("Prodi"); JLabel label5=new JLabel ("Alamat"); JLabel label6=new JLabel ("No. Telepon"); JLabel label7=new JLabel ("--"); JTextField txt1=new JTextField(); JTextField txt2=new JTextField(); JTextField txt3=new JTextField(); JTextField txt4=new JTextField(); JTextField txt5=new JTextField(); JTextField txt6=new JTextField(); JButton bttn=new JButton ("Simpan"); bttn.setBounds(120,280,80,25); label1.setBounds(240,25,150,25); label2.setBounds(20,70,150,25); label3.setBounds(20,110,150,25); label4.setBounds(20,150,150,25); label5.setBounds(20,190,150,25); label6.setBounds(20,230,150,25); label7.setBounds(180,230,10,25); txt1.setBounds(120,70,100,25); txt2.setBounds(120,110,200,25); txt3.setBounds(120,150,150,25); txt4.setBounds(120,190,400,25); txt5.setBounds(120,230,50,25); txt6.setBounds(198,230,150,25); JFrame frame = new JFrame(); frame.add(bttn);

11. 9 frame.setTitle("Design Preview [satu]"); frame.add(txt1); frame.add(txt2); frame.add(txt3); frame.add(txt4); frame.add(txt5); frame.add(txt6); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(label4); frame.add(label5); frame.add(label6); frame.add(label7); frame.setSize (580,380); frame.setLayout(null); frame.setVisible(true); } } Output :

12. 10 1.B) Soal Modul Halaman 10 – Tambahan Soal Input : import javax.swing.*; class DataMahasiswa { public static void main (String [] args){ JLabel label0=new JLabel ("DATA MAHASISWA"); JLabel label1=new JLabel ("Nama"); JLabel label2=new JLabel ("NPM"); JLabel label3=new JLabel ("Kelas"); JLabel label4=new JLabel ("Mata Kuliah"); JLabel label5=new JLabel ("Dosen"); JLabel label6=new JLabel ("Program Studi"); JLabel label7=new JLabel ("Universitas"); JTextField txt1=new JTextField(); JTextField txt2=new JTextField(); JTextField txt3=new JTextField(); JTextField txt4=new JTextField(); JTextField txt5=new JTextField(); JTextField txt6=new JTextField(); JTextField txt7=new JTextField(); JButton bttn1=new JButton ("Save"); JButton bttn2=new JButton ("Cancel"); bttn1.setBounds(180,300,80,25); bttn2.setBounds(320,300,80,25);

13. 11 label0.setBounds(240,20,150,25); label1.setBounds(70,70,150,25); label2.setBounds(70,100,150,25); label3.setBounds(70,130,150,25); label4.setBounds(70,160,150,25); label5.setBounds(70,190,150,25); label6.setBounds(70,220,150,25); label7.setBounds(70,250,150,25); txt1.setBounds(180,70,220,25); txt2.setBounds(180,100,220,25); txt3.setBounds(180,130,220,25); txt4.setBounds(180,160,220,25); txt5.setBounds(180,190,220,25); txt6.setBounds(180,220,220,25); txt7.setBounds(180,250,220,25); JFrame frame = new JFrame(); frame.setTitle("Data Mahasiswa Kelas H Pemrograman Visual"); frame.add(bttn1); frame.add(bttn2); frame.add(txt1); frame.add(txt2); frame.add(txt3); frame.add(txt4); frame.add(txt5); frame.add(txt6); frame.add(txt7); frame.add(label0); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(label4); frame.add(label5); frame.add(label6); frame.add(label7); frame.setSize (500,400); frame.setLayout(null); frame.setVisible(true); } }

14. 12 Output :

15. 13 2. Soal Modul Halaman 10 Input: import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JCheckBox; import javax.swing.JRadioButton; class Tugas_no_2_hal10 { public static void main (String [] args){ String [] agama={"Islam","Katolik","Protestan","Hindu","Budha","Konghuchu"}; JLabel label1=new JLabel ("NPM"); JLabel label2=new JLabel ("Nama"); JLabel label3=new JLabel ("Agama"); JLabel label4=new JLabel ("Jenis Kelamin"); JLabel label5=new JLabel ("Hobi"); JTextField txt1=new JTextField(); JTextField txt2=new JTextField(); JButton bttn1=new JButton ("Save"); JButton bttn2=new JButton ("Edit"); JButton bttn3=new JButton ("Delete"); JButton bttn4=new JButton ("Exit"); bttn1.setBounds(80,310,70,25); bttn2.setBounds(160,310,70,25); bttn3.setBounds(240,310,70,25); bttn4.setBounds(320,310,70,25); JComboBox cb_agama=new JComboBox(agama); cb_agama.setBounds (130,110,150,25); JRadioButton r1= new JRadioButton("Laki-laki"); JRadioButton r2= new JRadioButton("Perempuan"); r1.setBounds(130,150,100,25); r2.setBounds(250,150,100,25); JCheckBox c1=new JCheckBox("Membaca"); JCheckBox c2=new JCheckBox("Menari"); JCheckBox c3=new JCheckBox("Olah Raga"); JCheckBox c4=new JCheckBox("Memasak");

16. 14 JCheckBox c5=new JCheckBox("Menyanyi"); JCheckBox c6=new JCheckBox("Menulis"); c1.setBounds(130,180,100,25); c2.setBounds(250,180,100,25); c3.setBounds(130,220,100,25); c4.setBounds(250,220,100,25); c5.setBounds(130,260,100,25); c6.setBounds(250,260,100,25); label1.setBounds(20,30,100,25); label2.setBounds(20,70,150,25); label3.setBounds(20,110,150,25); label4.setBounds(20,150,150,25); label5.setBounds(20,180,150,25); txt1.setBounds(130,30,150,25); txt2.setBounds(130,70,230,25); JFrame frame = new JFrame(); frame.setTitle("Desain Preview [dua]"); frame.add(r1); frame.add(r2); frame.add(c1); frame.add(c2); frame.add(c3); frame.add(c4); frame.add(c5); frame.add(c6); frame.add(bttn1); frame.add(bttn2); frame.add(bttn3); frame.add(bttn4); frame.add(txt1); frame.add(txt2); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(label4); frame.add(label5); frame.add(cb_agama); frame.setSize (450,400); frame.setLayout(null); frame.setVisible(true);

17. 15 } } Output :

18. 16 3. Soal Modul Halaman 10 - Tambahan soal Input : import javax.swing.*; import javax.swing.JComboBox; class PemrogramanVisual { public static void main (String [] args){ JLabel label0=new JLabel ("PEMROGRAMAN VISUAL"); JLabel label1=new JLabel ("Nama"); JLabel label2=new JLabel ("NPM"); JLabel label3=new JLabel ("Kelas"); JLabel label4=new JLabel ("Mata Kuliah"); JLabel label5=new JLabel ("Dosen"); JLabel label6=new JLabel ("Program Studi"); JLabel label7=new JLabel ("Universitas"); String[] kelas={"S6A","S6B","S6C","S6D","S6E","S6F","S6G","S6H"}; JComboBox cb_kelas=new JComboBox(kelas); cb_kelas.setBounds (180,130,220,25); String[] mata_kuliah={"Pemrograman Visual","Pemrograman Web","Kalkulus","Statistika Dasar","Jaringan Komputer","B.Indonesia","B.Inggris","Agama"}; JComboBox cb_mata_kuliah=new JComboBox(mata_kuliah); cb_mata_kuliah.setBounds (180,160,220,25); JTextField txt1=new JTextField(); JTextField txt2=new JTextField(); JTextField txt5=new JTextField(); JTextField txt6=new JTextField(); JTextField txt7=new JTextField(); JButton bttn1=new JButton ("Save"); JButton bttn2=new JButton ("Delete"); JButton bttn3=new JButton ("Edit"); JButton bttn4=new JButton ("Exit"); bttn1.setBounds(90,300,70,25); bttn2.setBounds(170,300,70,25); bttn3.setBounds(250,300,70,25); bttn4.setBounds(330,300,70,25); label0.setBounds(230,20,150,25); label1.setBounds(70,70,150,25);

19. 17 label2.setBounds(70,100,150,25); label3.setBounds(70,130,150,25); label4.setBounds(70,160,150,25); label5.setBounds(70,190,150,25); label6.setBounds(70,220,150,25); label7.setBounds(70,250,150,25); txt1.setBounds(180,70,220,25); txt2.setBounds(180,100,220,25); txt5.setBounds(180,190,220,25); txt6.setBounds(180,220,220,25); txt7.setBounds(180,250,220,25); JFrame frame = new JFrame(); frame.setTitle("Mata Kuliah Pemrograman Visual"); frame.add(bttn1); frame.add(bttn2); frame.add(bttn3); frame.add(bttn4); frame.add(txt1); frame.add(txt2); frame.add(cb_kelas); frame.add(cb_mata_kuliah); frame.add(txt5); frame.add(txt6); frame.add(txt7); frame.add(label0); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(label4); frame.add(label5); frame.add(label6); frame.add(label7); frame.setSize (500,400); frame.setLayout(null); frame.setVisible(true); } }

20. 18 Output :

21. 19 1. FlowLayout Input : A. FlowLayout Sederhana import javax.swing.*; import java.awt.*; class flow{ public static void main (String [] args){ JFrame f=new JFrame("Flow Layout"); FlowLayout fl=new FlowLayout(FlowLayout.CENTER); JButton b1=new JButton("Posisi 1"); JButton b2=new JButton("Posisi 2"); JButton b3=new JButton("Posisi 3"); JButton b4=new JButton("Posisi 4"); JButton b5=new JButton("Posisi 5"); f.getContentPane().setLayout(fl); f.getContentPane().add(b1); f.getContentPane().add(b2); f.getContentPane().add(b3); f.getContentPane().add(b4); f.getContentPane().add(b5); f.setSize(500,80); f.setVisible(true); } } B. FlowLayout Kostum import javax.swing.*; import java.awt.*; class flow1{ public static void main (String [] args){ JFrame f=new JFrame("M.Hafitz [email protected]"); FlowLayout fl=new FlowLayout(FlowLayout.CENTER); JButton b1=new JButton("Home"); JButton b2=new JButton("Artikel"); JButton b3=new JButton("Program"); JButton b4=new JButton("About Us"); JButton b5=new JButton("Contact"); f.getContentPane().setLayout(fl); f.getContentPane().add(b1); f.getContentPane().add(b2); f.getContentPane().add(b3); f.getContentPane().add(b4); f.getContentPane().add(b5); f.setSize(500,80); f.setVisible(true); } } Output : a. b.

22. 20 2. FlowLayout Input : A. BorderLayout Sederhana import javax.swing.*; import java.awt.*; class border{ public static void main (String [] args){ JFrame f=new JFrame("Border Layout"); BorderLayout bl=new BorderLayout(); f.getContentPane().setLayout(bl); JButton b1=new JButton("NORTH"); JButton b2=new JButton("SOUTH"); JButton b3=new JButton("EAST"); JButton b4=new JButton("WEST"); JButton b5=new JButton("CENTER"); f.getContentPane().add(b1,BorderLayout.N ORTH); f.getContentPane().add(b2,BorderLayout.SO UTH); f.getContentPane().add(b3,BorderLayout.E AST); f.getContentPane().add(b4,BorderLayout.W EST); f.getContentPane().add(b5,BorderLayout.CE NTER); f.pack(); f.setVisible(true); } } B. BorderLayout Kostum import javax.swing.*; import java.awt.*; class border1{ public static void main (String [] args){ JFrame f=new JFrame("M.Hafitz [email protected] - BorderLayout"); BorderLayout bl=new BorderLayout(); f.getContentPane().setLayout(bl); JButton b1=new JButton("201343500823"); JButton b2=new JButton("S6H"); JButton b3=new JButton("Rizki"); JButton b4=new JButton("Mohamad"); JButton b5=new JButton("Hafitz"); f.getContentPane().add(b1,BorderLayout.NOR TH); f.getContentPane().add(b2,BorderLayout.SOU TH); f.getContentPane().add(b3,BorderLayout.EAS T); f.getContentPane().add(b4,BorderLayout.WES T); f.getContentPane().add(b5,BorderLayout.CEN TER); f.pack(); f.setVisible(true); } }

23. 21 Output : a. b.

24. 22 3. GridLayout Input : A. GridLayout Sederhana import javax.swing.*; import java.awt.*; class grid{ public static void main(String [] args){ JFrame f=new JFrame("Grid Layout"); JButton b1=new JButton("Satu"); JButton b2=new JButton("Dua"); JButton b3=new JButton("Tiga"); JButton b4=new JButton("Empat"); JButton b5=new JButton("Lima"); GridLayout gl=new GridLayout(3,2); f.getContentPane().setLayout(gl); f.getContentPane().add(b1); f.getContentPane().add(b2); f.getContentPane().add(b3); f.getContentPane().add(b4); f.getContentPane().add(b5); f.pack(); f.setVisible(true); } } A. GridLayout Operator Aritmatika import javax.swing.*; import java.awt.*; class grid1{ public static void main(String [] args){ JFrame f=new JFrame("M.Hafitz [email protected] - Grid Layout"); JButton b1=new JButton("+"); JButton b2=new JButton("-"); JButton b3=new JButton("/"); JButton b4=new JButton("*"); JButton b5=new JButton("%"); JButton b6=new JButton("="); GridLayout gl=new GridLayout(3,2); f.getContentPane().setLayout(gl); f.getContentPane().add(b1); f.getContentPane().add(b2); f.getContentPane().add(b3); f.getContentPane().add(b4); f.getContentPane().add(b5); f.getContentPane().add(b6); f.pack(); f.setVisible(true); } } Output : a. b.

25. 23 1. ActionListener – Contoh 1 Input : A. ActionListener Sederhana#1 import java.awt.event.*; import javax.swing.*; public class klik extends JFrame implements ActionListener { JButton tombol = new JButton ("Click Me!"); public klik() { tombol.addActionListener(this); add(tombol); setSize (200,100); setVisible (true); } public static void main (String args []) { klik test = new klik(); } public void actionPerformed (ActionEvent e) { if (e.getSource() == tombol) { JOptionPane.showMessageDialog(null, "Pemrograman Visual"); } } } B. ActionListener Kostum#1 import java.awt.event.*; import javax.swing.*; public class klik extends JFrame implements ActionListener { JButton tombol = new JButton ("Klik Disini!"); public klik() { tombol.addActionListener(this); add(tombol); setSize (200,100); setVisible (true); } public static void main (String args []) { klik test = new klik(); } public void actionPerformed (ActionEvent e) { if (e.getSource() == tombol) { JOptionPane.showMessageDialog(null, "Nama : M.Hafitz RizkinNPM : 201343500823nKelas : S6HnMata Kuliah : Pemrograman VisualnDosen : Nahot Frastian, M.Kom"); } } } Output : a. Setelah di Klik

26. 24 b. 2. ActionListener – Contoh 2 Input : A. ActionListener Sederhana#2 import java.awt.event.*; import java.awt.*; import javax.swing.*; class action1 extends JFrame implements ActionListener { JButton bttn = new JButton("Click me"); JLabel lbl = new JLabel(); public action1() { super("Button"); setSize(500, 400); setVisible(true); setLayout(null); add(bttn); bttn.setBounds(180, 100, 150, 25); add(lbl); lbl.setBounds(10,150,475,55); lbl.setFont(new Font("Tahoma", Font.BOLD, 22)); bttn.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource() == bttn){ lbl.setText("Anda telah menekan sebuah tombol"); } } public static void main(String[] args){ new action1(); } } B. ActionListener Kostum#2 import java.awt.event.*; import java.awt.*; import javax.swing.*; class action1 extends JFrame implements ActionListener { JButton bttn = new JButton("Klik Tombol Ini!"); JLabel lbl = new JLabel(); public action1() { super("M.Hafitz [email protected] Button"); setSize(500, 400); setVisible(true); setLayout(null); add(bttn); bttn.setBounds(180, 100, 150, 25); add(lbl); lbl.setBounds(10,150,475,55); lbl.setFont(new Font("Times New Roman", Font.BOLD, 12)); bttn.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource() == bttn){ lbl.setText("Halo, perkenalkan nama saya M.Hafitz Rizki.nKelas S6H, dengan NPM 201343500823."); } } public static void main(String[] args){ new action1(); } } Setelah di Klik

27. 25 Output : a. Setelah di Klik

28. 26 b. Setelah di Klik

29. 27 3. ActionListener – Contoh 3 Input : A. ActionListener Sederhana#3 import javax.swing.*; import java.awt.event.*; class fun3 extends JFrame implements ActionListener{ JLabel l1=new JLabel("Input Nama :"); JLabel l2=new JLabel(); JTextField t1=new JTextField(); JButton b1=new JButton("Submit"); fun3(){ l1.setBounds(10,10,100,30); t1.setBounds(10,50,250,30); b1.setBounds(50,90,80,30); l2.setBounds(10,130,250,30); add(l1); add(l2); add(t1); add(b1); b1.addActionListener(this); setSize(280,250); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e){ if (e.getSource()==b1){ String isi=t1.getText(); l2.setText(isi); } } public static void main (String [] args){ fun3 f3=new fun3(); } } B. ActionListener Kostum#3 import javax.swing.*; import java.awt.event.*; class fun3 extends JFrame implements ActionListener{ JLabel l1=new JLabel("Input Nama :"); JLabel l2=new JLabel(); JTextField t1=new JTextField(); JButton b1=new JButton("Submit"); fun3(){ l1.setBounds(10,10,100,30); t1.setBounds(10,50,250,30); b1.setBounds(50,90,80,30); l2.setBounds(10,130,250,30); add(l1); add(l2); add(t1); add(b1); b1.addActionListener(this); setSize(280,250); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e){ if (e.getSource()==b1){ String isi=t1.getText(); l2.setText(isi); } } public static void main (String [] args){ fun3 f3=new fun3(); } } Output : a. Setelah di Klik

30. 28 b. 4. ActionListener – Contoh 4 Input : A. ActionListener Sederhana#4 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class loginNonDB extends JFrame implements ActionListener { JLabel lblUser=new JLabel("User"), lblPass=new JLabel("Password"); JTextField txtUser=new JTextField(); JTextField txtPass =new JTextField(); JButton btnLogin=new JButton("log in"); public static void main(String [] args) { loginNonDB log = new loginNonDB(); log.setVisible(true); } loginNonDB() { super("Login"); setSize(250,200); setLayout(null); lblUser.setBounds(15,20,100,25); lblPass.setBounds(15,55,100,25); txtUser.setBounds(90,20,120,25); txtPass.setBounds(90,55,120,25); btnLogin.setBounds(80,110,85,25); btnLogin.addActionListener(this); add(lblUser); add(txtUser); add(lblPass); add(txtPass); add(btnLogin); setLocationRelativeTo(null); B. ActionListener Sederhana#4 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class loginNonDB extends JFrame implements ActionListener { JLabel lblUser=new JLabel("Username"), lblPass=new JLabel("Password"); JTextField txtUser=new JTextField(); JTextField txtPass =new JTextField(); JButton btnLogin=new JButton("Sign In"); public static void main(String [] args) { loginNonDB log = new loginNonDB(); log.setVisible(true); } loginNonDB() { super("Sign In"); setSize(250,200); setLayout(null); lblUser.setBounds(15,20,100,25); lblPass.setBounds(15,55,100,25); txtUser.setBounds(90,20,120,25); txtPass.setBounds(90,55,120,25); btnLogin.setBounds(80,110,85,25); btnLogin.addActionListener(this); add(lblUser); add(txtUser); add(lblPass); add(txtPass); Setelah di Klik

31. 29 setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==btnLogin) { if (txtUser.getText().equals("admin") && txtPass.getText().equals("root")) { javax.swing.JOptionPane.showMessageDial og(null, "Login BerhasilnAnda berhasil masuk", "Konfirmasi", javax.swing.JOptionPane.INFORMATION _MESSAGE); System.exit(0); }else{ javax.swing.JOptionPane.showMessageDial og(null, "User anda tidak dikenal","Kesalahan",javax.swing.JOptionP ane.ERROR_MESSAGE); txtUser.setText(""); txtUser.requestFocus(); System.exit(0); } } } } add(btnLogin); setLocationRelativeTo(null); setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==btnLogin) { if (txtUser.getText().equals("kiki") && txtPass.getText().equals("pvisual")) { javax.swing.JOptionPane.showMessageDi alog(null, "Login BerhasilnAnda berhasil masuk", "Konfirmasi", javax.swing.JOptionPane.INFORMATIO N_MESSAGE); System.exit(0); }else{ javax.swing.JOptionPane.showMessageDi alog(null, "User anda tidak dikenal","Kesalahan",javax.swing.JOption Pane.ERROR_MESSAGE); txtUser.setText(""); txtUser.requestFocus(); System.exit(0); } } } } Output : a. 1) Login Berhasil

32. 30 2) Login Gagal b. 1) Login Berhasil 2) Login Gagal

33. 31 1. Latihan Membuat Aplikasi - 1 A. Input : package soal1; /** * Nama : M.Hafitz Rizki * NPM : 201343500823 * kelas : S6H * Dosen : Nahot Frastian, M.Kom * Mata Kuliah : Pemrograman Visual * Universitas : Indraprasta PGRI */ public class NewJFrame extends javax.swing.JFrame { /** * Creates new form NewJFrame */ public NewJFrame() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jFrame1 = new javax.swing.JFrame(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jBtnClear = new javax.swing.JButton(); jBtnExit = new javax.swing.JButton(); jTxtPanjang = new javax.swing.JTextField(); jTxtLebar = new javax.swing.JTextField(); jTxtLuas = new javax.swing.JTextField(); javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane()); jFrame1.getContentPane().setLayout(jFrame1Layout); jFrame1Layout.setHorizontalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE)

34. 32 ); jFrame1Layout.setVerticalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel1.setText("Panjang"); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel2.setText("Lebar"); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel3.setText("Luas"); jBtnClear.setText("Clear"); jBtnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnClearActionPerformed(evt); } }); jBtnExit.setText("Exit"); jBtnExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnExitActionPerformed(evt); } }); jTxtLebar.addFocusListener(new java.awt.event.FocusAdapter() { public void focusLost(java.awt.event.FocusEvent evt) { jTxtLebarFocusLost(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(57, 57, 57) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

35. 33 .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addComponent(jLabel2) .addComponent(jLabel1)) .addGap(59, 59, 59) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTxtLebar, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTxtLuas, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTxtPanjang, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(76, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(jBtnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jBtnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(76, 76, 76)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(53, 53, 53) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTxtPanjang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTxtLebar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(29, 29, 29) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

36. 34 .addComponent(jTxtLuas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jBtnExit) .addComponent(jBtnClear)) .addGap(72, 72, 72)) ); pack(); }// </editor-fold> private void jBtnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jTxtLebar.setText(""); jTxtPanjang.setText(""); jTxtLuas.setText(""); } private void jBtnExitActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: dispose(); } private void jTxtLebarFocusLost(java.awt.event.FocusEvent evt) { // TODO add your handling code here: int panjang=Integer.parseInt(jTxtPanjang.getText()); int lebar=Integer.parseInt(jTxtLebar.getText()); int luas=panjang*lebar; jTxtLuas.setText(String.valueOf(luas)); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

37. 35 */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SE VERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SE VERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SE VERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SE VERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jBtnClear; private javax.swing.JButton jBtnExit; private javax.swing.JFrame jFrame1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JTextField jTxtLebar; private javax.swing.JTextField jTxtLuas;

38. 36 private javax.swing.JTextField jTxtPanjang; // End of variables declaration } B. Output :

39. 37 2. Latihan Membuat Aplikasi – 2 A. Input : package soal1; /** * Nama : M.Hafitz Rizki * NPM : 201343500823 * kelas : S6H * Dosen : Nahot Frastian, M.Kom * Mata Kuliah : Pemrograman Visual * Universitas : Indraprasta PGRI */ public class NewJFrame2 extends javax.swing.JFrame { /** * Creates new form NewJFrame2 */ public NewJFrame2() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jBtnTambah = new javax.swing.JButton(); jBtnExit = new javax.swing.JButton(); jBtnBagi = new javax.swing.JButton(); jBtnKurang = new javax.swing.JButton(); jBtnClear = new javax.swing.JButton(); jBtnKali = new javax.swing.JButton(); jTxtNila = new javax.swing.JTextField(); jTxtNilb = new javax.swing.JTextField(); jTxtNilc = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

40. 38 jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel1.setText("Hasil A"); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel2.setText("Hasil B"); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel3.setText("Hasil"); jBtnTambah.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jBtnTambah.setText("Tambah"); jBtnTambah.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnTambahActionPerformed(evt); } }); jBtnExit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jBtnExit.setText("Exit"); jBtnExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnExitActionPerformed(evt); } }); jBtnBagi.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jBtnBagi.setText("Bagi"); jBtnBagi.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnBagiActionPerformed(evt); } }); jBtnKurang.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jBtnKurang.setText("Kurang"); jBtnKurang.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnKurangActionPerformed(evt); } }); jBtnClear.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jBtnClear.setText("Clear"); jBtnClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {

41. 39 jBtnClearActionPerformed(evt); } }); jBtnKali.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jBtnKali.setText("Kali"); jBtnKali.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnKaliActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(46, 46, 46) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1) .addComponent(jTxtNila) .addComponent(jTxtNilb) .addComponent(jTxtNilc, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE) .addComponent(jLabel3)) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jBtnTambah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jBtnKurang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jBtnKali, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jBtnBagi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jBtnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

42. 40 .addComponent(jBtnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(93, 93, 93)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(48, 48, 48) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jBtnTambah)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(4, 4, 4) .addComponent(jTxtNila, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(jBtnKurang))) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTxtNilb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTxtNilc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(jBtnKali) .addGap(18, 18, 18) .addComponent(jBtnBagi) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE) .addComponent(jBtnClear) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jBtnExit)

43. 41 .addGap(33, 33, 33)))) ); pack(); }// </editor-fold> private void jBtnTambahActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int a = Integer.parseInt(jTxtNila.getText()); int b = Integer.parseInt(jTxtNilb.getText()); int c1 = a+b; jTxtNilc.setText(String.valueOf(c1)); } private void jBtnKaliActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int a = Integer.parseInt(jTxtNila.getText()); int b = Integer.parseInt(jTxtNilb.getText()); int c1 = a*b; jTxtNilc.setText(String.valueOf(c1)); } private void jBtnKurangActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int a = Integer.parseInt(jTxtNila.getText()); int b = Integer.parseInt(jTxtNilb.getText()); int c1 = a-b; jTxtNilc.setText(String.valueOf(c1)); } private void jBtnBagiActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int a = Integer.parseInt(jTxtNila.getText()); int b = Integer.parseInt(jTxtNilb.getText()); int c1 = a/b; jTxtNilc.setText(String.valueOf(c1)); } private void jBtnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jTxtNila.setText(""); jTxtNilb.setText(""); jTxtNilc.setText(""); }

44. 42 private void jBtnExitActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: dispose(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.S EVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() {

45. 43 public void run() { new NewJFrame2().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jBtnBagi; private javax.swing.JButton jBtnClear; private javax.swing.JButton jBtnExit; private javax.swing.JButton jBtnKali; private javax.swing.JButton jBtnKurang; private javax.swing.JButton jBtnTambah; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JTextField jTxtNila; private javax.swing.JTextField jTxtNilb; private javax.swing.JTextField jTxtNilc; // End of variables declaration } B. Output :

46. 44 1. Operator Button Tambah 2. Operator Button Kurang 3. Operator Button Kali

47. 45 4. Operator Button Bagi 5. Operator Button Clear

48. 46 3. Latihan Membuat Aplikasi – 3 A. Input : public class Klinik extends javax.swing.JFrame { /** * Creates new form Klinik */ public Klinik() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel10 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox(); jLabel12 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jCheckBox1 = new javax.swing.JCheckBox(); jCheckBox2 = new javax.swing.JCheckBox(); jCheckBox3 = new javax.swing.JCheckBox(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jCheckBox4 = new javax.swing.JCheckBox(); jCheckBox5 = new javax.swing.JCheckBox(); jCheckBox6 = new javax.swing.JCheckBox(); jLabel11 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jTextField4 = new javax.swing.JTextField();

49. 47 jTextField5 = new javax.swing.JTextField(); jTextField6 = new javax.swing.JTextField(); jTextField7 = new javax.swing.JTextField(); jTextField8 = new javax.swing.JTextField(); jTextField9 = new javax.swing.JTextField(); jComboBox2 = new javax.swing.JComboBox(); jTextField10 = new javax.swing.JTextField(); jTextField11 = new javax.swing.JTextField(); jLabel10.setText("Administrasi"); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jLabel12.setText("jLabel12"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Puskesmas Sukmajaya"); jCheckBox1.setText("Umum"); jCheckBox2.setText("Gigi"); jCheckBox3.setText("Polio"); jLabel2.setText("Nama Dokter"); jLabel3.setText("Waktu Praktek"); jLabel4.setText("Lama Inap"); jLabel5.setText("Nama Ruang"); jLabel6.setText("Harga/Hari"); jLabel7.setText("Diskon"); jLabel8.setText("No.Pasien"); jLabel9.setText("Nama"); jCheckBox4.setText("Administrasi"); jCheckBox5.setText("Konsultasi Dokter"); jCheckBox6.setText("Obat"); jLabel11.setText("Total"); jButton1.setText("Clear"); jButton2.setText("Exit"); jTextField2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField2ActionPerformed(evt); } }); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "Kenanga", "Melati", "Cempaka" })); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

50. 48 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jCheckBox1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jCheckBox2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jCheckBox3)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addGap(26, 26, 26) .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel4) .addComponent(jLabel5))) .addGroup(layout.createSequentialGroup() .addGap(98, 98, 98) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField11, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE) .addComponent(jTextField10)))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel8) .addComponent(jLabel9)) .addGap(44, 44, 44) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE) .addComponent(jTextField2))) .addGroup(layout.createSequentialGroup() .addGap(22, 22, 22) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jCheckBox5)

51. 49 .addComponent(jCheckBox6) .addComponent(jCheckBox4)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE) .addComponent(jTextField4) .addComponent(jTextField5)))) .addContainerGap(22, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1) .addGap(18, 18, 18) .addComponent(jButton2) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(109, 109, 109) .addComponent(jLabel1)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel6)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel7) .addGap(161, 161, 161) .addComponent(jLabel11) .addGap(39, 39, 39) .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel3) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))))

52. 50 .addContainerGap(106, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addComponent(jLabel1) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jCheckBox1) .addComponent(jCheckBox2) .addComponent(jCheckBox3) .addComponent(jLabel8) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jLabel9) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(36, 36, 36) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(jCheckBox4) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

53. 51 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(jCheckBox5) .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jCheckBox6) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel7) .addGap(51, 65, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel11) .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(58, 58, 58)))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton2) .addComponent(jButton1)) .addContainerGap()))) );

54. 52 pack(); }// </editor-fold> private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) { } public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Klinik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Klinik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Klinik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Klinik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Klinik().setVisible(true);

55. 53 } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JCheckBox jCheckBox2; private javax.swing.JCheckBox jCheckBox3; private javax.swing.JCheckBox jCheckBox4; private javax.swing.JCheckBox jCheckBox5; private javax.swing.JCheckBox jCheckBox6; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField10; private javax.swing.JTextField jTextField11; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; private javax.swing.JTextField jTextField5; private javax.swing.JTextField jTextField6; private javax.swing.JTextField jTextField7; private javax.swing.JTextField jTextField8; private javax.swing.JTextField jTextField9; // End of variables declaration }

56. 54 B.Output :

57. 55 4. Latihan Membuat Aplikasi – 4 A. Input : import javax.swing.*; import java.awt.*; import java.sql.*; import java.awt.event.*; import javax.swing.table.DefaultTableModel; import javax.swing.JOptionPane; public class penjualan extends JFrame{ String[] kolom={"Kode","Nama Barang","Satuan","Harga","Stok","Stok Min"}; //memberi nama kolom DefaultTableModel dtm = new DefaultTableModel(null,kolom); JTable t = new JTable(); JScrollPane sp =new JScrollPane(t); JLabel l1=new JLabel("PROGRAM PENJUALAN"); JLabel l2=new JLabel("Kode Barang"); JLabel l3=new JLabel("Nama Barang"); JLabel l4=new JLabel("Satuan"); JLabel l5=new JLabel("Harga"); JLabel l6=new JLabel("Stok"); JLabel l7=new JLabel("Stok Minimal"); JTextField tf2=new JTextField(); JTextField tf3=new JTextField(); JTextField tf5=new JTextField(); JTextField tf6=new JTextField(); JTextField tf7=new JTextField(); JButton tb=new JButton("Tambah"); JButton s=new JButton("Simpan"); JButton k=new JButton("Koreksi"); JButton h=new JButton("Hapus"); JButton b=new JButton("Batal"); JButton kl=new JButton("Keluar"); JButton tr=new JButton("Tampil Report"); void tampil()//memangil tampil{ try{ Class.forName ("com.mysql.jdbc.Driver");//nama JDBC Connection c = DriverManager.getConnection("jdbc:mysql://localhost/penjualan","root",""); //lokasi database Statement s = c.createStatement(); String sql ="select * from barang";//memanggil fungsi select ResultSet rs = s.executeQuery(sql); while (rs.next()){ String kd_brg = rs.getString("kd_brg");//mengatur type data kolom String nm_brg = rs.getString("nm_brg");

58. 56 String satuan = rs.getString("satuan"); String harga = rs.getString("harga"); String stok = rs.getString("stok"); String stok_min = rs.getString("stok_min"); String [] barang = {kd_brg,nm_brg,satuan,harga,stok,stok_min}; dtm.addRow(barang); } rs.close(); c.close(); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Aktifkan XAMPP terlebih dahulu"); //menampilkan hasil eksekusi System.exit(0); } } public penjualan(){ final JFrame fr=new JFrame(); fr.setTitle("S6H UNINDRA_VISUAL"); String[]st={"Lusin","Box","Unit","Buah"}; final JComboBox sb= new JComboBox(st); l1.setBounds(280,10,300,25); l2.setBounds(20,50,100,25); tf2.setBounds(150,50,100,25); l3.setBounds(20,100,100,25);tf3.setBounds(150,100,200,25); l4.setBounds(20,150,100,25);sb.setBounds(150,150,70,25); l5.setBounds(20,200,100,25);tf5.setBounds(150,200,150,25); l6.setBounds(20,250,100,25);tf6.setBounds(150,250,70,25); l7.setBounds(20,300,100,25);tf7.setBounds(150,300,70,25); sp.setBounds (20,340,600,200); tb.setBounds (20,560,100,20);s.setBounds (130,560,100,20);k.setBounds (240,560,100,20); h.setBounds (350,560,100,20);b.setBounds (460,560,100,20);kl.setBounds (570,560,100,20);tr.setBounds (250,600,150,20); fr.add(l1);fr.add(sp);fr.add(tb);fr.add(s);fr.add(h);fr.add(k);fr.add(kl);fr.add(b);fr.add(tr); fr.add(l2);fr.ad

Related presentations


Other presentations created by kikismsf