Holla pengunjung blog pintar wandarizki..
Pada postingan yang sebelumnya saya sudah mengenalkan tentang manajemen layout beserta contoh programnya. Pada postingan kali ini kita akan melanjut pembahasan tentang manajemen layout Part II. Langsung saja kita ke contoh program nya.
1. Membuat Program FrameAWT.java
Syntax Program :
import java.awt.*;
public class frameAWT {
public static void main(String[] args) {
Frame myFrame = new Frame("Demo program AWT");
myFrame.setLayout(new BorderLayout());
Label labelPil = new Label("Tittle Pilihan Buah");
myFrame.add(labelPil,BorderLayout.NORTH);
Panel myPanel = new Panel(new BorderLayout());
Label labelBuah = new Label("Buah = ");
myPanel.add(labelBuah,BorderLayout.WEST);
Choice myChoice = new Choice();
myChoice.add("Mangga");
myChoice.add("Pisang");
myChoice.add("Jeruk");
myChoice.add("Apple");
myChoice.add("Anggur");
myChoice.add("Nangka");
myFrame.add(myPanel,BorderLayout.SOUTH);
myFrame.setSize(300,200);
myPanel.add(myChoice,BorderLayout.EAST);
myFrame.show();
}}
Output Program :
2. Membuat Program Objek GUI
Syntax Program :
import javax.swing.*;
class GUI extends JFrame {
JLabel lNama = new JLabel("Nama Lengkap : ");
final JTextField fNama = new JTextField(10);
JLabel lJnsKlmn = new JLabel("Jenis Kelamin : ");
JRadioButton rBPria = new JRadioButton("Laki-laki");
JRadioButton rBWanita = new JRadioButton("Perempuan");
JLabel lAgama = new JLabel("Agama : ");
String [] NamaAgama = {"Islam", "Kristen", "Katolik","Hindu", "Budha"};
JComboBox cMBAgama = new JComboBox(NamaAgama);
JLabel lHobby = new JLabel("Hobby : ");
JCheckBox cBSepakBola = new JCheckBox("Sepakbola");
JCheckBox cBBasket = new JCheckBox("Basket");
JButton bTNSave = new JButton("OK");
public GUI() {
setTitle("Mencoba kombinasi komponen GUI");
setDefaultCloseOperation(3);
setSize(350,200);
ButtonGroup Group = new ButtonGroup();
Group.add(rBPria);
Group.add(rBWanita);
setLayout(null);
add(lNama);
add(fNama);
add(lJnsKlmn);
add(rBPria);
add(rBWanita);
add(lAgama);
add(cMBAgama);
add(lHobby);
add(cBSepakBola);
add(cBBasket);
add(bTNSave);
lNama.setBounds(10,10,120,20);
fNama.setBounds(130,10,150,20);
lJnsKlmn.setBounds(10,35,120,20);
rBPria.setBounds(130,35,100,20);
rBWanita.setBounds(230,35,100,20);
lAgama.setBounds(10,60,150,20);
cMBAgama.setBounds(130,60,120,20);
lHobby.setBounds(10,85,120,20);
cBSepakBola.setBounds(130,85,100,20);
cBBasket.setBounds(230,85,150,20);
bTNSave.setBounds(100,130,120,20);
fNama.setBounds(130,10,150,20);
setVisible(true);
}
}
class ObjekGUI {
public static void main(String []args){
GUI G = new GUI();
}
}
Output Program :
3. Membuat Program Login
Syntax Program :
import javax.swing.*;
class Login extends JFrame {
JLabel lUser = new JLabel("User Name : ");
final JTextField fUser = new JTextField(10);
JLabel lPass = new JLabel("Password : ");
final JTextField fPass = new JTextField(10);
JButton bTNSave = new JButton("OK");
JButton bTNCancel = new JButton("CANCEL");
public Login() {
setTitle("Login");
setDefaultCloseOperation(3);
setSize(310,200);
setLayout(null);
add(lUser);
add(fUser);
add(lPass);
add(fPass);
add(bTNSave);
add(bTNCancel);
lUser.setBounds(10,10,120,20);
fUser.setBounds(130,10,150,20);
lPass.setBounds(10,35,120,20);
fPass.setBounds(130,35,150,20);
bTNSave.setBounds(50,75,80,20);
bTNCancel.setBounds(150,75,80,20);
setVisible(true);
}
}
class formLogin {
public static void main(String []args){
Login L = new Login();
}
}
Output Program :
4. Membuat Program MultiLayout
Syntax Program :
import java.awt.*;
import javax.swing.*;
class Multi extends JFrame{
JButton B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11;
JLabel lNama1,lNama2,lNama3;
JPanel P1,P2,P3;
public Multi() {
setTitle("MULTILAYOUT");
JLabel lNama1= new JLabel("Flow Layout" );
JButton B0=new JButton ("0");
JButton B1=new JButton ("1");
JButton B2=new JButton ("2");
JButton B3=new JButton ("3");
JLabel lNama2= new JLabel("None Layout" );
JButton B4=new JButton ("4");
JButton B5=new JButton ("5");
JButton B6=new JButton ("6");
JButton B7=new JButton ("7");
JLabel lNama3= new JLabel("Grid Layout" );
JButton B8=new JButton ("8");
JButton B9=new JButton ("9");
JButton B10=new JButton ("10");
JButton B11=new JButton ("11");
P1=new JPanel();
P2=new JPanel();
P3=new JPanel();
setLayout(null);
add(lNama1);
add(lNama2);
add(lNama3);
add(P1);
add(P2);
add(P3);
lNama1.setBounds(20,10,100,20);
lNama2.setBounds(220,10,100,20);
lNama3.setBounds(320,10,100,20);
P1.setBounds(5,30,200,40);
P2.setBounds(215,30,200,100);
P3.setBounds(320,30,200,150);
P1.setLayout(new FlowLayout());
P1.add(B0);
P1.add(B1);
P1.add(B2);
P1.add(B3);
P2.setLayout(null);
P2.add(B4);
P2.add(B5);
P2.add(B6);
P2.add(B7);
B4.setBounds(15,10,50,20);
B5.setBounds(15,30,50,20);
B6.setBounds(15,50,50,20);
B7.setBounds(15,70,50,20);
P3.setLayout(new GridLayout(2,2));
P3.add(B8);
P3.add(B9);
P3.add(B10);
P3.add(B11);
P3.setSize(90,90);
setSize(450,180);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class MultiLayout {
public static void main(String[] args) {
Multi l= new Multi();
}
}
Ouput Program :
5. Membuat Program Button
Syntax Program :
import javax.swing.*;
class Button extends JFrame {
JButton bTN1 = new JButton("BUTTON");
JButton bTN2 = new JButton("BUTTON");
JButton bTN3 = new JButton("BUTTON");
JButton bTN4 = new JButton("BUTTON");
JButton bTN5 = new JButton("BUTTON");
JButton bTN6 = new JButton("BUTTON");
JButton bTN7 = new JButton("BUTTON");
JButton bTN8 = new JButton("BUTTON");
JButton bTN9 = new JButton("BUTTON");
JButton bTN10 = new JButton("BUTTON");
public Button() {
setTitle("Menampilkan JButton");
setDefaultCloseOperation(3);
setSize(200,300);
setLayout(null);
add(bTN1);
add(bTN2);
add(bTN3);
add(bTN4);
add(bTN5);
add(bTN6);
add(bTN7);
add(bTN8);
add(bTN9);
add(bTN10);
bTN1.setBounds(30,10,120,20);
bTN2.setBounds(30,35,120,20);
bTN3.setBounds(30,60,120,20);
bTN4.setBounds(30,85,120,20);
bTN5.setBounds(30,110,120,20);
bTN6.setBounds(30,135,120,20);
bTN7.setBounds(30,160,120,20);
bTN8.setBounds(30,185,120,20);
bTN9.setBounds(30,210,120,20);
bTN10.setBounds(30,235,120,20);
setVisible(true);
}
}
class cobaButton {
public static void main(String []args){
Button B = new Button();
}
}
Output Program :
Oke Cukup Sekian Postingan Saya Kali ini ya. Semoga Bermanfaat.
Nantikan Postingan Saya Selanjutnya...
Wa'alaikumussalam warahmatullah
Tidak ada komentar:
Posting Komentar