Tuesday, October 21, 2008

Bermain dengan Gridbagconstraint di java

Property yang ada di class Gridbagconstraint antara lain :
- gridx -> digunakan untuk menentukan posisi x dr komponen
- gridy -> digunakan untuk menentukan posisi y dr komponen
- anchor -> digunakan jika komponen lebih kecil dari display area
- insets -> digunakan untuk menentukan external padding dari komponen, minimum space antara komponen dan ujung dari display area
- gridheight -> digunakan untuk menentukan jumlah sel di suatu kolom untuk display area komponen
- gridwidth -> digunakan untuk menentukan jumlah sel di baris untuk display area komponen

class Gridbagconstraint ini kita coba untuk di test di program yang kita beri nama "MyGridBagConstraints1.java" sbb :

Download Source :
esnips.com/doc/d4d4254d-46a8-4e8e-9f50-8ee70c7e8b17/MyGridBagConstraints1
=================================================================================================
package test1;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Insets;


import javax.swing.JFrame;

public class MyGridBagConstraints1 extends JFrame{

/**
* @param args
*/
private Label lblName = null;
private TextField txfName = null;
private Label lblAddress = null;
private TextField txfAddress = null;
private Label lblAge = null;
private TextField txfAge = null;
private Label lblWeight = null;
private TextField txfWeight = null;
private Label lblDescription = null;
private TextArea txaDescription = null;
private Panel pnlWeightAge = null;

public MyGridBagConstraints1() {
super("My Title");
initialize();
}

public void initialize(){

GridBagConstraints gbcLblName = new GridBagConstraints();
gbcLblName.gridx = 0;
gbcLblName.gridy = 1;
gbcLblName.anchor = GridBagConstraints.WEST;
gbcLblName.weighty = 0.1;
lblName = new Label();
lblName.setText("Name");
GridBagConstraints gbcTxfName = new GridBagConstraints();
gbcTxfName.gridx = 1;
gbcTxfName.gridy = 1;
gbcTxfName.anchor = GridBagConstraints.WEST;
gbcTxfName.insets = new Insets(0,10,0,5);
gbcTxfName.weightx = 0.1;

GridBagConstraints gbcLblAddress = new GridBagConstraints();
gbcLblAddress.gridx = 0;
gbcLblAddress.gridy = 2;
gbcLblAddress.anchor = GridBagConstraints.WEST;
gbcLblAddress.weighty = 0.1;
lblAddress = new Label();
lblAddress.setText("Address");
GridBagConstraints gbcTxfAddress = new GridBagConstraints();
gbcTxfAddress.gridx = 1;
gbcTxfAddress.gridy = 2;
gbcTxfAddress.anchor = GridBagConstraints.WEST;
gbcTxfAddress.insets = new Insets(0,10,0,5);
gbcTxfAddress.weightx = 0.1;

GridBagConstraints gbcLblAge = new GridBagConstraints();
gbcLblAge.gridx = 0;
gbcLblAge.gridy = 3;
gbcLblAge.anchor = GridBagConstraints.WEST;
gbcLblAge.weighty = 0.1;
lblAge = new Label();
lblAge.setText("Age");
GridBagConstraints gbcPnlWeightAge = new GridBagConstraints();
gbcPnlWeightAge.gridx = 1;
gbcPnlWeightAge.gridy = 3;
gbcPnlWeightAge.anchor = GridBagConstraints.WEST;
gbcPnlWeightAge.insets = new Insets(0,10,0,5);
gbcPnlWeightAge.weighty = 0.1;

GridBagConstraints gbcLblDescription = new GridBagConstraints();
gbcLblDescription.gridx = 0;
gbcLblDescription.gridy = 4;
gbcLblDescription.anchor = GridBagConstraints.WEST;
gbcLblDescription.weighty = 0.1;
lblDescription = new Label();
lblDescription.setText("description");
GridBagConstraints gbcTxaDescription = new GridBagConstraints();
gbcTxaDescription.gridx = 1;
gbcTxaDescription.gridy = 4;
gbcTxaDescription.anchor = GridBagConstraints.WEST;
gbcTxaDescription.insets = new Insets(0,10,0,5);
gbcTxaDescription.weightx = 0.1;

this.setLayout(new GridBagLayout());
this.setSize(new Dimension(695, 300));
this.add(lblName, gbcLblName);
this.add(getTxfName(), gbcTxfName);
this.add(lblAddress, gbcLblAddress);
this.add(getTxtAddress(), gbcTxfAddress);
this.add(lblAge, gbcLblAge);
this.add(getPnlWeightAge() , gbcPnlWeightAge);
this.add(lblDescription, gbcLblDescription);
this.add(getTxaDescription(), gbcTxaDescription);

}
private Panel getPnlWeightAge() {
if (pnlWeightAge == null) {
lblWeight = new Label();
lblWeight.setText("Weight");
pnlWeightAge = new Panel();
pnlWeightAge.add(getTxtAge(), null);
pnlWeightAge.add(lblWeight, null);
pnlWeightAge.add(getTxtWeight(), null);
}
return pnlWeightAge;
}

private TextField getTxfName() {
if (txfName == null) {
txfName = new TextField();
txfName.setColumns(30);
txfName.setName("name");
}
return txfName;
}

private TextField getTxtAddress() {
if (txfAddress == null) {
txfAddress = new TextField();
txfAddress.setColumns(40);
txfAddress.setName("address");
}
return txfAddress;
}

private TextField getTxtAge() {
if (txfAge == null) {
txfAge = new TextField();
txfAge.setColumns(10);
txfAge.setName("age");
}
return txfAge;
}

private TextField getTxtWeight() {
if (txfWeight == null) {
txfWeight = new TextField();
txfWeight.setColumns(10);
txfWeight .setName("weight");
}
return txfWeight;
}

private TextArea getTxaDescription() {
if (txaDescription == null) {
txaDescription = new TextArea();
txaDescription.setName("addRemark");
txaDescription .setRows(4);
txaDescription.setColumns(40);
}
return txaDescription;
}
}


=================================================================================================

No comments: