代码拉取完成,页面将自动刷新
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Random;
public class UI {
private Frame win;
private Label l_name;
private Button button;
private Random r = new Random();
public UI() {
win = new Frame();
win.setLayout(new FlowLayout());
win.setVisible(true);
win.setBounds(100, 100, 200, 200);
button = new Button("getName");
l_name = new Label("name");
win.add(l_name);
win.add(button);
button.addActionListener(new NameHolder());
}
class NameHolder implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
int n = r.nextInt(5);
int m = 0;
String name = null;
File file = new File("names.txt");
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while ((name = br.readLine()) != null) {
m = m + 1;
if (m == n) {
System.out.println(n + name);
l_name.setText(name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class TestUI {
public static void main(String[] args) {
new UI();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。