Java GUI – Program Menghitung Luas Segitiga

Menghitung Luas Segitiga dengan Java GUI

Langkah pertama membuat form dengan menggunakan komponen :

> Label

> JTexField

> JButton

Tampilan formnya sebagai berikut.. :


Listing Program :


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ade
 */
public class Luas extends javax.swing.JFrame {

/** Creates new form Luas */
 public Luas() {
 super("Lus Lingkaran");
 initComponents();
 }

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
 @SuppressWarnings("unchecked")
 //
 private void initComponents() {

jLabel1 = new javax.swing.JLabel();
 jLabel2 = new javax.swing.JLabel();
 txt1 = new javax.swing.JTextField();
 txt2 = new javax.swing.JTextField();
 txt3 = new javax.swing.JTextField();
 jLabel3 = new javax.swing.JLabel();
 jLabel4 = new javax.swing.JLabel();
 jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
 jLabel1.setText("PROGRAM MENCARI LUAS SEGITIGA");
 jLabel1.setName("jLabel1"); // NOI18N

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
 jLabel2.setText("ALAS");
 jLabel2.setName("jLabel2"); // NOI18N

txt1.setName("txt1"); // NOI18N

txt2.setName("txt2"); // NOI18N

txt3.setName("txt3"); // NOI18N

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
 jLabel3.setText("TINGGI");
 jLabel3.setName("jLabel3"); // NOI18N

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
 jLabel4.setText("LUAS");
 jLabel4.setName("jLabel4"); // NOI18N

jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
 jButton1.setText("HITUNG");
 jButton1.setName("jButton1"); // NOI18N
 jButton1.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 jButton1ActionPerformed(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(122, 122, 122)
 .addComponent(jLabel1)
 .addContainerGap(93, Short.MAX_VALUE))
 .addGroup(layout.createSequentialGroup()
 .addGap(25, 25, 25)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(jLabel4)
 .addComponent(jLabel2)
 .addComponent(jLabel3))
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGap(19, 19, 19)
 .addComponent(txt1, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
 .addGroup(layout.createSequentialGroup()
 .addGap(18, 18, 18)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(txt3, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
 .addComponent(txt2, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
 .addComponent(jButton1))))
 .addGap(42, 42, 42))
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGap(38, 38, 38)
 .addComponent(jLabel1)
 .addGap(18, 18, 18)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(jLabel2)
 .addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(jLabel3)
 .addComponent(txt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(jLabel4)
 .addComponent(txt3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 .addComponent(jButton1)
 .addContainerGap(32, Short.MAX_VALUE))
 );

pack();
 }//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
 double alas=Integer.parseInt(txt1.getText());
 double tinggi=Integer.parseInt(txt2.getText());

 double hasil=(alas*tinggi)/2;
 txt3.setText(String.valueOf(hasil));

}

/**
 * @param args the command line arguments
 */
 public static void main(String args[]) {
 /* Set the Nimbus look and feel */
 //
 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
 */
 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(Luas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (InstantiationException ex) {
 java.util.logging.Logger.getLogger(Luas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (IllegalAccessException ex) {
 java.util.logging.Logger.getLogger(Luas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
 java.util.logging.Logger.getLogger(Luas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 }
 //

/* Create and display the form */
 java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
 new Luas().setVisible(true);
 }
 });
 }
 // Variables declaration - do not modify
 private javax.swing.JButton jButton1;
 private javax.swing.JLabel jLabel1;
 private javax.swing.JLabel jLabel2;
 private javax.swing.JLabel jLabel3;
 private javax.swing.JLabel jLabel4;
 private javax.swing.JTextField txt1;
 private javax.swing.JTextField txt2;
 private javax.swing.JTextField txt3;
 // End of variables declaration
}

Output Program :

Tampilan Program ketika di RUN

About Ade Akbar

Hidup, Berarti dan Mati

Posted on Desember 5, 2011, in Program Java. Bookmark the permalink. 4 Komentar.

  1. Cobaaaaa ajj program ini waktu aq masih SD, ga perlu repot2 ngapalin rumus..
    tp kok cm segitiga?? kerucut, belah ketupat,layang-layang, trapesium, dll sekalian yahh.. semangaaaatttt😀

  2. waktu masih SD masih jamannya tradisiorong…. sekarang jaman modern hehehehehhe

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: