LA DECIMA
Kamis, 22 Mei 2014
membuat form biodata (PBO 2)
cara mendesain form biodata, berikut scriptnya :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
public class FormBiodata extends JFrame implements ActionListener, ItemListener, MouseListener, MouseMotionListener{
// Pendeklarasian atribut
@SuppressWarnings("unchecked")
JLabel lblNama, lblAlamat, lblJnsKelamin, lblAgama, lblHobi, lblFonts, lblMouse;
JTextField txtNama;
JTextArea txtAlamat, txtResult;
JScrollPane spAlamat, spResult;
JRadioButton rbLaki, rbPerempuan, rbHitam, rbMerah, rbBiru;
ButtonGroup grupKelamin, grupWarna;
JComboBox cboAgama, cboFonts,cboSize;
JCheckBox cbMakan, cbBaca, cbJalan, cbBold, cbItalic, cbUnderline;
JButton cmdBatal, cmdSimpan, cmdClear;
Font font;
int valBold = Font.PLAIN;
int valItalic = Font.PLAIN;
String itemAgama[] = {"Islam","Kristen","Katolik","Hindu","Budha","Konghuchu"};
String itemFonts[] = {"Arial","Arial Narrow","Bridgnorth","Calibri","Courier New","EngrvrsOldEng BT","Fraktur BT","Kingthings Petrock","Monotype Corsiva","Times New Roman", "Tahoma"};
String itemSize[] = {"12","14","16","18","20","22","24","26","28","36","48","72"};
// Pendeklarasian Konstruktor
public FormBiodata(){
setDefaultLookAndFeelDecorated(true);
setTitle("Formulir Biodata...");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(450,520);
setLocationRelativeTo(null);
// Inisislisasi Komponen
lblNama = new JLabel("Nama");
add(lblNama);
lblNama.setBounds(10,10,150,20);
lblAlamat = new JLabel("Alamat");
add(lblAlamat);
lblAlamat.setBounds(10,40,150,40);
lblJnsKelamin = new JLabel("Jenis Kelamin");
add(lblJnsKelamin);
lblJnsKelamin.setBounds(10,90,150,20);
lblAgama = new JLabel("Agama");
add(lblAgama);
lblAgama.setBounds(10,125,150,20);
lblHobi = new JLabel("Hobi");
add(lblHobi);
lblHobi.setBounds(10,160,150,20);
txtNama = new JTextField();
add(txtNama);
txtNama.setBounds(120,10,200,20);
txtAlamat = new JTextArea();
spAlamat = new JScrollPane(txtAlamat);
add(spAlamat);
spAlamat.setBounds(120,40,300,40);
rbLaki = new JRadioButton("Laki-laki",true);
add(rbLaki);
rbLaki.setBounds(120,90,100,20);
rbPerempuan = new JRadioButton("Perempuan");
add(rbPerempuan);
rbPerempuan.setBounds(220,90,100,20);
grupKelamin = new ButtonGroup();
grupKelamin.add(rbLaki);
grupKelamin.add(rbPerempuan);
cboAgama = new JComboBox(itemAgama);
add(cboAgama);
cboAgama.setBounds(120,125,140,20);
cbMakan = new JCheckBox("Makan");
add(cbMakan);
cbMakan.setBounds(120,160,100,20);
cbBaca = new JCheckBox("Baca");
add(cbBaca);
cbBaca.setBounds(220,160,100,20);
cbJalan = new JCheckBox("Jalan-jalan");
add(cbJalan);
cbJalan.setBounds(320,160,100,20);
cmdSimpan = new JButton("Simpan");
add(cmdSimpan);
cmdSimpan.setBounds(40,200,100,30);
cmdClear = new JButton("Clear");
add(cmdClear);
cmdClear.setBounds(170,200,100,30);
cmdBatal = new JButton("Keluar");
add(cmdBatal);
cmdBatal.setBounds(300,200,100,30);
lblFonts = new JLabel("Pilih Fonts :");
add(lblFonts);
lblFonts.setBounds(120,250,150,20);
cboFonts = new JComboBox(itemFonts);
add(cboFonts);
cboFonts.setBounds(200,250,160,20);
cboSize = new JComboBox(itemSize);
add(cboSize);
cboSize.setBounds(370,250,50,20);
txtResult = new JTextArea();
spResult = new JScrollPane(txtResult);
add(spResult);
spResult.setBounds(120,280,300,170);
cbBold = new JCheckBox("Bold");
add(cbBold);
cbBold.setBounds(10,280,80,20);
cbItalic = new JCheckBox("Italic");
add(cbItalic);
cbItalic.setBounds(10,305,80,20);
cbUnderline = new JCheckBox("Underline");
add(cbUnderline);
cbUnderline.setBounds(10,330,80,20);
rbHitam = new JRadioButton("Hitam",true);
add(rbHitam);
rbHitam.setBounds(10,370,80,20);
rbMerah = new JRadioButton("Merah");
add(rbMerah);
rbMerah.setBounds(10,395,80,20);
rbBiru = new JRadioButton("Biru");
add(rbBiru);
rbBiru.setBounds(10,420,80,20);
grupWarna = new ButtonGroup();
grupWarna.add(rbHitam);
grupWarna.add(rbMerah);
grupWarna.add(rbBiru);
lblMouse = new JLabel("Informasi Posisi Mouse : ");
add(lblMouse);
lblMouse.setBounds(10,380,300,170);
// Pemberian Event Source mengarahkan ke method MouseListener
addMouseListener(this);
// Pemberian Event Source mengarahkan ke method MouseMotionListener
addMouseMotionListener(this);
// Pemberian Event Source mengarahkan ke method ActionListener
cmdSimpan.addActionListener(this);
cmdClear.addActionListener(this);
cmdBatal.addActionListener(this);
cboFonts.addActionListener(this);
cboSize.addActionListener(this);
// Pemberian Event Source mengarahkan ke method ItemListener
cbBold.addItemListener(this);
cbItalic.addItemListener(this);
cbUnderline.addItemListener(this);
rbHitam.addItemListener(this);
rbMerah.addItemListener(this);
rbBiru.addItemListener(this);
}
// Event Listener dengan Interface ActionListener
public void actionPerformed(ActionEvent event) {
if (event.getSource()== cmdBatal) keluarSystem();
else if (event.getSource()== cmdSimpan) simpanText();
else if (event.getSource()== cmdClear) bersihkanText();
else if (event.getSource() == cboFonts) jenisFonts();
else if (event.getSource() == cboSize) jenisFonts();
}
// Event Listener dengan Interface ItemListener
public void itemStateChanged(ItemEvent item) {
// Bold, Italic
if ( item.getSource() == cbBold ){
valBold = cbBold.isSelected() ? Font.BOLD : Font.PLAIN;
int size = new Integer(cboSize.getSelectedItem()+"");
txtResult.setFont( new Font(cboFonts.getSelectedItem().toString(), valBold + valItalic, size ) );
}
if ( item.getSource() == cbItalic ){
valItalic = cbItalic.isSelected() ? Font.ITALIC : Font.PLAIN;
int size = new Integer(cboSize.getSelectedItem()+"");
txtResult.setFont( new Font(cboFonts.getSelectedItem().toString(), valBold + valItalic, size ) );
}
// Underline
if (cbUnderline.isSelected()){
int size = new Integer(cboSize.getSelectedItem()+"");
font = new Font(cboFonts.getSelectedItem().toString(),valBold + valItalic,size);
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
txtResult.setFont(font.deriveFont(attributes));
}else{
int size = new Integer(cboSize.getSelectedItem()+"");
font = new Font(cboFonts.getSelectedItem().toString(),valBold + valItalic,size);
txtResult.setFont(font);
}
// rbHitam, rbMerah, rbBiru;
if (item.getSource() == rbHitam) txtResult.setForeground(Color.BLACK);
else if (item.getSource() == rbMerah) txtResult.setForeground(Color.RED);
else if (item.getSource() == rbBiru) txtResult.setForeground(Color.BLUE);
}
// Event Handler untuk Tombol Batal
public void keluarSystem() {
System.exit(0);
}
// Event Handler untuk Tombol Clear
public void bersihkanText() {
txtNama.setText("");
txtAlamat.setText("");
cboAgama.setSelectedItem("Islam");
txtResult.setText("");
txtResult.setText("");
rbLaki.setSelected(true);
cbMakan.setSelected(false);
cbBaca.setSelected(false);
cbJalan.setSelected(false);
}
// Event Handler untuk Tombol Simpan
public void simpanText() {
txtResult.append("Nama : " + txtNama.getText()+"\n" );
txtResult.append("Alamat : " + txtAlamat.getText()+"\n" );
// Jenis kelamin Lakilaki / Perempuan
if(rbLaki.isSelected()==true ){
txtResult.append("Jenis Kelamin : " + rbLaki.getText()+"\n" );
}else{
txtResult.append("Jenis Kelamin : " + rbPerempuan.getText()+"\n" );
}
// Makan, Baca, Jalan
if(cbMakan.isSelected() && cbBaca.isSelected() && cbJalan.isSelected()){
txtResult.append("Hobi : " + cbMakan.getText()+ ", " + cbBaca.getText()+ ", " + cbJalan.getText()+"\n" );
}
// Makan, Baca
else if(cbMakan.isSelected() && cbBaca.isSelected()){
txtResult.append("Hobi : " + cbMakan.getText()+ ", " + cbBaca.getText()+"\n" );
}
// Makan, Jalan
else if(cbMakan.isSelected() && cbJalan.isSelected()){
txtResult.append("Hobi : " + cbMakan.getText()+ ", " + cbJalan.getText()+"\n" );
}
// Jalan, Baca
else if(cbJalan.isSelected() && cbBaca.isSelected()){
txtResult.append("Hobi : " + cbJalan.getText()+ ", " + cbBaca.getText()+"\n" );
}
// Makan
else if(cbMakan.isSelected()){
txtResult.append("Hobi : " + cbMakan.getText()+"\n" );
}
// Baca
else if(cbBaca.isSelected()){
txtResult.append("Hobi : " + cbBaca.getText()+"\n" );
}
// Jalan
else if(cbJalan.isSelected()){
txtResult.append("Hobi : " + cbJalan.getText()+"\n" );
}
txtResult.append("-------------------------------------" +"\n" );
}
// Event Handler untuk Combo Box Jenis dan Ukuran Fonts
private void jenisFonts(){
if (cbUnderline.isSelected()){
int size = new Integer(cboSize.getSelectedItem()+"");
font = new Font(cboFonts.getSelectedItem().toString(),valBold + valItalic,size);
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
txtResult.setFont(font.deriveFont(attributes));
}else{
int size = new Integer(cboSize.getSelectedItem()+"");
font = new Font(cboFonts.getSelectedItem().toString(),valBold + valItalic,size);
txtResult.setFont(font);
}
}
//Mouse Listener event Handler
//Mouse Clicked
public void mouseClicked(MouseEvent me){
lblMouse.setText("Informasi Posisi Mouse : "+"Clicked at ["+ me.getX() + "," + me.getY() + "]");
}
//Mouse Released
public void mouseReleased(MouseEvent me){
lblMouse.setText("Informasi Posisi Mouse : "+"Pressed at [" + me.getX() + "," + me.getY() + "]");
}
//Mouse Pressed
public void mousePressed(MouseEvent me){
lblMouse.setText("Informasi Posisi Mouse : "+"Pressed at [" + me.getX() + "," + me.getY() + "]");
}
//Mouse Entered
public void mouseEntered(MouseEvent me){
lblMouse.setText("Informasi Posisi Mouse : "+"Entered at [" + me.getX() + "," + me.getY() + "]");
}
//Mouse Exited
public void mouseExited(MouseEvent me){
lblMouse.setText("Informasi Posisi Mouse : "+"Exit Window at [" + me.getX() + "," + me.getY() + "]");
}
// Mouse Motion Listener Event Handler
//Mouse Dragged
public void mouseDragged(MouseEvent me){
lblMouse.setText("Informasi Posisi Mouse : "+"Dragged at [" + me.getX() + "," + me.getY() + "]");
//konten.setBackground(Color.YELLOW);
}
//Mouse Moved
public void mouseMoved(MouseEvent me){
lblMouse.setText("Informasi Posisi Mouse : "+"Moved at [" + me.getX() + "," + me.getY() + "]");
}
// Program Utama
public static void main(String[] args){
FormBiodata formBiodata = new FormBiodata();
formBiodata.setVisible(true);
}
}
Label:
java
Langganan:
Posting Komentar (Atom)
CAHIEL STORY
Design Template By cahiel gustyartono
Tidak ada komentar
Posting Komentar