TUGAS PRAKTIKUM
PEMROGRAMAN JAVA II
MANAJEMEN LAYOUT
Disusun oleh :
D. Wahyu Baswara Seta
123080043 / Plug 10
Asisten / Coas :
Shiddieqy Y.Katili / Dimas A.
JURUSAN TEKNIK INFORMATIKA
FAKULTAS TEKNOLOGI INDUSTRI
UNIVERSITAS PEMBANGUNAN NASIONAL “VETERAN” YOGYAKARTA
2010
BAB I
TUGAS
1. MultiLayout
import java.awt.*; import javax.swing.*;
class pMultiLayout extends JFrame{ JButton b1,b2,b3,b4,b5,b6,b7; JLabel lbl; public pMultiLayout(){
super("MultiLayout"); setSize(400, 400); setDefaultCloseOperation(3); b1=new JButton("B1"); b2=new JButton("B2"); b3=new JButton("B3"); b4=new JButton("B4"); b5=new JButton("B5"); b6=new JButton("B6"); b7=new JButton("B7"); lbl=new JLabel(new ImageIcon("src/Image/1.jpg"));
JPanel p=new JPanel(); add(p,"North"); p.setLayout(new BorderLayout()); p.add(b1,"West"); p.add(b2,"Center"); p.add(b3,"East");
JPanel p1=new JPanel(); add(p1,"East"); p1.setLayout(new BorderLayout()); p1.add(lbl,"Center"); p1.add(b6,"East"); setVisible(true);
JPanel p2=new JPanel(); add(p2,"West"); p2.setLayout(new GridLayout(2,1)); p2.add(b4); p2.add(b5);
JPanel p3=new JPanel(new BorderLayout()); add(p3,"South"); p3.add(b7); pack();
}
public static void main(String[] args) { new pMultiLayout(); } } |
Ouput :
Penjelasan :
import java.awt.*; //untuk fungsi layout
import javax.swing.*;//untuk fungsi seperti Jpanel,Jlabel dll
class pMultiLayout extends JFrame{// kelas
JButton b1,b2,b3,b4,b5,b6,b7; //pendeklarasian button
JLabel lbl; // pendeklarasian label
public pMultiLayout(){//method
super("MultiLayout");//menampilkan judul
setSize(400, 400);//mengatur ukuran frame (lebar,tinggi)
setDefaultCloseOperation(3);//menutup frame
b1=new JButton("B1");//pengistansiasi komponen
b2=new JButton("B2");
b3=new JButton("B3");
b4=new JButton("B4");
b5=new JButton("B5");
b6=new JButton("B6");
b7=new JButton("B7");
lbl=new JLabel(new ImageIcon("src/Image/1.jpg"));//pengistansiasi komponen label dengan menampilkan gambar
JPanel p=new JPanel();//mengistansiasi komponen JPanel
add(p,"North");//menambahkan panel dengan nama p di sebelah utara
p.setLayout(new BorderLayout());//menambahkan borderLayout pada panel
p.add(b1,"West");//menambahkan b1 di sebelah barat panel
p.add(b2,"Center");// menambahkan b2 di tengah panel
p.add(b3,"East");// menambahkan b3 di sebelah timur panel
JPanel p1=new JPanel();//mengistansiasi komponen Jpanel 1
add(p1,"East");// menambahkan panel1 dengan nama p1 di sebelah timur
p1.setLayout(new BorderLayout());//menambahkan borderLayout padapanel
p1.add(lbl,"Center");// menambahkan lbl (gambar) di tengah panel1
p1.add(b6,"East");//menambahkan b6 di sebelah timur layout
setVisible(true);//menampilkan frame
JPanel p2=new JPanel();//mengistansiasi komponen Jpanel 2
add(p2,"West");//menambahkan panel2 dengan nama p2 di barat
p2.setLayout(new GridLayout(2,1));// menambahkan gridLayout padapanel
p2.add(b4);//menambahkan b4 pada panel2
p2.add(b5);
JPanel p3=new JPanel(new BorderLayout());//menambahkan borderLayout padapanel3
add(p3,"South");//Borderlayout diletakkan di sebelah selatan
p3.add(b7);//menambahkan b7 pada b3
pack();//mengatur ukuran panel secara otomatis
}
public static void main(String[] args) { //metod main
new pMultiLayout(); //objek yang digunakan untuk memanggil construktor
}}
2. GridBagLayout
import java.awt.*; import javax.swing.*;
class GLayout extends JFrame {
JButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, bt11; JPanel pn1, pn2, p3; GridBagLayout layout; BorderLayout border; GridBagConstraints constraint;//u ngantur kmponen layout gridbag
public GLayout() { initPref(); // metod bt1 = new JButton("B1"); bt2 = new JButton("B2"); bt3 = new JButton("B3"); bt4 = new JButton("B4"); bt5 = new JButton("B5"); bt6 = new JButton("B6"); bt7 = new JButton("B7"); bt8 = new JButton("B8"); bt9 = new JButton("B9"); bt10 = new JButton("B10"); bt11 = new JButton("B11");
pn1 = new JPanel(); pn2 = new JPanel(); //border = new BorderLayout(); layout = new GridBagLayout(); constraint = new GridBagConstraints(); constraint.insets = new Insets(5, 5, 5, 5); // mngtur jarak antar kmponen constraint.fill = GridBagConstraints.BOTH; // mengisi setLayout(layout);
addkomponen(bt1, 0, 0, 1, 1, 0, 0); addkomponen(bt2, 0, 1, 1, 1, 0, 0); addkomponen(bt3, 0, 2, 1, 1, 0, 0); addkomponen(bt4, 1, 0, 2, 3, 50,0); addkomponen(bt5, 3, 0, 1, 1, 0, 0); addkomponen(bt6, 3, 1, 1, 2, 0, 0); addkomponen(bt7, 0, 3, 5, 1, 0, 0); addkomponen(bt8, 0, 4, 1, 1, 0, 0); addkomponen(bt9, 1, 4, 1, 1, 0, 0); addkomponen(bt10, 2, 4, 1, 1,0, 0); addkomponen(bt11, 3, 4, 1, 1,0, 0); }
public void addkomponen(JComponent comp, int kolom, int baris, int lebar, int tinggi, int lebarmanual, int tinggimanual) { constraint.gridx = kolom; constraint.gridy = baris; constraint.gridwidth = lebar; constraint.gridheight = tinggi; constraint.ipadx = lebarmanual; constraint.ipady = tinggimanual; layout.setConstraints(comp, constraint); add(comp); }
private void initPref() { setTitle("GridBagLayout"); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); setSize(300,250);
} }
public class GridbagLayout {
public static void main(String[] args) { new GLayout(); } } |
Output :
Penjelasan :
import java.awt.*;
import javax.swing.*;
class GLayout extends JFrame {//merupakan suatu kelas
JButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, bt11;
JPanel pn1, pn2, p3;
GridBagLayout layout;// pendeklarasian GridBaglayout dg nama layout
BorderLayout border; // pendeklarasian borderlayout dg nama border
GridBagConstraints constraint;//untuk mengantur komponen layout
public GLayout() { // constructor
initPref(); // metod
bt1 = new JButton("B1");
bt2 = new JButton("B2");
bt3 = new JButton("B3");
bt4 = new JButton("B4");
bt5 = new JButton("B5");
bt6 = new JButton("B6");
bt7 = new JButton("B7");
bt8 = new JButton("B8");
bt9 = new JButton("B9");
bt10 = new JButton("B10");
bt11 = new JButton("B11");
pn1 = new JPanel();
pn2 = new JPanel();
layout = new GridBagLayout(); //instansiasi objek
constraint = new GridBagConstraints();
constraint.insets = new Insets(5, 5, 5, 5); // mengatur jarak antar komponen
constraint.fill = GridBagConstraints.BOTH; // mengisikomponen
setLayout(layout);
addkomponen(bt1, 0, 0, 1, 1, 0, 0);//menambahkan komponen bt1 dan sekaligus mengatur letak berdasarka posisi (kolom,baris,lebar,tinggi,lebar manual,tinggi manual).
addkomponen(bt2, 0, 1, 1, 1, 0, 0);
addkomponen(bt3, 0, 2, 1, 1, 0, 0);
addkomponen(bt4, 1, 0, 2, 3, 50,0);
addkomponen(bt5, 3, 0, 1, 1, 0, 0);
addkomponen(bt6, 3, 1, 1, 2, 0, 0);
addkomponen(bt7, 0, 3, 5, 1, 0, 0);
addkomponen(bt8, 0, 4, 1, 1, 0, 0);
addkomponen(bt9, 1, 4, 1, 1, 0, 0);
addkomponen(bt10, 2, 4, 1, 1,0, 0);
addkomponen(bt11, 3, 4, 1, 1,0, 0);
}
public void addkomponen(JComponent comp, int kolom, int baris, int lebar, int tinggi, int lebarmanual, int tinggimanual) {//metod
constraint.gridx = kolom; //mengatur kolom
constraint.gridy = baris;//mengatur baris
constraint.gridwidth = lebar;// mengatur lebar
constraint.gridheight = tinggi;//mengatur tinggi
constraint.ipadx = lebarmanual;// mengatur lebar manual
constraint.ipady = tinggimanual;// mengatur tinggi manual
layout.setConstraints(comp, constraint);//mengatur layout secara keseluruhan
add(comp);//menambhkan komponen
}
private void initPref() { //metod
setTitle("GridBagLayout");//menampilkan judul layout
setDefaultCloseOperation(EXIT_ON_CLOSE);//menutup frame
setVisible(true);//(menampilkan frame)
setSize(300,250);//mengatur ukuran frame
}}
public class GridbagLayout {//kelas
public static void main(String[] args) { // metod main
new GLayout();//objek yang memanggil kelas
}}
Tidak ada komentar:
Posting Komentar