Pemrograman Visual Java “Membuat Form Penilaian” import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Nilaigel2 { public static void main (String[]arg) { //membuat Komponen Yang di perlukan JFrame frame = new JFrame (".::.Form Nilai.::. Moh. ALI MT"); JLabel lblNim = new JLabel ("NIM"); JLabel lblNama = new JLabel ("NAMA"); JLabel lblAlamat = new JLabel ("ALAMAT"); final JTextField txtfNim = new JTextField (15); final JTextField txtfNama = new JTextField (25); final JTextField txtfAlamat = new JTextField (25); JLabel lblTugas = new JLabel ("Nilai Tugas"); JLabel lblUTS = new JLabel ("Nilai UTS"); JLabel lblUAS = new JLabel ("Nilai UAS"); String []DftNilai={"10","20","30","40","50","60","70","80","90","100"}; final JComboBox cbbTugas=new JComboBox(DftNilai); final JComboBox cbbUTS=new JComboBox(DftNilai); final JComboBox cbbUAS=new JComboBox(DftNilai); final JTextArea areaCheck = new JTextArea (15,30); JScrollPane scrollPane = new JScrollPane (areaCheck, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); JButton btnTampil = new JButton ("Tampilkan"); btnTampil.setMnemonic('T'); JButton btnBersih = new JButton ("Bersihkan"); btnBersih.setMnemonic('B'); JButton btnExit = new JButton ("Keluar"); btnExit.setMnemonic('K'); // Pengaturan Layout Container isiFrame; isiFrame = frame.getContentPane(); isiFrame.setLayout(null); isiFrame.add(lblNim); isiFrame.add(lblNama); isiFrame.add(lblAlamat); isiFrame.add(txtfNim); isiFrame.add(txtfNama); isiFrame.add(txtfAlamat); isiFrame.add(lblTugas); isiFrame.add(lblUTS);
1
isiFrame.add(lblUAS); isiFrame.add(cbbTugas); isiFrame.add(cbbUTS); isiFrame.add(cbbUAS); isiFrame.add(scrollPane); isiFrame.add(btnTampil); isiFrame.add(btnBersih); isiFrame.add(btnExit); lblNim.setBounds(10,10,75,20); txtfNim.setBounds(70,10,125,20); lblNama.setBounds(10,35,75,20); txtfNama.setBounds(70,35,300,20); lblAlamat.setBounds(10,60,75,20); txtfAlamat.setBounds(70,60,350,20); lblTugas.setBounds(10,95,80,20); cbbTugas.setBounds(80,95,50,20); lblUTS.setBounds(160,95,60,20); cbbUTS.setBounds(220,95,50,20); lblUAS.setBounds(300,95,60,20); cbbUAS.setBounds(370,95,50,20); scrollPane.setBounds(10,130,370,200); btnTampil.setBounds(10,350,100,27); btnBersih.setBounds(150,350,100,27); btnExit.setBounds(300,350, 100, 27); btnTampil.setBackground(Color.blue); btnBersih.setBackground(Color.blue); btnExit.setBackground(Color.blue); btnTampil.setForeground(Color.yellow); btnBersih.setForeground(Color.yellow); btnExit.setForeground(Color.yellow); // Penangan Kejadian dan Algoritmanya //Algoritmanya //event untuk button.. btnTampil.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { String nTugas=String.valueOf(cbbTugas.getSelectedItem()); String nUTS=String.valueOf(cbbUTS.getSelectedItem()); String nUAS=String.valueOf(cbbUAS.getSelectedItem()); float float float float
nilTugas=Float.parseFloat(nTugas); nilUTS=Float.parseFloat(nUTS); nilUAS=Float.parseFloat(nUAS); nilAkhir=(nilTugas+nilUTS+nilUAS)/3;
String grade=" "; String keterangan=" ";
2
String Pesan=" "; if(nilAkhir>85) { grade="A"; keterangan ="Perfect dan Anda Lulus"; Pesan ="Selamat, Pertahankan Prestasimu"; } else if(nilAkhir>70 && nilAkhir<=85) { grade="B"; keterangan="Excellent dan Anda Lulus"; Pesan =" Selamat Ya.., Tingkatkan Terus Prestasimu"; } else if(nilAkhir>60 && nilAkhir<=70) { grade="C"; keterangan="Good dan Anda Lulus"; Pesan =" Belajarlah Lebih Rajin Lagi"; } else if(nilAkhir>50 && nilAkhir<=60) { grade="D"; keterangan="Bad dan Belum Lulus"; Pesan ="Anda Perlu belajar Lebih Keras Lagi"; } else { grade="E"; keterangan ="Worst & Anda Belum Lulus"; Pesan ="Anda Jangan Bermalas Malasan & Terus Belajar"; } String Show; Show="NIM\t: "+txtfNim.getText()+ "\nNama\t: "+txtfNama.getText()+ "\nAlamat\t: "+txtfAlamat.getText()+ "\nNilai Tugas\t: "+nTugas+ "\nNilai UTS\t: "+nUTS+ "\nNilai UAS\t: "+nUAS+ "\nNilai Akhir\t: "+nilAkhir+ "\nGrade\t: "+grade+ "\nKeterangan\t: "+keterangan+ "\nPesan\t: "+Pesan; areaCheck.append(Show); } public { public { public { public { });
void mouseEntered(MouseEvent e) } void mouseExited(MouseEvent e) } void mousePressed(MouseEvent e) } void mouseReleased(MouseEvent e) }
3
// event tuk membersihkan btnBersih.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ txtfNim.setText(""); txtfNama.setText(""); txtfAlamat.setText(""); txtfNim.requestFocus(); areaCheck.setText(""); } }); //evnt tuk keluar program btnExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {System.exit(0); } }); //Event agar dapat DiEnter .... txtfNim.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){ if(e.getKeyChar()==e.VK_ENTER) { txtfNama.requestFocus(); } } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } }); txtfNama.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){ if(e.getKeyChar()==e.VK_ENTER) { txtfAlamat.requestFocus(); } } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } }); //event Enter ke ComboBox txtfAlamat.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){ if(e.getKeyChar()==e.VK_ENTER) { cbbTugas.requestFocus(); } } public void keyReleased(KeyEvent e){ }
4
public void keyTyped(KeyEvent e){ } }); cbbTugas.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){ if(e.getKeyChar()==e.VK_ENTER) { cbbUTS.requestFocus(); } } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } }); cbbUTS.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){ if(e.getKeyChar()==e.VK_ENTER) { cbbUAS.requestFocus(); } } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } }); //
}
frame.setBackground(Color.BLUE); frame.setSize(450,430); // frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Program/sourcecode diatas akan tampil sebagai berikut
5
masih kosong
Gambar 1. Tampilan Ketika Button Tampil belum di klik atau dienter, text Area
Gambar 2. Taxt Area Terisi ketika Button di klik atau dienter.
6