Skip to content

Instantly share code, notes, and snippets.

@Phonbopit
Created May 3, 2013 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phonbopit/5511500 to your computer and use it in GitHub Desktop.
Save Phonbopit/5511500 to your computer and use it in GitHub Desktop.
Simple Random change text background
public class SimpleRandom extends javax.swing.JFrame {
Thread auto;
public SimpleRandom() {
initComponents();
auto = new Thread() {
@Override
public void run() {
while(true) {
try {
sleep(1000);
int r = randColor();
int g = randColor();
int b = randColor();
//txtHello คือ JTextField ใน JFrame
txtHello.setBackground(new Color(r, g, b));
} catch (InterruptedException ex) {
//ignore
}
}
};
};
}
//สำหรับรับค่าจาก button ชื่อ btnClick
private void btnclickActionPerformed(java.awt.event.ActionEvent evt) {
//กดปุ่มแล้ว start thread.
if(!auto.isAlive()) {
auto.start();
}
}
//random สี แล้วส่งกลับเป็น int (0-255)
private int randColor() {
Random rand = new Random();
return rand.nextInt(256);
}
/* ******************
* initComponent()....
* main (String[] args)....
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment