Created
May 3, 2013 17:16
-
-
Save Phonbopit/5511500 to your computer and use it in GitHub Desktop.
Simple Random change text background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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