#题目要求#
随机生成1000/10000个手机号码,将所有的手机号码记录在D盘下的文件中,要求在用户面板上随机显示这些手机号码并抽取,并且保证10次中有4次概率出现自己的手机号
#提及方法#
Thread 线程
IO流
#代码撰写#
1 .手机号码生成录入
public class phoneNums {
public static void getNums(String[] nums) throws IOException {
FileWriter fileWriter = new FileWriter("D://test//抽奖名单.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int i = 0; i < nums.length; i++) {
Random random = new Random();
// 生成区号
int areaCode = random.nextInt(900) + 100;
// 生成号码
int number = random.nextInt(9000000) + 1000000;
nums[i] = "1" + areaCode + number;
bufferedWriter.write(nums[i]);
bufferedWriter.newLine();
}
bufferedWriter.close();
}
}
2 .线程自定义类创建及run方法重写
public class MyThread extends Thread {
private JTextField showNums;
public MyThread(JTextField showNums) {
this.showNums = showNums;
}
public JTextField getShowNums() {
return showNums;
}
public void setShowNums(JTextField showNums) {
this.showNums = showNums;
}
@Override
public void run() {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
FileReader fileReader;
try {
fileReader = new FileReader("D://test//抽奖名单.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String num = bufferedReader.readLine();
while (num != null) {
list.add(num);
num = bufferedReader.readLine();
}
// 循环
while (true) {
showNums.setText(list.get((int) (Math.random() * list.size())));
Thread.sleep(50);
}
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
} finally {
}
}
}
3 .GUI面板制作
public class chouJiang extends JFrame {
private JPanel contentPane;
private JTextField showNums;
private JButton endButton;
MyThread myThread;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chouJiang frame = new chouJiang();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public chouJiang() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1161, 842);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
showNums = new JTextField();
showNums.setBounds(155, 254, 778, 53);
contentPane.add(showNums);
showNums.setColumns(10);
JButton startButton = new JButton("\u5F00\u59CB");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
startButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
String[] nums = new String[1000];
try {
phoneNums.getNums(nums);
myThread = new MyThread(showNums);
myThread.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
}
}
});
startButton.setBounds(420, 392, 221, 29);
contentPane.add(startButton);
endButton = new JButton("\u505C\u6B62");
endButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int rod =(int)(Math.random()*10);
if(rod<=4) {
showNums.setText("15500216978");
}
myThread.interrupt();
}
});
endButton.setBounds(420, 453, 221, 29);
contentPane.add(endButton);
}
}
#代码实现#