21 May 2010

Pemrograman GUI di Java menggunakan JCreator

Disini akan membuatnya menjadi 2 file, jika masih bingung silahkan baca artikel ini dulu biar lebih jelas.

Disini akan digunakan package Swing.

Output Dari Script yang dibuat seperti berikut :



file1 : GUISwingAwt.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GUISwingAwt extends JFrame{
JPanel pl = new JPanel();
JLabel lbl1 = new JLabel ("Nilai Absen");
JLabel lbl2 = new JLabel ("Nilai Tugas");
JLabel lbl3 = new JLabel ("Nilai UTS");
JLabel lbl4 = new JLabel ("Nilai UAS");
JLabel lbl5 = new JLabel ("Hasil");
JLabel lbl6 = new JLabel ("Grade");
JTextField txt1 = new JTextField();
JTextField txt2 = new JTextField();
JTextField txt3 = new JTextField();
JTextField txt4 = new JTextField();
JTextField txt5 = new JTextField();
JTextField txt6 = new JTextField();
JButton bthitung = new JButton ("Hitung");
JButton btbatal = new JButton ("Batal");
JButton btclose = new JButton ("Close");
public GUISwingAwt() {
pl.setLayout(null);
//posisikan komponen
add (pl);

lbl1.setBounds(15,20,80,25);
lbl2.setBounds(15,50,80,25);
lbl3.setBounds(15,80,80,25);
lbl4.setBounds(15,110,80,25);
lbl5.setBounds(15,140,80,25);
lbl6.setBounds(15,170,80,25);

txt1.setBounds(120,20,100,25);
txt2.setBounds(120,50,100,25);
txt3.setBounds(120,80,100,25);
txt4.setBounds(120,110,100,25);
txt5.setBounds(120,140,100,25);
txt6.setBounds(120,170,100,25);

bthitung.setBounds(15,210,70,25);
btbatal.setBounds(95,210,70,25);
btclose.setBounds(175,210,70,25);
//menampilkan komponen
pl.add(lbl1);
pl.add(lbl2);
pl.add(lbl3);
pl.add(lbl4);
pl.add(lbl5);
pl.add(lbl6);

pl.add(txt1);
pl.add(txt2);
pl.add(txt3);
pl.add(txt4);
pl.add(txt5);
pl.add(txt6);

pl.add(bthitung);
pl.add(btbatal);
pl.add(btclose);

setTitle("GUISwingAwt");
setBounds(100,100,300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
bthitung.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
hitungNilaiAkhir();
}
});

btbatal.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
hapusdata();
}
});

btclose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}

public void hitungNilaiAkhir(){
int nilaiabsen = Integer.parseInt(txt1.getText());
int nilaitugas = Integer.parseInt(txt2.getText());
int nilaiUTS = Integer.parseInt(txt3.getText());
int nilaiUAS = Integer.parseInt(txt4.getText());

double nilaiakhir = ((nilaiabsen*0.1)+(nilaitugas*0.2)+(nilaiUTS*0.3)+(nilaiUAS*0.4));
String nilaiakhirA = Double.toString(nilaiakhir);
txt5.setText(nilaiakhirA);

if(nilaiakhir>=90){
txt6.setText("A");
}
else if(nilaiakhir<=89 && nilaiakhir>69){
txt6.setText("B");
}
else if(nilaiakhir<=69 && nilaiakhir>49){
txt6.setText("C");
}
else if(nilaiakhir<=49 && nilaiakhir>29){
txt6.setText("D");
}
else {txt6.setText("E");}
}

public void hapusdata(){
txt1.setText("");
txt2.setText("");
txt3.setText("");
txt4.setText("");
txt5.setText("");
txt6.setText("");
}
}

file2 : GUISwingAwtBeraksi.java

public class GUISwingAwtBeraksi {
public static void main (String [] args){
GUISwingAwt obj = new GUISwingAwt();
obj.setVisible(true);
}
}
Script lengkapnya bisa didownload DISINI. :)

No comments:

Post a comment