Ai
1 Star 0 Fork 0

lain/OOP

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
UI.java 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
lain 提交于 2024-12-01 00:50 +08:00 . feat: Add basic UI with random name generation
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();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lain_exe/oop.git
git@gitee.com:lain_exe/oop.git
lain_exe
oop
OOP
master

搜索帮助