Membuat Aplikasi Number Format Converter Menggunakan Java


Ini adalah contoh sebuah prrogram untuk menkonversi nomber antara Integer, Biner, Oktal, Hexa. Jika ada yang tidak dimengerti atau ada yang ingin ditanyakan, silahkan tanyakan di bagian komentar :


/**
* NumberConverter.java
*
* copyright 2007 echo_khannedy@yahoo.co.id
*/
package chapter02;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* @author echo
*
*/
@SuppressWarnings(“serial”)
public class NumberConverter extends javax.swing.JFrame {
private JPanel jPanel1;
private JTextField text_result;
private JComboBox combo_result;
private JComboBox combo_input;
private JLabel jLabel2;
private JButton button_proses;
private JTextField text_input;
private JLabel jLabel1;
private String[] formatNumber;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NumberConverter inst = new NumberConverter();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NumberConverter() {
super();
initVariable();
initGUI();
}
private void initVariable() {
formatNumber = new String[] { “Integer”, “Biner”, “Oktal”, “Hexa” };
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle(“Number Converter”);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText(“Input”);
jLabel1.setBounds(12, 12, 58, 16);
}
{
text_input = new JTextField();
jPanel1.add(text_input);
text_input.setBounds(70, 10, 172, 20);
}
{
button_proses = new JButton();
jPanel1.add(button_proses);
button_proses.setText(“CONVERT”);
button_proses.setBounds(70, 42, 89, 26);
button_proses.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
convertTheNumber();
} catch (NumberFormatException t) {
JOptionPane.showMessageDialog(
NumberConverter.this, “ILLEGAL INPUT”);
}
}
});
}
{
jLabel2 = new JLabel();
jPanel1.add(jLabel2);
jLabel2.setText(“Result”);
jLabel2.setBounds(12, 82, 58, 16);
}
{
text_result = new JTextField();
jPanel1.add(text_result);
text_result.setBounds(70, 80, 172, 20);
text_result.setEditable(false);
}
{
combo_input = new JComboBox(formatNumber);
jPanel1.add(combo_input);
combo_input.setBounds(254, 7, 128, 25);
}
{
combo_result = new JComboBox(formatNumber);
jPanel1.add(combo_result);
combo_result.setBounds(254, 76, 128, 25);
}
}
pack();
this.setSize(402, 144);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void convertTheNumber() throws NumberFormatException {
String str_input = text_input.getText();
if (str_input.length() < 1) {
return;
}
int number_input;
String type = combo_input.getSelectedItem().toString();
if (type.equals(formatNumber[0])) {
number_input = Integer.valueOf(str_input);
} else if (type.equals(formatNumber[1])) {
number_input = Integer.valueOf(str_input, 2);
} else if (type.equals(formatNumber[2])) {
number_input = Integer.valueOf(str_input, 8);
} else if (type.equals(formatNumber[3])) {
number_input = Integer.valueOf(str_input, 16);
} else {
number_input = 0;
}
String result;
type = combo_result.getSelectedItem().toString();
if (type.equals(formatNumber[0])) {
result = String.valueOf(number_input);
} else if (type.equals(formatNumber[1])) {
result = Integer.toBinaryString(number_input);
} else if (type.equals(formatNumber[2])) {
result = Integer.toOctalString(number_input);
} else if (type.equals(formatNumber[3])) {
result = Integer.toHexString(number_input);
} else {
result = “0”;
}
text_result.setText(result);
}
}

[SCREEN SHOOT]

Iklan

14 pemikiran pada “Membuat Aplikasi Number Format Converter Menggunakan Java

  1. Mas,saya kan ada tugas kampz dan rencanay mau buat aplikasi convert image gtu pake java, yang mau saya tanya
    ” hampir sama ga ya coding number convert untuk image convert ( rubah2 dikit )?”
    ” boleh ga saya ikutin coding / tampilan dari number convert untuk image convert ?”

    makasih banget ya mas….

    tolong dibalas ke email aJa ni_fy87@yahoo.com

    by nining

  2. Ping balik: Belajar JaVa « WAHYU FIGHTER

  3. Ping balik: Tutorial Java dari Eko Kurniawan Khannedy « nge-WP@net

  4. mas programnya aku buat tugas java ya,.,..,????
    d suruh download program apa j tp harus ngerti,…..
    blh d jlasin g mas ttng coding program itu,.,.,.

  5. numpang tanya
    untuk bikin nomor otomatis gmn ya?
    mksdnya bgni… di aplikasi ada no pesanan, misalnya skrg no pesanannya no23
    bgtu besok aplikasi dibuka untuk input lg, di text field sdh otomatis ada no24
    thks

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s