思路:
- 把长连接弄好
- 课堂详细页面可以看到班级的所有学生,双击学生弹窗,可以确认从该课程中移除学生
- 教师可以在班级详细页面通过输入学生的id,将学生加入该班级
- 做好签到的页面,输入签到标题,选择签到的结束时间,点击确认签到
- 老师可以添加学生,可以移除学生
- 创建班级的时候,用一个成员变量记录当前的时间,加入课程的时候
最近完成了:
- 教师双击课程页面中的课程,进入课堂活动页面。其中实现了发布随机选人。
接下来是实现:学生在课堂里面可以查看随机选人的结果
正则表达式:
http://t.csdn.cn/xgyrZ
JavaFX 中 ListView的用法:
//可以在Listview上显示1
ObservableList<String>list= FXCollections.observableArrayList();
list.add("1");
courseListView1.setItems(list);
courseListView1.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
其中的courseListview1是定义在fx :id处的
JavaFX给ListView添加点击事件:
//双击事件
courseListView1.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getButton().equals(MouseButton.PRIMARY)) {
State stage=new Stage;
stage.show();
}
}
}
});
雪花算法:
//新建一个类
public class SnowflakeIdGenerator {
private final long twepoch = 1288834974657L;
private final long workerIdBits = 5L;
private final long datacenterIdBits = 5L;
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private final long sequenceBits = 12L;
private final long workerIdShift = sequenceBits;
private final long datacenterIdShift = sequenceBits + workerIdBits;
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long lastTimestamp = -1L;
public SnowflakeIdGenerator(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) |
(datacenterId << datacenterIdShift) |
(workerId << workerIdShift) |
sequence;
}
private long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
}
在要获取id的地方,新建一个方法:
public static String createCode(){
SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
long id = idGenerator.nextId();
String username = String.format("%011d", id % 100000000000L);
return username;
}
双击事件:
courseListView1.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
//在里面写要进行的操作
//显示新页面
stage.setScene(new Scene());
stage.show();
}
});
警告及提示的使用:
新建一个类:
public class Warning {
public static void page(Alert.AlertType alertType,String title,String content)
{
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
}
调用时使用:
//警告
Warning.page(Alert.AlertType.ERROR,"Waring","User1 does not exist or email does not match");//传入的第一个是警告,第二个是窗口标题,第三个是提示信息
//提示
Warning.page(Alert.AlertType.CONFIRMATION,"Success","Created successfully");
//传入的第一个是提示,第二个是窗口标题,第三个是提示信息
发送邮箱:
先新建一个类,用来发送邮箱:
public class SendEmail {
public static void sendEmail(String sendAddress, String title, String contend, String copySendAddress) throws MessagingException {
Properties properties=new Properties();
//设置服务器名称
properties.setProperty("mail.host","smtp.qq.com");
//设置邮箱的传输协议
properties.setProperty("mail.transport.protocol","smtp");
//设置是否要验证服务器用户名和密码
properties.setProperty("mail.smtp.auth","true");
//1.创建客户端和邮箱服务器对象
Session session=Session.getInstance(properties);
//2.开启session的debug模式,方便查看发送的email的运行状态
session.setDebug(true);
//3.用过session得到transport传输对象
Transport transport=session.getTransport();
//4.使用用户名密码连接邮箱服务器
transport.connect("smtp.qq.com","2171204141@qq.com","quydlpssavwwdhhg");
//5.创建邮件
Message message=createSimpleMail(session,sendAddress,title,contend,copySendAddress);
//6.发送邮件
transport.sendMessage(message,message.getAllRecipients());
transport.close();
}
private static Message createSimpleMail(Session session, String sendAddress, String title, String content, String copySendAddress) throws MessagingException {
//创建邮件对象
MimeMessage message=new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("2171204141@qq.com"));
//指明邮件的收件人
message.setRecipients(Message.RecipientType.TO, String.valueOf(new InternetAddress(sendAddress)));
//邮件的标题
message.setSubject(title);
//邮件的内容
message.setContent(content,"text/html;charset=UTF-8");
//设置抄送人
message.setRecipients(Message.RecipientType.CC,InternetAddress.parse(copySendAddress));
return message;
}
public void main(String[] args) throws MessagingException {
sendEmail("2171204141@qq.com","验证码","con","2171204141@qq.com");
}
}
新建一个类用来获取随机验证码,并且调用上一个类中的方法:
public class ObtainCode {
public static String obtainCode(String emailField) throws MessagingException {
Random r=new Random();
int random = r.nextInt(1000000);
String strCode=String.format("%06d", random);
String str="验证码为:"+strCode+",此验证码三分钟内有效。";
System.out.println("发送邮箱前");
SendEmail.sendEmail(emailField,"学习通",str,emailField);
System.out.println(strCode);
return strCode;
}
}
使用时要传入接受者的邮箱:
String strCode= ObtainCode.obtainCode("*********");
序列化和反序列化:
在User类中加入要传输的变量,实现的功能是:通过序列化将客户端的变量一次传个服务端(也可以反过来,将服务端的信息(比如服务端读取数据课库中的信息)传给客户端)。
//序列化
Gson gson = new Gson();
user = gson.fromJson(responseParts[1], User.class);
System.out.println("反序列化后: " + user);
//反序列化
Gson gson = new Gson();
user = gson.fromJson(userJson, User1.class);//User1.class自动赋值
解析:将User类中的其中User是创建在客户端的一个类,User1是创建在服务端的一个类,这两段分别放在客户端和服务端。其中User类和User1类最好是一样的,当中都是成员变量和get、set方法,也可以将接收端的set方法删去,因为用不到。
隐藏的方法:
在客户端中,教师和学生的功能不相同,所以在学生端需要隐藏某些按键(比如我教的课,发布签到,作业等)。
public class MainInterfaceController implements Initializable{
@FXML
private Tab test;
@FXML
private TabPane test1;
public void initialize(URL location, ResourceBundle resourceBundle){
test1.getTabs().remove(test);
}
}
其中URL选择第一个类。