[Java Swing Desktop / Basic] Aplikasi Konversi Berat

Sebelumnya saya memberikan contoh program konversi suhu dengan menggunakan Java. Kali ini contoh programnya hampir sama yaitu konversi berat.

Contoh program ini cocok bagi anda yang sering belajar bahasa pemrograman Java. Berikut ini source codenya

image

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Color;

@SuppressWarnings("serial")
public class KonversiBeratApp extends JFrame {

	private JPanel contentPane;
	private JLabel lblKilogram;
	private JLabel lblGram;
	private JLabel lblOns;
	private JTextField txtKg;
	private JTextField txtGram;
	private JTextField txtOns;
	double kg, gram, ons;
	private JLabel lblIcon;

	/**
	 * Create the frame.
	 */
	public KonversiBeratApp() {
		setTitle("Konversi Berat");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(245, 222, 179));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblKilogram = new JLabel("Kilogram");
		lblKilogram.setFont(new Font("Tahoma", Font.BOLD, 10));
		lblKilogram.setBounds(87, 80, 46, 14);
		contentPane.add(lblKilogram);

		lblGram = new JLabel("Gram");
		lblGram.setFont(new Font("Tahoma", Font.BOLD, 10));
		lblGram.setBounds(87, 111, 46, 14);
		contentPane.add(lblGram);

		lblOns = new JLabel("Ons");
		lblOns.setFont(new Font("Tahoma", Font.BOLD, 10));
		lblOns.setBounds(87, 142, 46, 14);
		contentPane.add(lblOns);

		txtKg = new JTextField();
		txtKg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				gram = Double.parseDouble(txtKg.getText()) * 1000;
				ons = Double.parseDouble(txtKg.getText()) / 10;
				txtGram.setText("" + gram);
				txtOns.setText("" + ons);

			}
		});
		txtKg.setBounds(143, 77, 117, 20);
		contentPane.add(txtKg);
		txtKg.setColumns(10);

		txtGram = new JTextField();
		txtGram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				kg = Double.parseDouble(txtGram.getText()) / 1000;
				ons = Double.parseDouble(txtGram.getText()) / 100;
				txtKg.setText("" + kg);
				txtOns.setText("" + ons);
			}
		});
		txtGram.setBounds(143, 108, 117, 20);
		contentPane.add(txtGram);
		txtGram.setColumns(10);

		txtOns = new JTextField();
		txtOns.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				kg = Double.parseDouble(txtOns.getText()) / 10;
				gram = Double.parseDouble(txtOns.getText()) * 100;
				txtKg.setText("" + kg);
				txtGram.setText("" + gram);
			}
		});
		txtOns.setBounds(143, 139, 117, 20);
		contentPane.add(txtOns);
		txtOns.setColumns(10);

		lblIcon = new JLabel("");
		lblIcon.setIcon(new ImageIcon(KonversiBeratApp.class
				.getResource("/KonversiBerat/truck.png")));
		lblIcon.setBounds(72, 47, 333, 201);
		contentPane.add(lblIcon);
		setLocationRelativeTo(null);
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					KonversiBeratApp frame = new KonversiBeratApp();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}

Seperti ini tampilan programnya

image

image

Selamat mencoba dan semoga bermanfaat🙂

Untuk pertanyaan, saran dan kritik silahkan ditulis di kolom komentar.

Posted from WordPress for Android

One thought on “[Java Swing Desktop / Basic] Aplikasi Konversi Berat

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