Kamis, 20 Maret 2014

Tulisan Berjalan Horizontal Berbasis GUI di Java (Thread)

yuuuuhuuu praktikum lagi,,, bisa nulis lagi
langsung aja. soal pertama praktikum SISTER (Sistem Terdistribusi) pertemuan ke 3 ini adalah:
membuat tulisan berjalan secara horizontal dengan berbasis GUI. didalamnya terdapat tombol untuk menghentikan maupun menjalankan tulisan tersebut. screenchot programnya kaya gini:
langkah pertama buat dulu tampilan di JFrame Form kaya diatas. komponen-komponen dari form tersebut kita kasih nama
jLabel2: Jalan Jalan Siang
berhenti: tombol Berhenti
lanjut: tombol Lanjutkan
Kemudian ketikkan sourcecode kaya dibawah ini:

package sister3;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jazilah
 */
public class no1 extends javax.swing.JFrame implements Runnable{
 boolean horz = false;
    int x, y, h, w;
    boolean kanankiri = true;
   
    public no1() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
 private void berhentiActionPerformed(java.awt.event.ActionEvent evt) {                                        
         horz = true;
    }                                       

    private void lanjutActionPerformed(java.awt.event.ActionEvent evt) {                                      
 new Thread(this).start();
        horz = false;
    }
 public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* 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(no1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(no1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(no1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(no1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new no1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton berhenti;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton lanjut;
    // End of variables declaration                  

       public void run() {
        while (horz == false) {
            if (kanankiri == true) {
                x++;
                if (x == 200) {
                    kanankiri = false;
                }

            } else {
                x--;
                if (x == 0) {
                    kanankiri = true;
                }
            }
           
            jLabel2.setBounds(x, 20, 400, 40);
            System.out.println("x = "+x);
            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                Logger.getLogger(Tugas1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
}
sourceCode lengkapnya bisa di unduh di sini


Tidak ada komentar:

Posting Komentar