Progam Sederhana Java Swing Konversi Bilangan - Ini adalah contoh program java swing sederhana yaitu kalkulator serbaguna, kalkulator ini bisa anda gunakan untuk konversi bilangan dari desimal ke biner, floating point, ones's complement, two complement dan juga bisa untuk menghintung aritmatika seperti penjumlahan, perkalian, pembagian dan pengurangan. untuk lebih jelasnya langsung saja ke scriptnya :
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel; import java.math.BigDecimal; import java.math.BigInteger; import javax.swing.JFrame; import javax.swing.UIManager; public class FloatingPoint extends javax.swing.JFrame { private int numExponent = 8; private int numMantissa = 23; private int bitLength; private int bias; private final String[] HEX = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E", "F"}; private String zero; int eksponen = 8; int mantissa = 23; int sumber, one, two; String hasil; String OC = "1"; String chi; void DesimaltoBiner(){ sumber = Integer.parseInt(tx_decimal3.getText()); hasil = Integer.toBinaryString(sumber); tx_biner1.setText(hasil); String hoc = OneComplement1(hasil); tx_oc1.setText(hoc); } void DesimaltoBiner2(){ sumber = Integer.parseInt(tx_decimal4.getText()); hasil = Integer.toBinaryString(sumber); tx_biner2.setText(hasil); String hoc = OneComplement1(hasil); tx_oc.setText(hoc); } public static String intBits(int i) { StringBuilder sb=new StringBuilder(); for (int j=0; j<32; ++j) { sb.append('0'+(i & 0x80000000)>>>31); i<<=1; } return sb.toString(); } private String OneComplement1(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { String ch = OneComplement2(str.charAt(i)); result += ch; } return result; } private String OneComplement2(char ch) { char ui = '0'; if (ch == ui) { chi = "1"; } else { chi = "0"; } return chi; } public FloatingPoint(int numExponent, int numMantissa) { this.numExponent = 8; this.numMantissa = 23; // Panjang bit total setBitLength(); // Hitung Bias calcBias(); // Buat representasi angka 0 dalam floating point createZero(); } public FloatingPoint() { this(8, 23); LookNFeel(); initComponents(); } private void calcBias() { bias = (int) Math.pow(2, numExponent-1) - 1; } public int getBias() { return bias; } public void setNumExponent(int n) { numExponent = n; calcBias(); setBitLength(); createZero(); } public int getNumExponent() { return numExponent; } /** Mengeset panjang bit mantissa yang baru * @param n nilai panjang bit mantissa yang baru */ public void setNumMantissa(int n) { numMantissa = n; setBitLength(); createZero(); } /** Mengembalikan nilai panjang mantissa sekarang * @return nilai panjang bit mantissa saat ini */ public int getNumMantissa() { return numMantissa; } private void createZero() { zero = ""; for(int i=0; i<bitLength; i++) { zero += "0"; } } private String createZero(int len) { String zero = ""; for(int i=0; i<len; i++) { zero += "0"; } return zero; } private boolean isZero(String bin) { for(int i=0; i<bin.length(); i++) { if(bin.charAt(i) == '1') { return false; } } return true; } private void setBitLength() { bitLength = 1 + numExponent + numMantissa; } public String dec2FloatingPoint(double d) { return dec2FloatingPoint(""+d); } public String dec2FloatingPoint(String dec) { // Variabel yang akan di-return nanti String fp = ""; dec = dec.toLowerCase(); if(!dec.contains(".")) { dec += ".0"; } if(dec.equals("0.0")) { return zero; } if(dec.contains("e")) { String[] numbers = dec.split("e"); int pangkat = Integer.parseInt(numbers[1]); // Kalau pangkatnya negatif if(pangkat < 0) { String[] sig = numbers[0].split("\\."); String padd = createZero(Math.abs(pangkat) - 1); // Kalau angka di depan komanya positif if(sig[0].charAt(0) != '-') { dec = "0."+padd+sig[0]+sig[1]; } else { dec = "-0."+padd + sig[0].substring(1)+sig[1]; } } } //System.out.println(dec); String[] splitted = dec.split("\\."); double d = Double.parseDouble(dec); long integral = Long.parseLong(splitted[0]); String fractional = splitted[1]; //System.out.println("Integral part : " + integral); //System.out.println("Fractional part : " + fractional); // Bagian pertama, periksa tanda untuk memberi nilai signed if(d < 0) { fp += "1"; } else { fp += "0"; } // Absolutkan nilai untuk mempermudah proses konversi integral = Math.abs(integral); d = Math.abs(d); // Bagian kedua, konversi nilai di depan koma seperti konversi bilangan biasa ke binary String binFront = dec2Binary(integral); //System.out.println("binFront : "+binFront); // Bagian ketiga, konversi nilai di belakang koma menggunakan method dec2Mantissa String binBack = dec2Mantissa(fractional); //System.out.println("binBack : "+binBack); // Kalau representasi floating points-nya ternyata 0 if(isZero(binFront) && isZero(binBack)) { return zero; } // Bagian keempat, hitung nilai eksponen-nya nanti setelah di normalisasi // Ada dua kasus, nilainya di atas satu atau di bawah satu. // Kalau di atas 1 nilai eksponenya positif. Kalau dibawah 1 nilainya negatif int exp; int counter = 0; if(integral == 0) { for(int i=0; i<binBack.length(); i++) { if (binBack.charAt(i) == '0') { counter++; } else { break; } } counter++; exp = -1*counter; } else { exp = binFront.length()-1; } //System.out.println("Exponent setelah normalisasi : "+exp); //System.out.println("Counter : "+counter); // Bagian kelima, normalisasi sesuai banyak nilai eksponen yang sudah dihitung diatas if(integral == 0) { binBack = binBack.substring(counter); } else if(binFront.length() != 1) { binBack = binFront.substring(1) + binBack; } // Masih bagian dari yang kelima, cukupkan jumlah bit-nya. Tambah bit 0 dibelakang binBack jika kurang // Atau potong sebanyak jumlah bit mantissa yang di-support int len = binBack.length(); if(len < numMantissa) { for(int i=0; i<numMantissa-len; i++) { binBack += "0"; } } else if(len > numMantissa) { binBack = binBack.substring(0, numMantissa); } //System.out.println("binBack setelah dinormalisasi :"+binBack); // Tahap keenam, jumlahkan nilai eksponen dengan bias dankonversi ke binary sebanyak jumlah bit eksponen exp = exp + bias; String exponent = dec2Binary(exp, numExponent); //System.out.println("Exponent : "+exponent); fp += exponent + binBack; return fp; } public String fp2Decimal(String fp) { // Variabel yang akan di-return nantinya String decimal = ""; // Tahap pertama, tentukan negatif atau tidak Dilihat dari signed bit-nya alias bit MSB-nya // 1 negatif, 0 positif if(fp.charAt(0) == '1') { decimal += "-"; } // Tahap kedua, tentukan nilai eksponen-nya, setelah dikurangi bias String exponent = fp.substring(1, 1+numExponent); int exp = bin2Decimal(exponent); exp -= bias; //System.out.println("Pangkatnya " + exp); // Tahap ketiga, bagi binari menjadi dua, bilangan didepan komadan yang dibelakang koma // Sesuai nilai eksponen yang sudah didapat sebelumnya. String bits = "1" + fp.substring(numExponent+1); String integral = ""; String fractional = ""; if(exp < 0) { String pad = ""; for(int i=0; i<Math.abs(exp)-1; i++) { pad += "0"; } integral = "0"; fractional = pad + bits; } else { if (bits.length() >= exp+1) { integral += bits.substring(0, exp+1); fractional = bits.substring(exp+1); } else { int diff = bits.length() - exp + 1; String pad = ""; for(int i=0; i<diff; i++) { pad += "0"; } integral += bits + pad; fractional = "0"; } } //System.out.println("Integralnya "+integral); //System.out.println("Fractionalnya "+fractional); // Tahap keempat, hitung nilai asli integralnya dan fractionalnya String decFront = ""+bin2Decimal(integral); String decBack = mantissa2Decimal(fractional); decimal += decFront +"."+ decBack; return decimal; } public String dec2Binary(long dec) { if(dec <= 0) { return "0"; } String bin = ""; while(dec != 0) { bin += dec % 2; dec = dec / 2; } //System.out.println("Sebelum dibalik : "+bin); // Dibalik dulu sebelum dikembalikan String temp = ""; for(int i=bin.length()-1; i >= 0; i--) { temp += bin.charAt(i); } return temp; } public String dec2Binary(long dec, int bit) { String bin = dec2Binary(dec); // Kalau kurang di padding 0 didepannya sebanyak kurangnya, kalau lebih dipotong int len = bin.length(); if(len < bit) { String padder = ""; for(int i = 0; i < bit-len; i++) { padder += "0"; } bin = padder + bin; } else if(len > bit) { bin = bin.substring(0, bit); } return bin; } public int bin2Decimal(String bin) { int dec = 0; int len = bin.length()-1; for(int i = len; i >= 0; i--) { if(bin.charAt(i) == '1') { dec += Math.pow(2, len-i); } } return dec; } public String bin2Hex(String bin) { String hex = ""; int len = bin.length(); for(int i = 0; i < len; i += 4) { String number = bin.substring(i, i+4); int index = bin2Decimal(number); hex += HEX[index]; } return hex; } public String hex2Binary(String hex) { String bin = ""; for(int i=0; i<hex.length(); i++) { // Cari index yang menunjuk karakter hexadesimal tersebut di array HEX int index = 0; for(int j=0; j<HEX.length; j++) { String chara = ""+hex.charAt(i); if(chara.equals(HEX[j])) { index = j; break; } } bin += dec2Binary(index, 4); } return bin; } public String dec2Mantissa(Long frac) { return dec2Mantissa(""+frac); } public String dec2Mantissa(String frac) { String mantissa = ""; BigInteger fractal = new BigInteger(frac); BigInteger threshold = new BigInteger("10").pow(frac.length());//(long) Math.pow(10, frac.length()); int counter = 0; //System.out.println("fractal : "+fractal); //System.out.println("threshold : "+threshold); // Batas loop-nya adalah sampai fractal 0 atau sudah dilakukan sebanyak numMantissa+bias // Karena batas angka digeser untuk floating point adalah sebanyak nilai bias while((!fractal.equals(BigInteger.ZERO)) && counter <= numMantissa+bias) { fractal = fractal.multiply(new BigInteger("2")); //System.out.println("Fractal saat ini : "+fractal); if(fractal.compareTo(threshold) >= 0) { mantissa += "1"; fractal = fractal.subtract(threshold); } else { mantissa += "0"; } counter++; } return mantissa; } public String mantissa2Decimal(String mantissa) { double val = 0; BigDecimal value; for(int i=0; i<mantissa.length(); i++) { if(mantissa.charAt(i) == '1') { val += 1.0 / Math.pow(2, i+1); //System.out.println(val); } } value = new BigDecimal(val); if(val == 0) { return "0"; } return value.toString().substring(2); } public void DesimaltoFloating(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); double input = Double.parseDouble(tx_decimal3.getText()); String hasil = util.dec2FloatingPoint(input); tx_floating1.setText(hasil); } public void DesimaltoFloating2(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); double input = Double.parseDouble(tx_decimal4.getText()); String hasil = util.dec2FloatingPoint(input); tx_floating2.setText(hasil); } public void twocomplement(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); int input= Integer.parseInt(tx_decimal3.getText()); String hasil = util.dec2FloatingPoint(input); tx_tc.setText(intBits(input)); } public void twocomplement2(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); int input= Integer.parseInt(tx_decimal4.getText()); String hasil = util.dec2FloatingPoint(input); tx_tc1.setText(intBits(input)); } public void kali(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); int input= Integer.parseInt(tx_decimal3.getText()); int input2= Integer.parseInt(tx_decimal4.getText()); int a = input*input2; aritmatika.setText(""+a); hasil = Integer.toBinaryString(a); hasilbiner.setText(hasil); } public void bagi(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); int input= Integer.parseInt(tx_decimal3.getText()); int input2= Integer.parseInt(tx_decimal4.getText()); int a = input/input2; aritmatika.setText(""+a); hasil = Integer.toBinaryString(a); hasilbiner.setText(hasil); } public void tambah(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); int input= Integer.parseInt(tx_decimal3.getText()); int input2= Integer.parseInt(tx_decimal4.getText()); int a = input+input2; aritmatika.setText(""+a); hasil = Integer.toBinaryString(a); hasilbiner.setText(hasil); } public void kurang(){ FloatingPoint util = new FloatingPoint(eksponen, mantissa); int input= Integer.parseInt(tx_decimal3.getText()); int input2= Integer.parseInt(tx_decimal4.getText()); int a = input-input2; aritmatika.setText(""+a); hasil = Integer.toBinaryString(a); hasilbiner.setText(hasil); } /** 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. */ // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel10 = new javax.swing.JPanel(); jPanel6 = new javax.swing.JPanel(); jPanel4 = new javax.swing.JPanel(); jLabel9 = new javax.swing.JLabel(); tx_decimal3 = new javax.swing.JTextField(); jButton5 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); tx_decimal4 = new javax.swing.JTextField(); tambah = new javax.swing.JButton(); kurang = new javax.swing.JButton(); kali = new javax.swing.JButton(); bagi = new javax.swing.JButton(); aritmatika = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); hasilbiner = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jLabel10 = new javax.swing.JLabel(); tx_biner1 = new javax.swing.JTextField(); tx_biner2 = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); jLabel6 = new javax.swing.JLabel(); tx_floating1 = new javax.swing.JTextField(); tx_floating2 = new javax.swing.JTextField(); jLabel13 = new javax.swing.JLabel(); jLabel12 = new javax.swing.JLabel(); jPanel5 = new javax.swing.JPanel(); jLabel11 = new javax.swing.JLabel(); tx_oc1 = new javax.swing.JTextField(); tx_oc = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jPanel7 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); tx_tc = new javax.swing.JTextField(); tx_tc1 = new javax.swing.JTextField(); jLabel15 = new javax.swing.JLabel(); jLabel14 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel10.setBackground(new java.awt.Color(0, 204, 204)); jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Kalkulator Serba Guna", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Tw Cen MT Condensed Extra Bold", 1, 14))); jPanel6.setBackground(new java.awt.Color(0, 102, 102)); jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Jin Toples (www.jintoples.blogspot.com)", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.BELOW_BOTTOM, new java.awt.Font("Tahoma", 1,14), new java.awt.Color(5, 5, 5))); // NOI18N jPanel4.setBackground(new java.awt.Color(0, 204, 204)); jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel9.setText("Bilangan 1"); jButton5.setText("Konversi"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jLabel1.setText("Bilangan 2"); tambah.setText("+"); tambah.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tambahActionPerformed(evt); } }); kurang.setText("-"); kurang.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { kurangActionPerformed(evt); } }); kali.setText("x"); kali.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { kaliActionPerformed(evt); } }); bagi.setText(":"); bagi.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bagiActionPerformed(evt); } }); jLabel3.setText("HASIL"); jPanel1.setBackground(new java.awt.Color(0, 102, 102)); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder ("HASIL")); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout (jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(hasilbiner, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(hasilbiner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout (jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addGroup(jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addGroup (jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel9) .addComponent(jLabel1)) .addGap(48, 48, 48) .addGroup (jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tx_decimal4, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE) .addComponent(tx_decimal3, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE) .addGroup (jPanel4Layout.createSequentialGroup() .addGroup (jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel3) .addGroup (jPanel4Layout.createSequentialGroup() .addComponent(tambah) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(kurang))) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(kali) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(bagi)))) .addComponent(jButton5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup() .addComponent(aritmatika, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()))) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9) .addComponent(tx_decimal3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(tx_decimal4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tambah) .addComponent(kurang) .addComponent(kali) .addComponent(bagi)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel4Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(aritmatika, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(14, Short.MAX_VALUE)) ); jButton1.setText("keluar"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel2.setBackground(new java.awt.Color(0, 204, 204)); jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel10.setForeground(new java.awt.Color(0, 51, 153)); jLabel10.setText("Bilangan Biner"); tx_biner1.setEnabled(false); tx_biner2.setEditable(false); tx_biner2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tx_biner2ActionPerformed(evt); } }); jLabel4.setText("1"); jLabel5.setText("2"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout (jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4) .addComponent(jLabel5)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(tx_biner1, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE) .addComponent(tx_biner2))) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(22, 22, 22) .addComponent(jLabel10))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel10) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tx_biner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tx_biner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel3.setBackground(new java.awt.Color(0, 204, 204)); jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel6.setForeground(new java.awt.Color(0, 0, 204)); jLabel6.setText("Floating Point"); tx_floating1.setEditable(false); tx_floating2.setEditable(false); jLabel13.setText("1"); jLabel12.setText("2"); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout (jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel13) .addComponent(jLabel12)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tx_floating2, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE) .addComponent(tx_floating1, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE) .addGroup(jPanel3Layout.createSequentialGroup() .addGap(53, 53, 53) .addComponent(jLabel6))) .addContainerGap()) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel6) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tx_floating1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel13)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tx_floating2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel12)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel5.setBackground(new java.awt.Color(0, 204, 204)); jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel11.setForeground(new java.awt.Color(0, 0, 204)); jLabel11.setText("One's Complement"); tx_oc1.setEnabled(false); tx_oc.setEditable(false); jLabel7.setText("2"); jLabel8.setText("1"); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout (jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup( jPanel5Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addGroup(jPanel5Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel5Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel8) .addComponent(jLabel7)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel5Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tx_oc1, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE) .addComponent(tx_oc, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))) .addGroup(jPanel5Layout.createSequentialGroup() .addGap(43, 43, 43) .addComponent(jLabel11))) .addContainerGap()) ); jPanel5Layout.setVerticalGroup( jPanel5Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel11) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel5Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel8) .addGroup(jPanel5Layout.createSequentialGroup() .addComponent(tx_oc1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel5Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tx_oc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)))) .addContainerGap(13, Short.MAX_VALUE)) ); jPanel7.setBackground(new java.awt.Color(0, 204, 204)); jPanel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel2.setForeground(new java.awt.Color(0, 0, 204)); jLabel2.setText("Two Comploment"); tx_tc.setEditable(false); tx_tc1.setEditable(false); jLabel15.setText("1"); jLabel14.setText("2"); javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout (jPanel7); jPanel7.setLayout(jPanel7Layout); jPanel7Layout.setHorizontalGroup( jPanel7Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel7Layout.createSequentialGroup() .addGroup(jPanel7Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel7Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel7Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel15) .addComponent(jLabel14)) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel7Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tx_tc1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE) .addComponent(tx_tc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE))) .addGroup(jPanel7Layout.createSequentialGroup() .addGap(81, 81, 81) .addComponent(jLabel2))) .addContainerGap()) ); jPanel7Layout.setVerticalGroup( jPanel7Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel7Layout.createSequentialGroup() .addGap(6, 6, 6) .addComponent(jLabel2) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel7Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel15) .addGroup(jPanel7Layout.createSequentialGroup() .addGap(26, 26, 26) .addComponent(jLabel14)) .addGroup(jPanel7Layout.createSequentialGroup() .addComponent(tx_tc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(tx_tc1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(13, Short.MAX_VALUE)) ); javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout (jPanel6); jPanel6.setLayout(jPanel6Layout); jPanel6Layout.setHorizontalGroup( jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup() .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(86, 86, 86)) .addGroup(jPanel6Layout.createSequentialGroup() .addGap(18, 18, 18) .addGroup(jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()))) ); jPanel6Layout.setVerticalGroup( jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup() .addGroup(jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel3, 0, 90, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(29, 29, 29) .addGroup(jPanel6Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(25, 25, 25) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout (jPanel10); jPanel10.setLayout(jPanel10Layout); jPanel10Layout.setHorizontalGroup( jPanel10Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel10Layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel10Layout.setVerticalGroup( jPanel10Layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel10Layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout (getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup( layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); pack(); }// </editor-fold> private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { DesimaltoBiner(); DesimaltoFloating(); twocomplement(); DesimaltoBiner2(); DesimaltoFloating2(); twocomplement2(); } private void tx_biner2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void tambahActionPerformed(java.awt.event.ActionEvent evt) { tambah(); } private void kaliActionPerformed(java.awt.event.ActionEvent evt) { kali(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void kurangActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: kurang(); } private void bagiActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: bagi(); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FloatingPoint().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextField aritmatika; private javax.swing.JButton bagi; private javax.swing.JTextField hasilbiner; private javax.swing.JButton jButton1; private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel15; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel10; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JPanel jPanel7; private javax.swing.JButton kali; private javax.swing.JButton kurang; private javax.swing.JButton tambah; private javax.swing.JTextField tx_biner1; private javax.swing.JTextField tx_biner2; private javax.swing.JTextField tx_decimal3; private javax.swing.JTextField tx_decimal4; private javax.swing.JTextField tx_floating1; private javax.swing.JTextField tx_floating2; private javax.swing.JTextField tx_oc; private javax.swing.JTextField tx_oc1; private javax.swing.JTextField tx_tc; private javax.swing.JTextField tx_tc1; // End of variables declaration public void LookNFeel() { try { JFrame.setDefaultLookAndFeelDecorated(true); UIManager.setLookAndFeel(new WindowsLookAndFeel()); } catch (Exception e) { } } }
Untuk menggunakannya anda dapat menekan tombol konversi untuk konversi dan tombol aritmatika untuk proses aritmatika.
Baca juga : Program Java Sederhana Color Picker
Progam Sederhana Java Swing Konversi Bilangan anda sekarang sudah selsai, baca juga tutorial java swing lainnya.
Baca juga : Program Java Sederhana Color Picker
Progam Sederhana Java Swing Konversi Bilangan anda sekarang sudah selsai, baca juga tutorial java swing lainnya.
0 komentar