1 / 31

Pemrograman JAVA (TIB09)

Pemrograman JAVA (TIB09). GUI. JFrame. Merupakan Container  menurunkan class Container JFrame dapat diaplikasikan pada class sebagai turunan public class Contoh extends javax.swing.Jframe { } Dapat juga diinstance sebagai class Di dalam Jframe dapat diletakkan komponen-komponen GUI.

xue
Télécharger la présentation

Pemrograman JAVA (TIB09)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pemrograman JAVA(TIB09) GUI

  2. JFrame • Merupakan Container  menurunkan class Container • JFrame dapat diaplikasikan pada class sebagai turunan public class Contoh extends javax.swing.Jframe { } • Dapat juga diinstance sebagai class • Di dalam Jframe dapat diletakkan komponen-komponen GUI

  3. Contoh Jframe diinheritance import java.awt.*; import javax.swing.*; public class JFrameExample extends JFrame{ public JFrameExample() { setTitle(“Contoh Jframe”); setSize(400,150); setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); setVisible(true); //atau show() } public static void main(String[] args) { JFrameExample w = new JFrameExample(); } }

  4. ContohJframediinstancesebagaiobjek import java.awt.*; import javax.swing.*; public class JFrameExample { public JFrameExample { JFrame f = new JFrame(“Contoh JFrame"); f.setSize(400, 150); f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); f.setVisible(true); //dapat diganti dengan f.show() } public static void main(String[] args) { JFrameExample w = new JFrameExample(); } }

  5. JFrame secara otomatis terdiri atas JRootPane dan title window. • JRootPane dapat diisi dengan komponen GUI seperti Button, image dsb

  6. Container • Untuk menambahkan komponen pada Frame maupun mengubah properti Frame, harus membuat sebuah container yang dapat dilakukan dengan method getContentPane()

  7. Contoh Container import java.awt.*; import javax.swing.*; public class JFrameExample { public static void main(String[] args) { WindowUtilities.setNativeLookAndFeel(); JFrame f = new JFrame(“Contoh JFrame"); f.setSize(400, 150); Container content = f.getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); content.add(new JButton("Button 1")); content.add(new JButton("Button 2")); content.add(new JButton("Button 3")); f.addWindowListener(new ExitListener()); f.setVisible(true); // dapat diganti dengan f.show() } }

  8. JLabel import java.awt.*; import javax.swing.*; public class JLabelExample { JFrame f; JLabel L1, L2, L3, L4; public JLabelExample() { f = new JFrame(“ContohJLabel"); L1 = new Jlabel (“Label pertama”); L2 = new Jlabel (“Label kedua”); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); L3 = new Jlabel (“Label denganAllignment”, SwingConstants.LEFT); L4 = new Jlabel (“Label denganAllignment”, SwingConstants.RIGHT); //penempatan pada container c.add(L1); c.add(L2); c.add(L3); c.add(L4); f.setSize(400, 150); f.setVisible(true); } public static void main(String[] args) { new JLabelExample(); } }

  9. JTextField import java.awt.*; import javax.swing.*; public class JTextFieldExample { JFrame f; JLabel L1; JTextField tF1; public JLabelExample() { f = new JFrame(“ContohJLabel"); L1 = new Jlabel (“Label pertama”); tF1 = new JTextField(25); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); //penempatan pada container c.add(L1); c.add(tF1); f.setSize(400, 150); f.setVisible(true); } public static void main(String[] args) { new JTextFieldExample(); } }

  10. JButton import java.awt.*; import javax.swing.*; public class JButtonExample { JFrame f; JLabel L1; JTextField tF1; JButton b1, b2; public JButtonExample() { f = new JFrame(“ContohJLabel"); L1 = new Jlabel (“Label pertama”); tF1 = new JTextField(25); b1 = new JButton("OK"); b2 = new JButton("cancel"); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); //penempatan pada container c.add(L1); c.add(tF1); c.add(b1); c.add(b2); f.setSize(400, 150); f.setVisible(true); } public static void main(String[] args) { new JButtonExample(); } }

  11. JPanel • Jpanel adalah komponen container, sehingga dapat mengakomodasi komponen-komponen lainnya. • Dapat mempunyai Layout sendiri • Secara default layout dari JPanel adalah FlowLayout.

  12. Layout Manager • Berhubungan dengan penempatan komponen pada container. • Jenis-jenis Layout: • BorderLayout • FlowLayout • GridLayout • CardLayout • Layout Null

  13. BorderLayout • Mempunyai 5 area yang namanyamengikutinamaarahmataangin NORTH, WEST, CENTER, EAST, SOUTH • Contoh: Container c = f.getContentPane(); c.setLayout(new BorderLayout()); c.add(BorderLayout.NORTH, komponen1); c.add(BorderLayout.WEST, komponen2); c.add(BorderLayout.CENTER, komponen3); c.add(BorderLayout.EAST, komponen4); c.add(BorderLayout.SOUTH, komponen5);

  14. FlowLayout • Komponendisusundarikirikekanandandariataskebawah • FlowLayoutmempunyaiallignment LEFT, CENTER dan RIGHT; default adalah CENTER • Contoh c.setLayout(new FlowLayout()); c.add(L1); c.add(tF1); c.add(b1); c.add(b2); • Dapatditambahkan parameter hgapdanvgapuntukmemberikanjarakantarasatukomponendengankomponenlainnyadalamsatuanpiksel • Contoh: c.setLayout(new FlowLayout(FlowLayout.RIGHT,40,8));

  15. GridLayout • Menempatkan komponen pada baris dan kolom sesuai dengan dimensi yang ditentukan • Beberapa konstruktor: • GridLayout(int baris, int kolom); • GridLayout(int baris, int kolom, int hgap, int vgap) • Pembagian sel sama besar

  16. GridBagLayout • Seperti GridLayout tetapi setiap sel dapat mempunyai ukuran yang berbeda sesuai dengan komponen yang menempatinya. • Intruksi pack() pada frame dalam pemakaian GridBagLayout akan membuat layar menjadi kompak sesuai dengan ukuran komponen yang di dalamnya.

  17. GridBagConstraint • Setiap komponen yang dimasukkan ke dalam container dapat mempunyai parameter untuk memberikan ukuran yang diinginkan • gridheight  tinggi grid • gridwidth  lebar grid • gridx dan gridy  koordinat x dan y dalam penempatan komponen • Contoh: GridBagConstraint gbc = new GridBagConstraint(); Container c = f.getContentPane(); c.setLayout(new GridBagLayout()); gbc.gridx = 0; gbc.gridy = 0; gbc.gridheight = 1; gbc.gridwidth = 2; c.add(komponen, gbc);

  18. CardLayout • Menampilkan window dalam bentuk panel • Dapat terdiri dari satu atau beberapa panel. • Perpindahan panel satu dengan panel lainnya dapat dilakukan dengan first(), last(), next() dan previous() • Bentuk lain dari CardLayout yang lebih baik lagi adalah dengan mempergunakan JTabbedPane

  19. Contoh CardLayout Container c = f.getContentPane(); CardLayout card = new CardLayout(); c.setLayout (card); c.add(komponen1, “Card 1”); c.add(komponen2, “Card 2”); c.add(komponen3, “Card 3”); c.add(komponen4, “Card 4”); c.add(komponen5, “Card 5”); c.add(komponen6, “Card 6”);

  20. JTabbedPane JTabbedPanetabbedPane = new JTabbedPane(); ImageIcon icon = createImageIcon("images/middle.gif"); JComponent panel1 = makeTextPanel("Panel #1"); tabbedPane.addTab("Tab 1", icon, panel1, "Does nothing"); tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); JComponent panel2 = makeTextPanel("Panel #2"); tabbedPane.addTab("Tab 2", icon, panel2, "Does twice as much nothing"); tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); JComponent panel3 = makeTextPanel("Panel #3"); tabbedPane.addTab("Tab 3", icon, panel3, "Still does nothing"); tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); JComponent panel4 = makeTextPanel("Panel #4 (has a preferred size of 410 x 50)."); panel4.setPreferredSize(new Dimension(410, 50)); tabbedPane.addTab("Tab 4", icon, panel4, "Does nothing at all"); tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);

  21. Layout Null • Denganmemberikannilainullpada parameter dalam Method setLayoutmemungkinkanuntukmenentukansendiripenempatankomponendengankoordinatabsolut X, Y • Penempatankomponendapatdilakukandengan method setBounds(int x, int y, int w, int h);\ • Contoh: c = f.getContentPane(); c.setLayout(null); komponen1 = new Jbutton(“Contoh”); c.add(komponen1); komponen1.setBounds(21,7,50,25);

  22. Komponen Java swing lainnya • JCheckBox • JRadioButton • JList • JScrollPane • JComboBox • dsb

  23. Window Dialog • Subwindow yang diasosiasikandenganaplikasidanhanyamunculbiladibutuhkan • Java Swing menyediakan class JOptionPane • Beberapa method yang disediakanuntukmelayani dialog: • showMessageDialog() • showConfirmDialog() • showOptionDialog() • showInputDialog()

  24. showMessagedialog import javax.swing.*; import java.awt.*; public class contohDialog { public static void main(String[] args) { Jframelayar = new Jframe(“Message Dialog); JOptionPane.showMessageDialog(layar, “Press OK to continue”); layar.setSize(200,80); layar.setLocation(60,60); layar.setVisible(true); layar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); . . } }

  25. Menu • Menu ditempatkanpadasebuah menu bar. • Java Swing menyediakan class JMenuBaruntukmenanganipenyediaan bar menu. JMenuBarnamaBarMenu = new JMenuBar(); • JMenuBarmerupakanbagiandariJComponent. • Untukmenempatkan bar menu pada frame dilakukandengan method setJMenuBar() f.setJMenuBar(namaBarMenu);

  26. Menambahkan menu • Menu yang akanditambahkanpada bar menu harusdibuatdahuluobjeknyadari class JMenu JMenunamaMenu = JMenu(“Teks pd menu”); • Setelahitubaruditambahkanpada bar menu dengan method add() namaBarMenu.add(namaMenu); • Penambahan menu pada bar menu harusdibuatobjeknyadengannama yang berbeda!

  27. submenu • Dalamsetiap menu dapatditambahkan submenu yang dibentukdari class JMenuItem. JMenuItem namaSubMenu1 = new JMenuItem(“submenu satu”); JMenuItem namaSubMenu2 = new JMenuItem(“submenu dua”); JMenuItem namaSubMenu3 = new JMenuItem(“submenu tiga”); • Penambahan submenu dilakukanlangsungpada menu yang menjadi menu induknya. namaMenu.add(namaSubMenu1); namaMenu.add(namaSubMenu2); namaMenu.add(namaSubMenu3);

  28. addSeparator • Jika diperlukan pemisah antara sub menu, maka dapat dipanggil metoda addSeparator() pada sub menu ybs.

  29. Isue menu yang lain • JCheckBoxMenuItem

  30. Tugas Mandiri Bikin Presentasi untuk menjelaskan Salah satu Class ataupun Method berikut ini: (materi yang anda bahas sesuai dengan angka terakhir NIM anda – untuk nim yang berakhir dengan 0 dan 9 kerjakan materi nomor 9 • JCheckBox • JRadioButton • JList • JScrollPane • JComboBox • showConfirmDialog() • showOptionDialog() • showInputDialog() • JCheckBoxMenuItem

More Related