Selasar Inspirasi

GUI Dasar





1. Komponen Swing dan Manajemen Layout
a. Membuat Frame
Ada 2 cara untuk membuat frame :
- Membuat class yang objek utamanya diinstansiasi dari class JFrame
Contoh:
JFrame frame = new JFrame (“teks….”);

- Membuat class yang diturunkan dari class JFrame.
Contoh:
class Frame extends JFrame { 
}

b. Meletakkan Komponen ke Frame
- Mendeklarasikan komponen-komponen
JLabel namaKomponen = new JLabel (“teks…”);

c. Memilih Layout
- Cara 1 :
getContentPane ().setLayout (new FlowLayout());
- Cara 2 :
Container konten = getContentPane();
Konten.setLayout (new FlowLayout());
- Cara 3 :
frame.add (nama Komponen);

d. Memasukkan komponen-komponen ke Frame
- Cara 1 :
getContentPane().add(namaKomponen);
- Cara 2 :
Konten.add(namaKomponen);

> Pengaplikasian

1. Text Field
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class TextField {
    private JFrame layar;
    private JLabel l1,l2;
    private JTextField tf1;
    private JPasswordField psw;
    private JButton b1,b2;
    
    //membuat instance dari kelas-kelas tersebut
    public TextField(){
        layar = new JFrame ("Text Field");
        l1 = new JLabel ("Username : ");
        l2 = new JLabel ("Password : ");
        tf1 = new JTextField (25);
        psw = new JPasswordField (25);
        b1 = new JButton("OK");
        b2 = new JButton ("Cancel");
        
        Container c = layar.getContentPane();
        c.setLayout(new FlowLayout());
        c.add(l1);
        c.add(tf1);
        c.add(l2);
        c.add(psw);
        c.add(b1);
        c.add(b2);
        
  b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
  JOptionPane.showMessageDialog(null, "Pastikan Data diisi dengan Benar");
            }
        });
        
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                tf1.setText(null);
                psw.setText(null);
            }
        });


        layar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        layar.setSize(380,150);
        layar.setVisible(true);
    }
    
    public static void main(String[] args) {
        new TextField();
    }
}

program text field sederhana menggunakan java


2. Form Checlist
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CheckList extends JFrame{
    
    //membuat instance baru dari CheckList
    public CheckList(){
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    panel1.setLayout(new GridLayout(3,1));
    panel2.setLayout(new GridLayout(3,1));
    Container c = this.getContentPane();
    c.setLayout(new GridLayout(1,2));
    c.add(panel1);
    c.add(panel2);
    
    JCheckBox cb1 = new JCheckBox("WIndows");
    JCheckBox cb2 = new JCheckBox("Linux");
    JCheckBox cb3 = new JCheckBox("Max OS");
    
    panel1.setBorder(BorderFactory.createTitledBorder("Sistem Operasi"));
    panel1.add(cb1);
    panel1.add(cb2);
    panel1.add(cb3);
    
    JRadioButton rb1 = new JRadioButton("Distributor");
    JRadioButton rb2 = new JRadioButton("Reseller");
    JRadioButton rb3 = new JRadioButton("Partner");
    
    //daftarkan setiap radiobutton sebagai anggota grup
    ButtonGroup rbgroup = new ButtonGroup();
    rbgroup.add(rb1);
    rbgroup.add(rb2);
    rbgroup.add(rb3);
    
    panel2.setBorder(BorderFactory.createTitledBorder("Status"));
    panel2.add(rb1);
    panel2.add(rb2);
    panel2.add(rb3);
    
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(60,60);
        this.setSize(300,200);
        this.setVisible(true);
    }
    
    public static void main(String[] args) {
        new CheckList();
    } 
}

program checklist sederhana menggunakan java

3. Kuas
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Kuas extends JPanel{
    private Point posisi;
    private Color warna;
    
    public Kuas(){
        addMouseMotionListener(new MMListener());
    }
    
    class MMListener extends MouseMotionAdapter {
        //tambahkan kode untuk tombol mouse
        public void mouseDragged(MouseEvent ev) {
            if (ev.isMetaDown()){
                System.out.println("Right clicked");
                posisi = ev.getPoint();
                warna = getBackground();
                repaint();
            }
            else {
                posisi = ev.getPoint();
                System.out.println("Repaint and clicked at "
                + posisi.x + " "+posisi.y);
                warna = Color.RED;
                repaint();
        }
    }
}
    
    public void paintComponent (Graphics g){
        g.setColor(warna);
        if (posisi != null){
            g.fillOval(posisi.x, posisi.y, 10, 10);
        }
    }
    
    public static void main(String[] args) {
        JFrame layar = new JFrame();
        Kuas m = new Kuas();
        m.setSize(300,300);
        Container c = layar.getContentPane();
        c.setLayout(null);
        c.add(m);
        layar.setLocation(200,200);
        layar.setSize(300,300);
        layar.setTitle("Program Kuas");
        layar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        layar.show();
        
    }
}

program kuas dengan java


Referensi 
Pemrograman Berorientasi Objek dengan Java – Novrido Charibaldi, Agus Sasmito Aribowo


0 Comments
Tweets
Comments

Responses

0 Respones to "GUI Dasar"

Posting Komentar

 
Return to top of page Copyright © 2010 | Platinum Theme Converted into Blogger Template by HackTutors