Vous êtes sur la page 1sur 4

Animasi tombol warna berjalan Java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.

*; public class AnimasiTombol extends JFrame implements ActionListener { JButton jbLight1 = new JButton(); JButton jbLight2 = new JButton(); JButton jbLight3 = new JButton(); JButton jbLight4 = new JButton(); JButton jbLight5 = new JButton(); JButton jbLight6 = new JButton(); JButton jbLight7 = new JButton(); JButton jbLight8 = new JButton(); JButton jbLight9 = new JButton(); JButton jbLight10 = new JButton(); JButton jbRight = new JButton(" GRK KE KANAN > "); JButton jbLeft = new JButton(" < GRK KE KIRI "); JButton stop = new JButton("STOP"); JButton keluar = new JButton("KELUAR"); javax.swing.Timer timer = new javax.swing.Timer(1000, this); int jbNo = 1; int status=0; public AnimasiTombol() { setLayout(null); setComponents(); setTitle("WARNA BERJALAN"); setSize(610, 120); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); } public void setComponents() { jbLight1.setBounds(10, 20, 50, 20); jbLight2.setBounds(70, 20, 50, 20); jbLight3.setBounds(130, 20, 50, 20); jbLight4.setBounds(190, 20, 50, 20); jbLight5.setBounds(250, 20, 50, 20); jbLight6.setBounds(310, 20, 50, 20);

jbLight7.setBounds(370, 20, 50, 20); jbLight8.setBounds(430, 20, 50, 20); jbLight9.setBounds(490, 20, 50, 20); jbLight10.setBounds(550, 20, 50, 20); jbLight1.setBackground(Color.green); jbLight2.setBackground(Color.black); jbLight3.setBackground(Color.black); jbLight4.setBackground(Color.black); jbLight5.setBackground(Color.black); jbLight6.setBackground(Color.black); jbLight7.setBackground(Color.black); jbLight8.setBackground(Color.black); jbLight9.setBackground(Color.black); jbLight10.setBackground(Color.black); jbLeft.setBounds(10, 50, 125, 20); jbRight.setBounds(150, 50, 140, 20); stop.setBounds(400, 50, 80, 20); keluar.setBounds(500, 50, 80, 20); add(jbLight1); add(jbLight2); add(jbLight3); add(jbLight4); add(jbLight5); add(jbLight6); add(jbLight7); add(jbLight8); add(jbLight9); add(jbLight10); add(jbRight); add(jbLeft); add(stop); add(keluar); jbRight.addActionListener(this); jbLeft.addActionListener(this); stop.addActionListener(this); keluar.addActionListener(this); } public void setButtonLight() { jbLight1.setBackground(Color.black); jbLight2.setBackground(Color.black); jbLight3.setBackground(Color.black); jbLight4.setBackground(Color.black); jbLight5.setBackground(Color.black); jbLight6.setBackground(Color.black); jbLight7.setBackground(Color.black);

jbLight8.setBackground(Color.black); jbLight9.setBackground(Color.black); jbLight10.setBackground(Color.black); switch ( jbNo ) { case 1: jbLight1.setBackground(Color.yellow); break; case 2: jbLight2.setBackground(Color.yellow); break; case 3: jbLight3.setBackground(Color.yellow); break; case 4: jbLight4.setBackground(Color.yellow); break; case 5: jbLight5.setBackground(Color.yellow); break; case 6: jbLight6.setBackground(Color.yellow); break; case 7: jbLight7.setBackground(Color.yellow); break; case 8: jbLight8.setBackground(Color.yellow); break; case 9: jbLight9.setBackground(Color.yellow); break; case 10: jbLight10.setBackground(Color.yellow); break; } } public void actionPerformed(ActionEvent ae) { if (ae.getSource()==jbLeft) { status=0; timer.start(); } else if (ae.getSource()==jbRight) { status=1; timer.start(); } else if (ae.getSource()==stop) { timer.stop(); } else if (ae.getSource()==keluar) { System.exit(0); } else if (ae.getSource()==timer) {

if (status==0) { jbNo-=1; } else if (status==1) { jbNo+=1; } if (jbNo>10) { jbNo=1; } else if(jbNo==0) { jbNo=10; }

setButtonLight(); } } public static void main(String[] args) { new AnimasiTombol(); } }

Vous aimerez peut-être aussi