Contoh Program Konversi Bilangan Menggunakan GUI JFrame Java

program java konversi bilangan menggunakan jframe
program java konversi bilangan menggunakan jframe

JFrame merupakan salah satu dari komponen GUI (Graphical User Interface), yang bertujuan agar pengguna dengan mudah menjalankan atau mengoprasikan sebuah aplikasi komputer. Sebagai langkah awal membuat program berbasis GUI kita mencoba membuat Frame. Frame merupakan komponen dasar dalam membuat sebuah aplikasi yang berbasis GUI. Frame berfungsi sebagai kontainer atau sebagai wadah penampung komponen GUI lainnya, seperti : Button, Table, Label, ComboBox, dll. Untuk membuat frame, kita memerlukan sebuah java Class yang bernama JFrame yang berada dalam package javax.swing. kali ini saya akan membahas komponen-komponen yang masuk dalam pembuatan java Class JFrame.

Program konversi bilangan desimal ke biner, hexa, dan oktal menggunakan java

langkah pertama buat dulu desainnya kurang lebih seperti berikut:

Buat file JFrame AplikasiKonversiBilangan.java dan masukan kode berikut:

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class AplikasiKonversiBilangan extends javax.swing.JFrame {

    public AplikasiKonversiBilangan() {
        initComponents();
        
        this.setIconImage(new ImageIcon(getClass().getResource("icon AKB.png")).getImage());
        }    
    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        Konversi3 = new javax.swing.JButton();
        txtOktal = new javax.swing.JTextField();
        Oktal = new javax.swing.JLabel();
        btnTipeKonversi3 = new javax.swing.JButton();
        txtDesimal3 = new javax.swing.JTextField();
        Desimal3 = new javax.swing.JLabel();
        btnKonversi2 = new javax.swing.JButton();
        txtHexa = new javax.swing.JTextField();
        Hexa = new javax.swing.JLabel();
        btnTipeKonversi2 = new javax.swing.JButton();
        txtDesimal2 = new javax.swing.JTextField();
        Desimal2 = new javax.swing.JLabel();
        btnKonversi = new javax.swing.JButton();
        txtBiner = new javax.swing.JTextField();
        Biner = new javax.swing.JLabel();
        btnTipeKonversi = new javax.swing.JButton();
        txtDesimal = new javax.swing.JTextField();
        Desimal = new javax.swing.JLabel();
        Bg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Aplikasi Konversi Bilangan - V.1.0 - Beta");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Konversi3.setText("Konversi");
        Konversi3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Konversi3ActionPerformed(evt);
            }
        });
        getContentPane().add(Konversi3, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 130, 80, 30));
        getContentPane().add(txtOktal, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 130, 80, 30));

        Oktal.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Oktal.setForeground(new java.awt.Color(255, 255, 255));
        Oktal.setText("Oktal");
        getContentPane().add(Oktal, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 140, -1, 10));

        btnTipeKonversi3.setText("=>");
        btnTipeKonversi3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTipeKonversi3ActionPerformed(evt);
            }
        });
        getContentPane().add(btnTipeKonversi3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 130, 50, 30));
        getContentPane().add(txtDesimal3, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 130, 90, 30));

        Desimal3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Desimal3.setForeground(new java.awt.Color(255, 255, 255));
        Desimal3.setText("Desimal");
        getContentPane().add(Desimal3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, -1, 10));

        btnKonversi2.setText("Konversi");
        btnKonversi2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKonversi2ActionPerformed(evt);
            }
        });
        getContentPane().add(btnKonversi2, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 90, 80, 30));
        getContentPane().add(txtHexa, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 90, 80, 30));

        Hexa.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Hexa.setForeground(new java.awt.Color(255, 255, 255));
        Hexa.setText("Hexa");
        getContentPane().add(Hexa, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 100, -1, 10));

        btnTipeKonversi2.setText("=>");
        btnTipeKonversi2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTipeKonversi2ActionPerformed(evt);
            }
        });
        getContentPane().add(btnTipeKonversi2, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 90, 50, 30));

        txtDesimal2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDesimal2ActionPerformed(evt);
            }
        });
        getContentPane().add(txtDesimal2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 90, 90, 30));

        Desimal2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Desimal2.setForeground(new java.awt.Color(255, 255, 255));
        Desimal2.setText("Desimal");
        getContentPane().add(Desimal2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, -1, 10));

        btnKonversi.setText("Konversi");
        btnKonversi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKonversiActionPerformed(evt);
            }
        });
        getContentPane().add(btnKonversi, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 50, 80, 30));

        txtBiner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBinerActionPerformed(evt);
            }
        });
        getContentPane().add(txtBiner, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 50, 80, 30));

        Biner.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Biner.setForeground(new java.awt.Color(255, 255, 255));
        Biner.setText("Biner");
        getContentPane().add(Biner, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 60, -1, 10));

        btnTipeKonversi.setText("=>");
        btnTipeKonversi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTipeKonversiActionPerformed(evt);
            }
        });
        getContentPane().add(btnTipeKonversi, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 50, 30));

        txtDesimal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDesimalActionPerformed(evt);
            }
        });
        getContentPane().add(txtDesimal, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 50, 90, 30));

        Desimal.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Desimal.setForeground(new java.awt.Color(255, 255, 255));
        Desimal.setText("Desimal");
        getContentPane().add(Desimal, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, 10));

        Bg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bg AKB.png"))); // NOI18N
        getContentPane().add(Bg, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        pack();
    }//                         

    private void txtDesimalActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
    }                                          

    private void txtBinerActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
    }                                        

    private void btnTipeKonversiActionPerformed(java.awt.event.ActionEvent evt) {                                                
                     
         if(btnTipeKonversi.getText().equals("=>")){
            btnTipeKonversi.setText("<=");
        } else {
            btnTipeKonversi.setText("=>");
        }
       
        if(btnTipeKonversi.getText().equals("=>")){
            txtBiner.setEnabled(false);
            txtBiner.setText("");
            txtDesimal.setEnabled(true);
        } else {
            txtBiner.setEnabled(true);
            txtDesimal.setEnabled(false);
            txtDesimal.setText("");
        }
        
    }                                               

    private void txtDesimal2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void btnKonversiActionPerformed(java.awt.event.ActionEvent evt) {                                            
       
         try {   
           
            if(txtBiner.getText().isEmpty()){
                int desi = Integer.valueOf(txtDesimal.getText());
                // konversi ke biner
                String bin = Integer.toBinaryString(desi);
                txtBiner.setText(bin);
            } else {
                String biner = txtBiner.getText();
                // konversi ke desimal
                int des = Integer.parseInt(biner, 2);
                txtDesimal.setText(String.valueOf(des));
            }

           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Masukan angka yang benar ! ");
        } finally {
            txtBiner.setEnabled(true);
            txtDesimal.setEnabled(true);
        }
                      
        
    }                                           

    private void btnTipeKonversi2ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        
         if(btnTipeKonversi2.getText().equals("=>")){
            btnTipeKonversi2.setText("<=");
        } else {
            btnTipeKonversi2.setText("=>");
        }
       
        if(btnTipeKonversi2.getText().equals("=>")){
            txtHexa.setEnabled(false);
            txtHexa.setText("");
            txtDesimal2.setEnabled(true);
        } else {
            txtHexa.setEnabled(true);
            txtDesimal2.setEnabled(false);
            txtDesimal2.setText("");
        }
        
    }                                                

    private void btnKonversi2ActionPerformed(java.awt.event.ActionEvent evt) {                                             
    
         try {   
           
            if(txtHexa.getText().isEmpty()){
                int desi2 = Integer.valueOf(txtDesimal2.getText());
                // konversi ke hexa
                String bin = Integer.toHexString(desi2);
                txtHexa.setText(bin);
            } else {
                String hexa = txtHexa.getText();
                // konversi ke desimal
                int des = Integer.parseInt(hexa, 2);
                txtDesimal2.setText(String.valueOf(des));
            }

           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Masukan angka yang benar ! ");
        } finally {
            txtHexa.setEnabled(true);
            txtDesimal.setEnabled(true);
        }
        
    }                                            

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

         if(btnTipeKonversi3.getText().equals("=>")){
            btnTipeKonversi3.setText("<=");
        } else {
            btnTipeKonversi3.setText("=>");
        }
       
        if(btnTipeKonversi3.getText().equals("=>")){
            txtOktal.setEnabled(false);
            txtOktal.setText("");
            txtDesimal3.setEnabled(true);
        } else {
            txtOktal.setEnabled(true);
            txtDesimal3.setEnabled(false);
            txtDesimal3.setText("");
        }
        
    }                                                

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

         try {   
           
            if(txtOktal.getText().isEmpty()){
                int desi3 = Integer.valueOf(txtDesimal3.getText());
                // konversi ke oktal
                String okt = Integer.toOctalString(desi3);
                txtOktal.setText(okt);
            } else {
                String oktal = txtOktal.getText();
                // konversi ke desimal
                int des3 = Integer.parseInt(oktal, 2);
                txtDesimal3.setText(String.valueOf(des3));
            }

           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Masukan angka yang benar !");
        } finally {
            txtOktal.setEnabled(true);
            txtDesimal.setEnabled(true);
        }
                
        
    }                                         

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AplikasiKonversiBilangan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AplikasiKonversiBilangan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AplikasiKonversiBilangan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AplikasiKonversiBilangan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AplikasiKonversiBilangan().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel Bg;
    private javax.swing.JLabel Biner;
    private javax.swing.JLabel Desimal;
    private javax.swing.JLabel Desimal2;
    private javax.swing.JLabel Desimal3;
    private javax.swing.JLabel Hexa;
    private javax.swing.JButton Konversi3;
    private javax.swing.JLabel Oktal;
    private javax.swing.JButton btnKonversi;
    private javax.swing.JButton btnKonversi2;
    private javax.swing.JButton btnTipeKonversi;
    private javax.swing.JButton btnTipeKonversi2;
    private javax.swing.JButton btnTipeKonversi3;
    private javax.swing.JTextField txtBiner;
    private javax.swing.JTextField txtDesimal;
    private javax.swing.JTextField txtDesimal2;
    private javax.swing.JTextField txtDesimal3;
    private javax.swing.JTextField txtHexa;
    private javax.swing.JTextField txtOktal;
    // End of variables declaration                   
}

Jika sudah jalankan programnya, dan hasilnya akan seperti berikut:


Download projectnya di link berikut:




keyword:
  • download program konversi bilangan menggunakan gui jframe java
  • download program konversi bilangan desimal ke biner, hexa, dan oktal menggunakan java
  • contoh program konversi bilangan menggunakan gui jframe java
  • program konversi bilangan menggunakan gui jframe java
  • program konversi bilangan desimal ke biner, hexa, dan oktal menggunakan java
  • matakuliah pbo
  • aplikasi konversi bilangan desimal biner hexa oktal jframe java
  • pemrograman berorientasi object 
  • pemrograman berorientasi object java
  • jframe java
  • gui java
  • cara membuat program konversi bilangan menggunakan gui jframe java

0 Response to "Contoh Program Konversi Bilangan Menggunakan GUI JFrame Java"

Posting Komentar

Visitor