JAva awtランダム選択を実現
4888 ワード
友达が授业を选んで难しいことを犯したので、私はあなたにくじを引くプログラムを书いたほうがいいと言って、天意を见て、いいですよ.そこで、やると言ったらやります.以下はソースコードです.参考にしてください.
public class SelectClass extends JFrame {
private static final long serialVersionUID = 1L;
JPanel pn1 = null;
JPanel pn2;
JPanel pn3;
JTextArea tf = null;
JLabel msg = null;
JButton btn1;
JButton btn2;
JButton btn3;
boolean flag = true;
// ,
public SelectClass() {
tf = new JTextArea(2, 40);
pn1 = new JPanel();
//
pn1.add(tf);
this.add(pn1, BorderLayout.NORTH);
msg = new JLabel(" !");
msg.setForeground(Color.red);
msg.setFont(new Font(" ", Font.PLAIN, 80));
pn2 = new JPanel();
pn2.add(msg);
//
this.add(pn2, BorderLayout.CENTER);
//
btn1 = new JButton(" ");
btn3 = new JButton(" ");
//
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*
* , , flag chouJiang(), 。
*/
class ThreadStart extends Thread {
@Override
public void run() {
//
flag = true;
chouJiang();
}
}
// , ,
new ThreadStart().start();
}
});
btn2 = new JButton(" ");
//
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// ,
class ThreadStop extends Thread {
@Override
public void run() {
flag = false;
}
}
//
new ThreadStop().start();
}
});
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chouJiang2();
}
});
pn3 = new JPanel();
pn3.add(btn1);
pn3.add(btn2);
pn3.add(btn3);
this.add(pn3, BorderLayout.SOUTH);
//
this.setTitle(" ===>");
this.setIconImage(getIconImage());
this.setSize(500, 300);
this.setLocation(500, 280);
// JFrame , ,
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// ,
public void chouJiang() {
String text = ""; //
int i = 0;
String[] arr = { " ( )", " ", " ", " ( )", " ", " ", " ( )", " ",
" ", " ", " ", " ", " ", " " };
while (flag) {
text = "";
i = (int) Math.floor(Math.random() * arr.length + 1) - 1;
System.out.println(i);
text = arr[i];
try {
Thread.sleep(35);
} catch (InterruptedException e) {
e.printStackTrace();
}
//
tf.setText(text);
tf.setFont(new Font(" ", Font.PLAIN, 20));
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void chouJiang2() {
String[] arr = { " ( )", " ", " ", " ( )", " ", " ", " ( )", " ",
" ", " ", " ", " ", " ", " " };
TreeMap map = new TreeMap();// key ,value
String text = "";
for (int i = 0; i < 100000; i++) {
System.out.println("---" + i);
text = "";
int j = (int) Math.floor(Math.random() * arr.length + 1) - 1;
text = arr[j];
// ,key ,value 1
if (map.get(text) == null) {
map.put(text, new Integer(1));
// , ,key , value 。
} else {
int value = ((Integer) (map.get(text))).intValue();
map.put(text, new Integer(value + 1));
}
}
Collection c = map.values();
Iterator extends Integer> i = (Iterator extends Integer>) c.iterator();
Integer candidate = i.next();
while (i.hasNext()) {
Integer next = i.next();
if (next.compareTo(candidate) > 0)
candidate = next;
}
int max = candidate;//
System.out.println("--------" + max);
ArrayList list = new ArrayList();//
Set set = map.entrySet();
//
for (Iterator it = set.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String key = entry.getKey().toString();
int value = ((Integer) entry.getValue()).intValue();
if (value == max) {
list.add(key);
}
System.out.println(key + " appeared " + value + " times");
}
System.out.println(list);
//
tf.setText(" 100000 " + "
" + " " + list.get(0) + ", " + max + " ");
tf.setFont(new Font(" ", Font.PLAIN, 16));
}
public static void main(String[] args) {
new SelectClass();
}
}