Perhitungan Dua Bilangan dengan Java NetBeans

423 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
423
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Perhitungan Dua Bilangan dengan Java NetBeans

  1. 1. hitungduabilangan importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; public class hitungduabilangan extends JFrame implements ActionListener { JTextFieldtxtA,txtB,txthasil; JPanel panel= new JPanel(); JLabelA,B,hasil; JButtontambah,bagi,kali,kurang; publichitungduabilangan() { setTitle("HitungDuaBilangan"); setSize(250,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); A = new JLabel("Nilai A:"); txtA = new JTextField(10); B = new JLabel("Nilai B:"); txtB = new JTextField(10); hasil = new JLabel("HasilPerhitungan"); txthasil = new JTextField(10); tambah = new JButton("+"); tambah.addActionListener(this);
  2. 2. bagi = new JButton("/"); bagi.addActionListener(this); kali = new JButton("X"); kali.addActionListener(this); kurang = new JButton("-"); kurang.addActionListener(this); A.setBounds(30,40,100,25); txtA.setBounds(100,40,80,25); B.setBounds(30,100,250,25); txtB.setBounds(100,100,80,25); hasil.setBounds(50,150,150,25); txthasil.setBounds(50,170,150,25); tambah.setBounds(75,200,90,25); bagi.setBounds(75,230,90,25); kali.setBounds(75,260,90,25); kurang.setBounds(75,290,90,25); panel.add(A); panel.add(txtA); panel.add(B);
  3. 3. panel.add(txtB); panel.add(hasil); panel.add(txthasil); panel.add(tambah); panel.add(bagi); panel.add(kali); panel.add(kurang); add(panel); panel.setLayout(null); setVisible(true); } public static void main(String[]args) { newhitungduabilangan(); } public void actionPerformed(ActionEvent e) { String d1 = txtA.getText(); String d2 = txtB.getText(); double a = Integer.parseInt(d1); double b = Integer.parseInt(d2); inthasiljml = (int)(a+b); inthasilbagi = (int)(a/b); inthasilkali = (int)(a*b); inthasilkurang = (int)(a-b); Object objek = e.getSource();
  4. 4. if(objek==tambah) txthasil.setText(new String().valueOf(hasiljml)); { if (objek==bagi) txthasil.setText(new String().valueOf(hasilbagi)); } { if (objek==kali) txthasil.setText(new String().valueOf(hasilkali)); } { if (objek==kurang) txthasil.setText(new String().valueOf(hasilkurang)); }} }

×