Anda di halaman 1dari 14

package jumlahhari;

import java.io.BufferedWriter; import java.io.FileWriter; import javax.swing.JOptionPane;

/** * * @author user */ public class JumlahHariCB extends javax.swing.JFrame {

int jumlahHari;

/** Creates new form JumlahHariCB */ public JumlahHariCB() { initComponents();

/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor.

*/ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {

jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); thnTF = new javax.swing.JTextField(); pilihCB = new javax.swing.JComboBox(); jPanel2 = new javax.swing.JPanel(); hariLB = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Aplikasi Penentu JUmlah Hari");

jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel1.setText("Tahun");

jLabel2.setText("Bulan");

pilihCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Januari", "Pebruari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "Nopember", "Desember" })); pilihCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pilihCBActionPerformed(evt); } });

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(23, 23, 23) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel2)) .addGap(58, 58, 58) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pilihCB, 0, 132, Short.MAX_VALUE) .addComponent(thnTF, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)) .addContainerGap(185, javax.swing.GroupLayout.PREFERRED_SIZE)) ); jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(thnTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(pilihCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(23, Short.MAX_VALUE)) );

jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(hariLB, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE) .addContainerGap()) );

jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(hariLB, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE) .addContainerGap()) );

jButton1.setText("HITUNG"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });

jButton2.setText("HAPUS"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } });

jButton3.setText("SIMPAN"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt); } });

jButton4.setText("KELUAR"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } });

javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addGap(24, 24, 24) .addComponent(jButton1) .addGap(29, 29, 29) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE) .addComponent(jButton3) .addGap(27, 27, 27) .addComponent(jButton4)

.addGap(36, 36, 36)) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton4) .addComponent(jButton3) .addComponent(jButton2)) .addContainerGap(20, Short.MAX_VALUE)) );

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(8, 8, 8) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(57, Short.MAX_VALUE)) );

pack(); }// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (thnTF.getText().equals("")){ JOptionPane.showMessageDialog(this, "Data TahunBelum Diisi(Kosong)"); thnTF.requestFocus(); }

else {

String bulan=pilihCB.getSelectedItem().toString(); int tahun=Integer.parseInt(thnTF.getText());

int d=pilihCB.getSelectedIndex(); switch(d){ case 0: case 2: case 4: case 6: case 7: case 9: case 11: jumlahHari=31; //System.out.println("Hari Kerja"); break; case 3: case 5: case 8: case 10: //System.out.println("Hari Libur"); jumlahHari=30; break; case 1:

if (((tahun % 4==0)&& (tahun % 100 !=0))||(tahun % 400==0)) jumlahHari=29; else jumlahHari=28;

break; //default: //JOptionPane.showMessageDialog(null,"Sal") }

hariLB.setText("Jumlah Hari pada bulan " +bulan+ " Tahun "+ tahun+" adalah " +jumlahHari+" hari"); } }

private void pilihCBActionPerformed(java.awt.event.ActionEvent evt) {

if (thnTF.getText().equals("")){ JOptionPane.showMessageDialog(this, "Data TahunBelum Diisi(Kosong)"); thnTF.requestFocus(); }

else {

String bulan=pilihCB.getSelectedItem().toString(); int tahun=Integer.parseInt(thnTF.getText());

int d=pilihCB.getSelectedIndex(); switch(d){ case 0: case 2: case 4: case 6: case 7: case 9: case 11: jumlahHari=31; //System.out.println("Hari Kerja"); break; case 3: case 5: case 8: case 10: //System.out.println("Hari Libur"); jumlahHari=30; break; case 1: if (((tahun % 4==0)&& (tahun % 100 !=0))||(tahun % 400==0)) jumlahHari=29; else jumlahHari=28;

break; //default: //JOptionPane.showMessageDialog(null,"Sal") }

hariLB.setText("Jumlah Hari pada bulan " +bulan+ " Tahun "+ tahun+" adalah " +jumlahHari+" hari"); } }

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

int x=JOptionPane.showConfirmDialog(null, "Yakin Ingin Keluar?", "Konfirmasi Keluar Aplikasi", JOptionPane.YES_NO_OPTION); if (x==JOptionPane.YES_OPTION) dispose(); }

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

thnTF.setText(""); hariLB.setText(""); thnTF.requestFocus();

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { try {

//Buat File BufferedWriter out = new BufferedWriter(new FileWriter("jumlahhari.txt")); // Simpan file out.write(hariLB.getText()); // pesan bila berhasil JOptionPane.showMessageDialog(null,"Berhasil disimpan dalam file"); //Tutup output Stream out.close(); } catch(Exception ex) { JOptionPane.showMessageDialog(null,"Error : "+ex.getMessage()); }

/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JumlahHariCB().setVisible(true); } }); }

// Variables declaration - do not modify private javax.swing.JLabel hariLB; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JComboBox pilihCB; private javax.swing.JTextField thnTF; // End of variables declaration

Anda mungkin juga menyukai