1. 本周学习总结
1.1 思维导图:Java图形界面总结
2.书面作业
1. GUI中的事件处理
1.1 写出事件处理模型中最重要的几个关键词。
监听,事件源,事件,注册。
1.2 任意编写事件处理相关代码(程序中需要出现你的学号与姓名)并截图,以证明你理解了事件处理模型。
package GUI;
import javax.swing.*;
import java.awt.event.*;
/**
*
* @author 周文华
*
*/
public class SimpleGui1B implements ActionListener {
JButton button;
public void go() {
JFrame frame=new JFrame();
button=new JButton("click me");
button.addActionListener(this);
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
button.setText("I've been clicked");
}
public static void main(String[] args) {
SimpleGui1B gui=new SimpleGui1B();
gui.go();
}
}
2. 大作业:使用图形界面改善你的购物车,考核点如下:
2.1 给出项目的分工表格与git地址
|成员 |负责任务|git地址
|- |- |- |-
|周文华 | 商品搜索 | 购物车1.2
|肖文婷| 购物车展示 |购物车1.2
2.2 尝试使用图形界面改写(截图你的程序,图形界面中需要出现你的姓名)
2.3 将基于控制台界面的程序改写为基于GUI的程序这个过程碰到什么问题?总结:以后编写方法时,应该遵循一些什么原则?
使用了netbeans之后感觉轻松了不少,编写的一般过程是:1.向事件源注册2.重写注册方法,方法与用户的行为对应。3.尽量使一些方法更具有适用性。
2.4 给出几个人在码云上同一项目的提交记录截图。如果某个人无提交记录,视为未完成。
3.其他:如果大作业实在搞不定,请完成实验任务书上的题目1、题目2、题目3。
3.1 运行界面截图,需要出现你的学号与姓名。
题目1:
题目2:
3.2 截图在NetBeans中出现的关键代码,不得将代码复制到其他编辑器。
关键代码:
题目二:
private void loginButtonActionPerformed(ActionEvent e) {
String id = idField.getText();
String password = passwordField.getText();
String[] result = isExist(id);
if (result == null) {
JOptionPane.showMessageDialog(null, "该帐号不存在");
} else {
if (result[0].equals(id) && result[1].equals(password)) {
JOptionPane.showMessageDialog(null, "登录成功");
} else {
JOptionPane.showMessageDialog(null, "登录失败,帐号密码不匹配");
}
}
}
private void registerButtonActionPerformed(ActionEvent e) {
String id = idField.getText();
String password = passwordField.getText();
String[] result = isExist(id);
if (result == null) {
writeFile(id, password);
JOptionPane.showMessageDialog(null, "注册成功");
} else {
JOptionPane.showMessageDialog(null, "注册失败,该帐号已存在");
}
}
private String[] isExist(String id) {
String[] strList = new String[2];
try {
File file = new File("user.txt");
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
String line = in.nextLine();
strList = line.split(" ");
if (strList[0].equals(id)) {
return strList;
}
}
in.close();
} catch (FileNotFoundException ex) {
}
return null;
}
private void writeFile(String id, String password) {
String matching = id + " " + password;
try {
File file = new File("user.txt");
PrintWriter out = new PrintWriter(file);
out.println(matching);
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
3.统计本周完成的代码量
需要将每周的代码统计情况融合到一张表中。
周次 | 总代码量 | 新增文件代码量 | 总文件数 | 新增文件数 |
---|---|---|---|---|
1 | 665 | 20 | 20 | 20 |
2 | 1705 | 23 | 23 | 23 |
3 | 1834 | 30 | 30 | 30 |
4 | 1073 | 1073 | 17 | 17 |
5 | 1073 | 1073 | 17 | 17 |
6 | 2207 | 1134 | 44 | 27 |
7 | 3292 | 1085 | 59 | 15 |
8 | 3505 | 213 | 62 | 3 |