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

用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条回答 默认 最新

  • 尹振坤 2021-12-31 14:55
    关注

    看看是不是想要这个效果:

    img


    img

    如果是,源程序如下:

    
    import javax.swing.*;
    import java.awt.*;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Random;
    import java.util.Vector;
    
    public class Luckymode extends JFrame implements Runnable {
        /**
         * 文本”设置获奖人数:“
         */
        JLabel numLabel, prizeLabel;
        JComboBox<String> numBox, prizeBox;
        JButton startBtn, endBtn;
        int number = 4;
        boolean IS_RUNNING = false;
        int i;
        String FILE_PATH = "D:/information.txt";
        JTextField[] phoneList = new JTextField[10];
        Vector<String> vector = new Vector<>();
    
        Luckymode(String s) {
            super(s);
            initComponents();
        }
    
        public void initComponents() {
            //界面初始化及监听事件
            Container jtogether = getContentPane();
            JPanel jup = new JPanel();
            jup.setLayout(new FlowLayout());
    
            prizeLabel = new JLabel("奖项:");
            String[] prizes = {"一等奖", "二等奖", "三等奖"};
    
            numLabel = new JLabel("人数:");
            String[] nums = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    
            prizeBox = new JComboBox(prizes);
            prizeBox.setSelectedIndex(2);
            prizeBox.addActionListener(e -> {
                IS_RUNNING = false;
                startBtn.setEnabled(true);
                endBtn.setEnabled(false);
                for (i = 0; i < 10; i++) {
                    phoneList[i].setText("");
                }
            });
            numBox = new JComboBox(nums);
            numBox.setSelectedIndex(4);
            numBox.addActionListener(e -> {
                if (e.getSource() == numBox) {
                    IS_RUNNING = false;
                    startBtn.setEnabled(true);
                    endBtn.setEnabled(false);
                    number = numBox.getSelectedIndex();
                    for (i = 0; i < 10; i++) {
    
                        phoneList[i].setEditable(i <= number);
                        phoneList[i].setText("");
                    }
                }
            });
            jup.add(prizeLabel);
            jup.add(prizeBox);
            jup.add(numLabel);
            jup.add(numBox);
    
    
            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++) {
                phoneList[i] = new JTextField("");
                phoneList[i].setHorizontalAlignment(JTextField.CENTER);
                jcenter.add(phoneList[i]);
                phoneList[i].setEditable(i <= number);
            }
            jtemp.add("Center", jcenter);
            jtemp.add("West", j1);
            jtemp.add("East", j2);
            JPanel jdown = new JPanel();
            jdown.setLayout(new FlowLayout());
            startBtn = new JButton("开始");
            endBtn = new JButton("结束");
            startBtn.addActionListener(e -> {
                IS_RUNNING = true;
                startBtn.setEnabled(false);
                endBtn.setEnabled(true);
            });
            endBtn.addActionListener(e -> {
                IS_RUNNING = false;
                startBtn.setEnabled(true);
                endBtn.setEnabled(false);
            });
            endBtn.setEnabled(false);
            jdown.add(startBtn);
            jdown.add(endBtn);
            jtogether.setLayout(new BorderLayout());
            jtogether.add("North", jup);
            jtogether.add("Center", jtemp);
            jtogether.add("South", jdown);
            textinfile();
            setVisible(true);
        }
    
        public String texthand(String a) {
            //将从文件读入的数据处理
            return a.replaceAll("(\\d{3,3}).{4,4}(\\d{4,4})", "$1****$2");
        }
    
        public void textinfile() {
            //将文件中手机号码信息读入vector
            try (FileReader f = new FileReader(FILE_PATH); BufferedReader br = new BufferedReader(f)) {
                String strline = br.readLine();
                while (strline != null) {
                    vector.add(texthand(strline));
                    strline = br.readLine();
                }
            } catch (IOException e) {
                System.err.printf("文件不存在%s%n", e.getMessage());
            }
        }
    
        public void run() {      //线程运行
            Random random = new Random();
            while (true) {
                try {
                    Thread.sleep((long) ((Math.random() * 100) + 50));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.printf("is_running:%s%n", IS_RUNNING);
                if (IS_RUNNING) {
                    int index;
                    for (i = 0; i <= number; i++) {
                        if (vector.size() > 0) {
    
                            // 在phoneList[i]中随机显示手机号码
                            index = random.nextInt(vector.size() - 1);
                            System.out.printf("update %d %s%n", index, vector.get(index));
                            phoneList[i].setText(vector.get(index));
                        } else {
                            phoneList[i].setText("无");
                        }
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Luckymode luck = new Luckymode("幸运手机号码抽取");
            luck.setSize(300, 450);
            luck.setLocation(800, 300);
            luck.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            new Thread(luck).start();
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题