Beranda > Java Programming > Java Interface (basic program :: operator math)

Java Interface (basic program :: operator math)


Praktikum ke-4 java sama seperti yang ke-3 lalu, hanya saja ada beberapa tambahan komponen seperti ComboBox.
Berikut gambar interface soalnya:

jawaban ane, hmm masih amatiran sih, tapi kata dosen ane, “bisa”, alhamdulillah..🙂

file lat1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class lat1 extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel(“Masukan Var1 “);
JTextField txt1=new JTextField(12);
JLabel lbl2=new JLabel(“Masukan Var2 “);
JTextField txt2=new JTextField(12);
JLabel lbl3=new JLabel(“Operator “);
JComboBox cb=new JComboBox();
JButton btn1=new JButton(” H a s i l “);
JButton btn2=new JButton(” Keluar “);
Container c=new Container();
JTextField txt3=new JTextField(12);
lat1()
{
super(“Latihan 1-yHan”);
cb.addItem(” + “);
cb.addItem(” – “);
cb.addItem(” * “);
cb.addItem(” / “);

c=getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
c.add(lbl1);
c.add(txt1);
c.add(lbl2);
c.add(txt2);
c.add(lbl3);
c.add(cb);
c.add(btn1);
c.add(txt3);
c.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btn1)
{

if (cb.getSelectedItem()==” + “)
{
Double a,b,c;
String s;
a=Double.parseDouble(txt1.getText());
b=Double.parseDouble(txt2.getText());
c=a+b;
s=String.valueOf(c);
txt3.setText(s);
}

if (cb.getSelectedItem()==” – “)
{
Double a,b,c;
String s;
a=Double.parseDouble(txt1.getText());
b=Double.parseDouble(txt2.getText());
c=a-b;
s=String.valueOf(c);
txt3.setText(s);
}

if (cb.getSelectedItem()==” * “)
{
Double a,b,c;
String s;
a=Double.parseDouble(txt1.getText());
b=Double.parseDouble(txt2.getText());
c=a*b;
s=String.valueOf(c);
txt3.setText(s);
}
if (cb.getSelectedItem()==” / “)
{
Double a,b,c;
String s;
a=Double.parseDouble(txt1.getText());
b=Double.parseDouble(txt2.getText());
c=a/b;
s=String.valueOf(c);
txt3.setText(s);
}
}
if(e.getSource()==btn2)
System.exit(0);
}

public static void main(String [] arg)
{
JFrame.setDefaultLookAndFeelDecorated(true);
lat1 x=new lat1();
x.setSize(270,250);
x.setLocation(300,300);
x.setVisible(true);
}
}

  1. Belum ada komentar.
  1. No trackbacks yet.

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

%d blogger menyukai ini: