用java实现从本地txt文件中抽取可自定义奖项的号码,能运行但无结果,是生成哈德曼树的问题?
import java.awt.;
import java.util.;
import javax.swing.;
import java.awt.event.;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Luckymode extends JFrame implements Runnable {
JLabel jnum,jtype; //文本”设置获奖人数:“
JComboBox phonenum, prize;
JButton start, end;
int number = 4;
boolean isrun = false;
int i;
String textname="E:/information.txt";
JTextField text[] = new JTextField[10];
Vector v=new Vector();
Luckymode(String s) {
super(s);
initComponents();
}
public void initComponents() { //界面初始化及监听事件
Container jtogether = getContentPane();
JPanel jup = new JPanel();
jup.setLayout(new FlowLayout());
jnum = new JLabel("人数:");
jtype=new JLabel("奖项:");
String[] str = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
String[] strtype={"一等奖","二等奖","三等奖"};
prize = new JComboBox(strtype);
prize.setSelectedIndex(2);
prize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isrun = false;
start.setEnabled(true);
end.setEnabled(false);
for (i = 0; i < 10; i++)
{
text[i].setText("");
}
}
});
phonenum = new JComboBox(str);
phonenum.setSelectedIndex(4);
phonenum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == phonenum) {
isrun = false;
start.setEnabled(true);
end.setEnabled(false);
number = phonenum.getSelectedIndex();
for (i = 0; i < 10; i++) {
text[i].setEditable(true);
text[i].setText("");
}
if (number != 10)
{
for (i = number+1; i < 10; i++) {
text[i].setEditable(false);
}
}
}
}
});
jup.add(jtype);
jup.add(prize);
jup.add(jnum);
jup.add(phonenum);
JPanel jtemp=new JPanel();
jtemp.setLayout(new BorderLayout());
JPanel j1=new JPanel();
j1.add(new JLabel(" "));
JPanel j2=new JPanel();
j2.add(new JLabel(" "));
JPanel jcenter = new JPanel();
jcenter.setLayout(new GridLayout(10, 1, 20, 0));
for (i = 0; i < 10; i++) {
text[i] = new JTextField("");
text[i].setHorizontalAlignment(JTextField.CENTER );
jcenter.add(text[i]);
}
for (i = number+1; i < 10; i++) {
text[i].setEditable(false);
}
jtemp.add("Center",jcenter);
jtemp.add("West",j1);
jtemp.add("East",j2);
JPanel jdown = new JPanel();
jdown.setLayout(new FlowLayout());
start = new JButton("开始");
end = new JButton("结束");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isrun = true;
start.setEnabled(false);
end.setEnabled(true);
}
});
end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isrun = false;
start.setEnabled(true);
end.setEnabled(false);
}
});
end.setEnabled(false);
jdown.add(start);
jdown.add(end);
jtogether.setLayout(new BorderLayout());
jtogether.add("North", jup);
jtogether.add("Center", jtemp);
jtogether.add("South", jdown);
textinfile();
setVisible(true);
}
public String texthand(String a){//将从文件读入的数据处理
String restring,s1,s2;
s1=a.substring(11);
s2=a.substring(0,9);
restring=s2+"**"+s1;
return restring;
}
public void textinfile()
{
FileReader f; //将文件中手机号码信息读入vector
try {
f = new FileReader("E:/information.txt");
BufferedReader br = new BufferedReader(f);
String strline=br.readLine() ;
while(strline!=null){
v.add(texthand(strline));
strline=br.readLine();
}
br.close();
f.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void run() { //线程运行
int k=0;
while (true) {
if(isrun){
int m=(int) (Math.random() * 100) + 50;
try {
Thread.sleep(m);
} catch (Exception e) {
e.printStackTrace();
}
for (i = 0; i <=number; i++) {
if(k>=v.size()){k=0;}
text[i].setText(v.get(k)); //在text[i]中随机显示手机号码
k++;
}
}
}
}
public static void main(String[] args) {
Luckymode luck = new Luckymode("幸运手机号码抽取");
luck.setSize(300, 450);
luck.setLocation(800, 300);
luck.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread t1 = new Thread(luck);
t1.start();
Thread t2 = new Thread(luck);
t2.start();
Thread t3 = new Thread(luck);
t3.start();
}}
文本内容是
15956124165 金华
12345668522 义务
16546554215 温州
12355987266 四川
19847526545 贵州
15698742365 北京
16987452365 绍兴
16558745266 南京
19874536354 天津
13654719989 武汉
13256987455 上海
18746987455 乌鲁木齐
15757424165 金华
12445678522 义务
16447556215 温州
12999987266 四川
17775777545 贵州
17778742365 北京
16955452365 绍兴
16233145286 南京
16458526354 天津
15254779939 武汉
11256987415 上海