Sebenernya ane g tau nama aslinya tuh meteran :))
Tapi skip aje...
Oiya, trimakasih kepada lek frans dan juga pakdhe bagas yang telah ambil bagian dalam pengerjaan ni thread...
:D
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Thread.java
*
* Created on Oct 10, 2011, 4:05:09 PM
*/
/**
*
* @author MAGISTRATE
*/
public class GUI_Thread extends javax.swing.JFrame{
/** Creates new form Thread */
public GUI_Thread() {
initComponents();
}
MyThread baru = new MyThread();
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
kecepatan = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
start = new javax.swing.JButton();
stop = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
kilometer = new javax.swing.JLabel();
meter = new javax.swing.JLabel();
komentar = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
author = new javax.swing.JMenuItem();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Masukkan Kecepatan");
kecepatan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
kecepatanActionPerformed(evt);
}
});
jLabel2.setText("Km/h");
start.setText("Start");
start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startActionPerformed(evt);
}
});
stop.setText("Stop");
stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopActionPerformed(evt);
}
});
jLabel4.setText("Kilometer");
jLabel5.setText("Meter");
kilometer.setText("0");
meter.setText("0");
komentar.setText("........................................");
jMenu1.setText("File");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
author.setText("Author");
author.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
authorActionPerformed(evt);
}
});
jMenu1.add(author);
jMenuItem1.setText("Exit");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(kecepatan, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(start)
.addGap(18, 18, 18)
.addComponent(stop))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(kilometer, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(13, 13, 13)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(meter, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addComponent(komentar))
.addContainerGap(25, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(kecepatan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(stop)
.addComponent(start))
.addGap(39, 39, 39)
.addComponent(komentar)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(meter)
.addComponent(kilometer))
.addContainerGap(39, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
kilometer.setText("0");
if (kecepatan.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Isi Kecepatan Sebelum Tancap Gas");
}
Thread t = new Thread(baru);
t.start();
}//GEN-LAST:event_startActionPerformed
private void kecepatanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kecepatanActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_kecepatanActionPerformed
private void stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopActionPerformed
baru.diam();
}//GEN-LAST:event_stopActionPerformed
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
}//GEN-LAST:event_jMenu1ActionPerformed
private void authorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_authorActionPerformed
JOptionPane.showMessageDialog(null, "Created by :\nCahyo Dimas K - 672010012");
}//GEN-LAST:event_authorActionPerformed
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
System.exit(0);
}//GEN-LAST:event_jMenuItem1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI_Thread().setVisible(true);
}
});
}
public class MyThread extends Thread implements Runnable{
boolean jalan;
@Override
public void run(){
int counter = 1;
jalan = true;
int cepat = Integer.valueOf(kecepatan.getText());
int speed;
if(cepat < 50){
speed = 500;
}else if(cepat >= 50 && cepat <=100) {
speed = 300;
}else{
speed = 100;
}
while (jalan) {
try {
int i = 0;
komentar.setText("Stop ketika telah mencapai 1000 Meter...");
while(i <= 1000) {
meter.setText((String.valueOf(i)));
Thread.sleep(speed);
i+=100;
if(i == 1000){
kilometer.setText(String.valueOf(counter));
}else{
}
}
} catch (InterruptedException x) {
}
counter++;
}
}
public void diam() {
jalan = false;
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem author;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JTextField kecepatan;
private javax.swing.JLabel kilometer;
private javax.swing.JLabel komentar;
private javax.swing.JLabel meter;
private javax.swing.JButton start;
private javax.swing.JButton stop;
// End of variables declaration//GEN-END:variables
}
Tidak ada komentar:
Posting Komentar