My Picture

More Colour Of My Life

More about me»

contoh Project Java
new story :)



    • PROGRAM 1
      import java.awt.*;
      import java.awt.event.*;
      import java.awt.geom.*;
      import java.util.*;
      import javax.swing.*;


      /**
      8. Shows animated bouncing balls, some running in higher
      9. threads
      10. */
      public class BounceExpress {


      public static void main(String[] args) {
      BounceFrame frame = new BounceFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      }
      }


      /**
      The frame with canvas and buttons.
      */
      class BounceFrame extends JFrame {


      /**
      Constructs the frame with the canvas for showing th
      bouncing ball and Start and Close buttons
      */
      public BounceFrame() {
      setSize(WIDTH, HEIGHT);
      setTitle("BounceExpress");



      Container contentPane = getContentPane();
      canvas = new BallCanvas();
      contentPane.add(canvas, BorderLayout.CENTER);
      JPanel buttonPanel = new JPanel();
      addButton(buttonPanel, "Start",
      new ActionListener() {


      public void actionPerformed(ActionEvent ev) {
      addBall(Thread.NORM_PRIORITY, Color.black);
      }
      });


      addButton(buttonPanel, "Express",
      new ActionListener() {


      public void actionPerformed(ActionEvent ev) {
      addBall(Thread.NORM_PRIORITY + 2, Color.cyan);
      }
      });


      addButton(buttonPanel, "Close",
      new ActionListener() {


      public void actionPerformed(ActionEvent ev) {
      System.exit(0);
      }
      });
      contentPane.add(buttonPanel, BorderLayout.SOUTH);
      }


      /**
      Adds a button to a container.
      @param c the container
      @param title the button title
      @param listener the action listener for the button
      */
      public void addButton(Container c, String title,
      ActionListener listener) {
      JButton button = new JButton(title);
      c.add(button);
      button.addActionListener(listener);
      }


      /**
      Adds a bouncing ball to the canvas and starts a thr
      to make it bounce
      @param priority the priority for the threads
      @color the color for the balls
      */
      public void addBall(int priority, Color color) {
      Ball b = new Ball(canvas, color);
      canvas.add(b);
      BallThread thread = new BallThread(b);
      thread.setPriority(priority);
      thread.start();
      }
      private BallCanvas canvas;
      public static final int WIDTH = 450;
      public static final int HEIGHT = 350;
      }


      /**
      A thread that animates a bouncing ball.
      */
      class BallThread extends Thread {


      /**
      Constructs the thread.
      @aBall the ball to bounce
      */
      public BallThread(Ball aBall) {
      b = aBall;
      }


      @Override
      public void run() {
      try {
      for (int i = 1; i <= 1000; i++) {
      b.move();
      sleep(1000);
      }
      } catch (InterruptedException exception) {
      }
      }
      private Ball b;
      }


      /**
      The canvas that draws the balls.
      */
      class BallCanvas extends JPanel {


      /**
      Add a ball to the canvas.
      @param b the ball to add
      */
      public void add(Ball b) {
      balls.add(b);
      }


      public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      for (int i = 0; i < balls.size(); i++) {
      Ball b = (Ball) balls.get(i);
      b.draw(g2);
      }
      }
      private ArrayList balls = new ArrayList();
      }










      /**
      A ball that moves and bounces off the edges of a
      component
      */
      class Ball {


      /**
      Constructs a ball in the upper left corner
      @c the component in which the ball bounces
      @aColor the color of the ball
      */
      public Ball(Component c, Color aColor) {
      canvas = c;
      color = aColor;
      }


      /**
      Draws the ball at its current position
      @param g2 the graphics context
      */
      public void draw(Graphics2D g2) {
      g2.setColor(color);
      g2.fill(new Ellipse2D.Double(x, y, XSIZE, YSIZE));
      }


      /**
      Moves the ball to the next position, reversing dire
      if it hits one of the edges
      */
      public void move() {
      x += dx;
      y += dy;
      if (x < 0) {
      x = 0;
      dx = -dx;
      }
      if (x + XSIZE >= canvas.getWidth()) {
      x = canvas.getWidth() - XSIZE;
      dx = -dx;
      }
      if (y < 0) {
      y = 0;
      dy = -dy;
      }
      if (y + YSIZE >= canvas.getHeight()) {
      y = canvas.getHeight() - YSIZE;
      dy = -dy;
      }


      canvas.repaint();
      }
      private Component canvas;
      private Color color;
      private static final int XSIZE = 15;
      private static final int YSIZE = 15;
      private int x = 0;
      private int y = 0;
      private int dx = 2;
      private int dy = 2;
      }


PROGRAM 2

import javax.swing.JOptionPane;
import model.Guru;
import query.GuruQuery;
import utils.DBConnection;

/**
*
* @author learning
*/
public class GuruForm extends javax.swing.JFrame {

/** Creates new form GuruForm */
public GuruForm() {
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();
txtNip = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtNama = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txtNoTelp = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtAlamat = new javax.swing.JTextField();
btnSimpan = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("NIP");

jLabel2.setText("Nama");

jLabel3.setText("Alamat");

jLabel4.setText("No Telepon");

txtAlamat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtAlamatActionPerformed(evt);
}
});

btnSimpan.setText("Simpan");
btnSimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSimpanActionPerformed(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(71, 71, 71)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtNip, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtNoTelp, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(214, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(204, Short.MAX_VALUE)
.addComponent(btnSimpan)
.addGap(129, 129, 129))
);
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(txtNip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtNoTelp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(46, 46, 46)
.addComponent(btnSimpan)
.addContainerGap(37, Short.MAX_VALUE))
);

pack();
}//


private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {
try {
Guru guru = new Guru();
guru.setNip(txtNip.getText());
guru.setAlamat(txtAlamat.getText());
guru.setNamaGuru(txtNama.getText());
guru.setNoTelp(txtNoTelp.getText());
// GuruQuery guruQ = new GuruQuery(Connec)
DBConnection conn = DBConnection.getInstance();
GuruQuery guruQ = new GuruQuery(conn.getCon());
guruQ.insertGuru(guru);
JOptionPane.showMessageDialog(this, "Data Saved");

} catch (Exception e) {
System.out.print(e);
}

}

private void txtAlamatActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new GuruForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnSimpan;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField txtAlamat;
private javax.swing.JTextField txtNama;
private javax.swing.JTextField txtNip;
private javax.swing.JTextField txtNoTelp;



PROGRAM 3



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

public class ComboBoxTest extends JFrame {

 private JComboBox cmbImage;
 private JLabel label;
 private String arrGambar[] = {"Blushing.png", "Cool.png", "Happy.png","Smile.png","Winking.png"};
 private Icon arrIcon[] = { new ImageIcon(arrGambar[0]),
       new ImageIcon(arrGambar[1]),
       new ImageIcon(arrGambar[2]),
       new ImageIcon(arrGambar[3]),
       new ImageIcon(arrGambar[4])
     };

 public ComboBoxTest() {

  super ("Mencoba Combo Box");
  Container container = getContentPane();
  container.setLayout(new FlowLayout());
  cmbImage = new JComboBox (arrGambar);
  cmbImage.setMaximumRowCount(3);

  cmbImage.addItemListener(
   new ItemListener() {
    public void itemStateChanged (ItemEvent e) {
     if (e.getStateChange() == ItemEvent.SELECTED)
      label.setIcon(arrIcon[cmbImage.getSelectedIndex()]);
    }
   } //end anonimous inner class
  );

  container.add(cmbImage);
  label = new JLabel (arrIcon[0]);
  container.add(label);

  setSize (350,400);
  setLocationRelativeTo(null);
  setVisible(true);
 }

    public static void main (String args[]) {
     ComboBoxTest test = new ComboBoxTest ();
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

} //end of class

MAAF, yang program pertama dan kedua saya ngopas dari project kakak kelas, hehe
yang ke tiga SAYA CUMA BISA REPOST.
karena barusan mengenal JAVA.
mohon berbagi ilmu  dan kritik sarannya iaa..

trimakasi telah mengunjungi blog saya^^
7 comments:
Anonymous

waaa...boleh kenalan gak sama kakak kelasnya itu..?
:))

wealah, mau?
tapi orang nya "nakutin" lho,,
wkwkwk

Anonymous

mau dong...
barangkali aja cocok...:))
gak papa, aku seneng kok kalau dia yang maem...:D

waa,,
bneran ni?
yakin?
kalu ad ap" aq gk iku" lhoo..
xixixi

ni FBnya :
http://www.facebook.com/profile.php?id=100000209468245

Anonymous

yakin dong..
wah manis ya dia..
:P
kamu gak naksir a sama dia?
HAHAHA :D

ha?
manis??
kecap dong manis?
naksir,
wah ntar kayaknya ada yang marah nii..
enggak berani deh..
hahaha

Anonymous

enak aja kecap..
madu tah lebih manis..:P
iya...hayo naksir ya?
gak usah pura2 wes...
hahaha :D
*gueje -.-

Post a Comment

kasi komentar ya :

kehidupan ini berwarna warni, mari kita berbagi agar tercipta pelangi ... :))

Anda Pengunjung Ke

Followers

Powered by Blogger.