Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

LAPORAN PRAKTIKUM

BAHASAM PEMOGRAAMAN 1
GRAPHICAL USER INTERFACE (1)
MODUL 7

Di Susun Oleh :

Nama : AJI AGUNG SAPUTRA


Kelas : TI 2017 A
NIM : 20170810003

TEKNIK INFORMATIKA
FAKULTAS ILMU KOMPUTER
UNIVERSITAS KUNINGAN
2018
A. Pre Test.

B. Praktikum.
Latihan 1.

package modul7bp1;
import javax.swing.*;
import java.awt.event.*;
public class Latihan01 extends JFrame implements ActionListener {
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();

JTextField text1 = new JTextField();


JTextField text2 = new JTextField();
JTextField text3 = new JTextField();

JButton button1 = new JButton();

public void CiptakanJFrame(){


setLayout (null);
setTitle ("Pesan");
setVisible (true);
setSize (300, 150);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setResizable (false);
setLocationRelativeTo(null);
}

public void CiptakanJLabel(){


label1.setText("NIM");
label1.setBounds (10,10,250,20);
add(label1);

label2.setText("NAMA");
label2.setBounds (10,30,250,20);
add(label2);

label3.setText("PROGRAM STUDI");
label3.setBounds (10,10,250,20);
add(label3);
}

public void CiptakanJTextField(){


text1.setBounds (120, 10,160,20);
add (text1);
text2.setBounds (120, 30,160,20);
add (text2);
text3.setBounds (120, 50,160,20);
add (text3);
}
public void ciptakanJButton(){
button1.setText("Tampilkan");
button1.setBounds (120,80,95,20);
button1.addActionListener(this);
add (button1);
}

@Override
public void actionPerformed(ActionEvent e){
text1.setText("017006001");
text2.setText("AJi Agung Saputra");
text3.setText("Teknik Informatika");

}
public static void main(String[] args) {

Latihan01 f1 = new Latihan01();


f1.CiptakanJFrame();
f1.CiptakanJLabel();
f1.CiptakanJTextField();
f1.ciptakanJButton();
}

}
Outputnya.

C. Post Test.
1. Buatlah program kalkulator dengan tampilan seperti gambar berikut.

Jawab:
Klikan code berikut :
package Modul7;
public class PostTest extends javax.swing.JFrame {
String angka;
int pilih;
Double angka1,angka2,jumlah;
public PostTest() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {

display = new javax.swing.JTextField();


jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("C");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("/");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("7");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("8");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setText("9");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setText("*");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

jButton7.setText("4");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});

jButton8.setText("5");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});

jButton9.setText("6");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});

jButton10.setText("-");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});

jButton11.setText("1");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});

jButton12.setText("2");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});

jButton13.setText("3");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
jButton14.setText("+");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});

jButton15.setText("0");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});

jButton16.setText(".");

jButton17.setText("=");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jButton7,
javax.swing.GroupLayout.PREFERRED_SIZE, 39,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.R
ELATED)
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.R
ELATED)
.addComponent(jButton9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.R
ELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton10,
javax.swing.GroupLayout.PREFERRED_SIZE, 41,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 125,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.RELATED))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.RELATED)
.addComponent(jButton5)
.addGap(2, 2, 2)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.TRAILING)
.addComponent(jButton2)
.addComponent(jButton6)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton11)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.RELATED)
.addComponent(jButton12))
.addComponent(jButton15,
javax.swing.GroupLayout.PREFERRED_SIZE, 84,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.R
ELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING, false)
.addComponent(jButton16,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton13,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.RELATED)
.addComponent(jButton14))
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jButton17,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE,
168, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(50, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE,
53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton10))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton11)
.addComponent(jButton12)
.addComponent(jButton13)
.addComponent(jButton14))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jButton15)
.addComponent(jButton16)
.addComponent(jButton17))
.addContainerGap(115, Short.MAX_VALUE))
);
pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


display.setText("");
angka1=0.0;
angka2=0.0;
jumlah=0.0;
angka="";
}

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {


angka1=Double.parseDouble(angka);
display.setText("+");
angka="";
pilih=1;
}

private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="0";
display.setText(angka);
}

private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {


switch(pilih){
case 1:angka2=Double.parseDouble(angka);
jumlah=angka1+angka2;
angka=Double.toString(jumlah);
display.setText(angka);break;
case 2:angka2=Double.parseDouble(angka);
jumlah=angka1-angka2;
angka=Double.toString(jumlah);
display.setText(angka);break;
case 3:angka2=Double.parseDouble(angka);
jumlah=angka1*angka2;
angka=Double.toString(jumlah);
display.setText(angka);break;
case 4:angka2=Double.parseDouble(angka);
jumlah=angka1/angka2;
angka=Double.toString(jumlah);
display.setText(angka);break;
}
}

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="1";
display.setText(angka);
}

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="2";
display.setText(angka);
}

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="3";
display.setText(angka);
}

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="6";
display.setText(angka);
}

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="5";
display.setText(angka);
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="4";
display.setText(angka);
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="7";
display.setText(angka);
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="8";
display.setText(angka);
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {


angka+="9";
display.setText(angka);
}

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {


angka1=Double.parseDouble(angka);
display.setText("-");
angka="";
pilih=2;
}

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {


angka1=Double.parseDouble(angka);
display.setText("*");
angka="";
pilih=3;
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


angka1=Double.parseDouble(angka);
display.setText("/");
angka="";
pilih=4;
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(PostTest.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(PostTest.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(PostTest.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(PostTest.class.getName()).log(java.util.logging
.Level.SEVERE, null, ex);
}

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PostTest().setVisible(true);
}
});
}

private javax.swing.JTextField display;


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
}

Hasil Outputnya.
Di sini saya mencoba perkalian maka hasilnya berikut.

You might also like