m0_59696848 2021-12-31 13:57 采纳率: 100%
浏览 118
已结题

用java实现号码抽取的程序,运行无结果

用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 上海

  • 写回答

3条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 1月9日
      • 已采纳回答 1月1日
      • 修改了问题 12月31日
      • 创建了问题 12月31日

      悬赏问题

      • ¥15 protel99.SE提示一下弹窗
      • ¥15 银河麒麟v10 执行.run失败如何解决
      • ¥15 如何用Python打开LA文件
      • ¥15 用mysql做一个高校工资管理系统
      • ¥21 Multisim设计用三端集成稳压器设计稳压电路
      • ¥15 tp6,在iframe里的网址通过获取url参数失败
      • ¥15 打开opendaylight查看拓扑,为什么直接就显示了两个Switch,在mininet配置了一个topo,这两个Switch还在
      • ¥15 无法远程连接pgsql
      • ¥15 建立DLNM模型时显示基础基矩阵不一致
      • ¥15 贝叶斯重复测量方差分析