2013-06-07 19:44:02Morris
[JAVA][作業] 簡易抽籤
自製一個抽籤系統,包含一個按鈕 ( “–Start–” )
按一次是開始產生亂碼,按第二次則是由左至右慢慢停下,按鈕可以重複使用。
package ce1002.E9.s100502205;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;
import java.util.Timer;
public class E9 extends JFrame {
int draw[] = { 100201021, 100201510, 100502006, 100502201, 100502203,
100502205, 100502515, 101502001, 101502002, 101502003, 101502004,
101502005, 101502006, 101502007, 101502008, 101502009, 101502010,
101502012, 101502013, 101502014, 101502015, 101502016, 101502017,
101502018, 101502019, 101502020, 101502021, 101502022, 101502023,
101502024, 101502025, 101502026, 101502027, 101502028, 101502029,
101502030, 101502501, 101502502, 101502503, 101502504, 101502505,
101502506, 101502507, 101502508, 101502509, 101502510, 101502511,
101502512, 101502513, 101502514, 101502515, 101502516, 101502517,
101502518, 101502519, 101502520, 101502521, 101502522, 101502523,
101502524, 101502525, 101502526, 101502527, 101502528, 101502556,
976002019, 982001011, 995002014, 995002022, 995002525 };
RPanel panel;
JButton jbtn;
public E9() {
this.setTitle("E9");
this.setSize(500, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(new GridBagLayout());
jbtn = new JButton("-start-");
panel = new RPanel();
GridBagConstraints GBC = new GridBagConstraints();
GBC.gridx = 5;
GBC.gridy = 5;
GBC.gridwidth = 9;
GBC.gridheight = 1;
GBC.ipadx = 0;
GBC.ipady = 0;
GBC.weightx = 0;
GBC.weighty = 0;
GBC.insets = new Insets(10, 10, 10, 10);
GBC.fill = GridBagConstraints.HORIZONTAL;
GBC.anchor = GridBagConstraints.EAST;
this.add(panel, GBC);
GBC.gridx = 20;
GBC.gridy = 8;
GBC.gridwidth = 2;
GBC.gridheight = 1;
GBC.ipadx = 0;
GBC.ipady = 0;
GBC.weightx = 0;
GBC.weighty = 0;
GBC.insets = new Insets(10, 10, 10, 10);
GBC.fill = GridBagConstraints.HORIZONTAL;
GBC.anchor = GridBagConstraints.EAST;
this.add(jbtn, GBC);
jbtn.addActionListener(new ActionListener() {
int count = 0;
public void actionPerformed(ActionEvent e) {
if (count % 2 == 0) {
if (panel.hasDemo == true || panel.hasRun == true)
return;
panel.randomRun();
count++;
} else {
if (panel.hasDemo == true && panel.hasRun == true)
return;
panel.demo();
count++;
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new E9();
}
class RPanel extends JPanel {
String showNumber;
boolean hasRun, hasDemo;
boolean[] digit = new boolean[9];
Timer timer, countTimer;
JLabel[] field = new JLabel[9];
int[] fieldNumber = new int[9];
Font font = new Font("Serif", Font.BOLD, 80);
public RPanel() {
hasRun = false;
hasDemo = false;
this.setLayout(new GridLayout(1, 9));
for (int i = 0; i < 9; i++) {
field[i] = new JLabel("" + 9);
fieldNumber[i] = i;
field[i].setFont(font);
digit[i] = true;
add(field[i]);
}
timer = new Timer();
timer.schedule(new Heart(), 100, 30);
}
public void setNumber(int x) {
showNumber = String.format("%09d", x);
}
public void randomRun() {
hasRun = true;
for (int i = 0; i < 9; i++) {
digit[i] = true;
fieldNumber[i] = (int) (Math.random()*10);
}
}
public void demo() {
setNumber(draw[(int) (Math.random() * draw.length)]);
hasDemo = true;
countTimer = new Timer();
countTimer.schedule(new Count(), 500, 500);
}
class Count extends TimerTask {
int Idx = 0;
public void run() {
digit[Idx] = false;
field[Idx].setText("" + showNumber.charAt(Idx));
Idx++;
repaint();
if (Idx == 9) {
hasRun = false;
hasDemo = false;
countTimer.cancel();
}
}
}
class Heart extends TimerTask {
public void run() {
if (hasRun == true) {
for (int i = 0; i < 9; i++) {
if (digit[i] == true) {
fieldNumber[i]++;
fieldNumber[i] %= 10;
field[i].setText("" + fieldNumber[i]);
}
field[i].repaint();
}
repaint();
}
}
}
}
}
上一篇:[JAVA][作業] 做個拉霸?
下一篇:[JAVA] 實作拖曳功能