实验目的
(1)熟练进行GUI编程,包括布局管理、事件处理和常用容器和控件(2)联合异常类
(3)联合文件类
(4)联合集合类
实验内容:
新建Java项目,命名为guiprj.在项目中新建- -个名为symexample包,在该包内新建下面的各个类。设计主窗口, 假设类名为FrameMain,含有菜单栏(MenuBar, JMenu和JMenultem).菜单包括“注册”,“查询",“退出", 其中.
1.点击“注册"菜单, 弹出一个类名为FrameRegister窗口.
(1)请在该窗口用文本框,标签,下拉列表,列表,复选框,单选框和按钮等设计一个学生信息(学号,姓名,性别,城市,爱好等)注册界面
(2)在该界面中放置-个“新增”按钮,可以把用户设定的注册信息存到- -个集合类中(建议设定一个Student类,再新建- -个ArrayList alstudents对象,新增的学生信息先存放到alstudents中) ;
(3)再放置-一个“保存’按钮,点击该按钮,可以把用户此次输入的所有学生的注册信息(建议把alstudents里的所有学生-个个取出进行存放,不要直接把alstudents存放到文件)存储到guiprij 项目根目录下的student.xt文件中(先判断文件是否存在,如果存在,要以新内容可以添加到文件尾部的方式打开文件,即需要支持多次注册);
(4)以上功能实现可以选择适合的任意文件流进行数据的存储,注意必要的异常处理。
2.点击"查询"菜单, 弹出一个类名为FrameBrowse窗口,
(1)界面类似“注册"窗口。
(2)打开该窗口时,它自动读取student.txt里的内容,把里面的每个学生读取出来放到一个HashMap结构中,里面关键字key是学号,其它信息想办法存储到它的value项。默认把第一个学生的信息在界面中相应的控件里显示出来。
(3) 放置一个名为"next的按钮, 点击它,可以提取HashMap并在界面里展现下一个学生的信息,放置一个名为"previous"的按钮,点击它,可以提取和展现前一-个学生的信息。
3.点击"退出"菜单,点击后弹出"是否真要退出?"的对话框,点击否不退出,点击’'确定“退出整个系统。
代码如下:
package symexample;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.*;
class Student {
//学生类存放学号、姓名、性别、地址、爱好
String idnumber;
String name;
String sex;
String address;
String hobby;
public String getIdnumber() {
return idnumber;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getAddress() {
return address;
}
public String getHobby() {
return hobby;
}
public void setIdnumber(String idnumber) {
this.idnumber=idnumber;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex=sex;
}
public void setAddress(String address) {
this.address=address;
}
public void setHobby(String hobby) {
this.hobby=hobby;
}
//重写输出格式
@Override
public String toString() {
return idnumber + " " + name + " " + sex + " " + address+" +hobby"+"\n";
}
}
public class FrameMain extends JFrame {
public static void main(String args[]) {
FrameMain test=new FrameMain();
test.setVisible(true);
}
//初始窗口
public FrameMain() {
setTitle("初始窗口");
setBounds(100,100,210,75);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton b1=new JButton();
final JButton b2=new JButton();
final JButton b3=new JButton();
b1.setText("注册");
b2.setText("查询");
b3.setText("退出");
b1.addActionListener(new ButtonAction1());
b2.addActionListener(new ButtonAction2());
b3.addActionListener(new ButtonAction3());
final FlowLayout flowlayout=new FlowLayout();
flowlayout.setAlignment(FlowLayout.LEFT);
getContentPane().setLayout(flowlayout);
getContentPane().add(b1);
getContentPane().add(b2);
getContentPane().add(b3);
}
//点击注册按钮事件处理器
class ButtonAction1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
new FrameRegister();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
//点击查询按钮事件处理器
class ButtonAction2 implements ActionListener{
int num=1;
public void actionPerformed(ActionEvent j) {
//先将文件中内容读取存储到hashmap中
Map<String,String>treemap=new HashMap<String,String>();
try {
InputStreamReader f1=new InputStreamReader(new FileInputStream("D:\\Student.txt"));
BufferedReader b1=new BufferedReader(f1);
String aline1;
while((aline1=b1.readLine())!=null) {
String str1=new String(aline1);
String[] str=str1.split(" ", 2);
treemap.put(str[0], str[1]);
}
f1.close();
b1.close();
}catch(Exception e) {
e.printStackTrace();
}
Set<String>arg=treemap.keySet();
//建立数组存储信息方便读取
String[] temp=new String[arg.size()];
Iterator<String>it4=arg.iterator();
int flag=0;
while(it4.hasNext()) {
temp[flag]=it4.next();
flag++;
}
String n=temp[0];
String m=treemap.get(n);
String[] ac=m.split(" ");
//初始化界面
JFrame frame=new JFrame();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JLabel label4 = new JLabel();
JLabel label5 = new JLabel();
JTextField text1 = new JTextField();
JTextField text2 = new JTextField();
JTextField text3 = new JTextField();
JTextField text4 = new JTextField();
JTextField text5 = new JTextField();
Font font = new Font("宋体",Font.PLAIN,20);
JButton b1 = new JButton();
JButton b2 = new JButton();
frame.setBounds(100, 100, 350, 300);
frame.add(label1);
frame.setLayout(null);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(label5);
frame.add(text1);
frame.add(text2);
frame.add(text3);
frame.add(text4);
frame.add(text5);
frame.add(b1);
frame.add(b2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
label1.setText("学号:");
label1.setFont(font);
label1.setBounds(30, 15, 50, 20);
label2.setText("姓名:");
label2.setFont(font);
label2.setBounds(30,55, 50, 20);
label3.setText("性别");
label3.setFont(font);
label3.setBounds(30, 95, 50, 20);
label4.setText("地址:");
label4.setFont(font);
label4.setBounds(30, 135, 50, 20);
label5.setText("爱好:");
label5.setFont(font);
label5.setBounds(30, 175, 50, 20);
text1.setBounds(100, 10, 200, 30);
text1.setFont(font);
text2.setBounds(100, 50, 200, 30);
text2.setFont(font);
text3.setBounds(100, 90, 200, 30);
text3.setFont(font);
text4.setBounds(100, 130, 200, 30);
text4.setFont(font);
text5.setBounds(100, 170, 200, 30);
text5.setFont(font);
text1.setText(n);
text2.setText(ac[0]);
text3.setText(ac[1]);
text4.setText(ac[2]);
text5.setText(ac[3]);
b1.setText("previous");
b1.setBounds(50, 220, 120, 30);
b1.setFont(font);
b2.setText("next");
b2.setBounds(210, 220, 80, 30);
b2.setFont(font);
//点击privious按钮处理事件
b1.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
//当读到第一个学生无法继续privious时,重置指针到第一个位置
if(num<=0 || num>=arg.size()) {
num=arg.size()-1;
System.out.println("已经是第一个学生了!");
}
String n=temp[num];
String m=treemap.get(n);
String[] ac=m.split(" ");
text1.setText(n);
text2.setText(ac[0]);
text3.setText(ac[1]);
text4.setText(ac[2]);
text5.setText(ac[3]);
num--;
}
});
//点击next按钮处理事件
b2.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
//当读到第一个学生无法继续next时,重置指针到最后一个位置
if(num<=0 || num>=arg.size()) {
num=0;
System.out.println("已经是最后一个学生了!");
}
String n=temp[num];
String m=treemap.get(n);
String[] ac=m.split(" ");
text1.setText(n);
text2.setText(ac[0]);
text3.setText(ac[1]);
text4.setText(ac[2]);
text5.setText(ac[3]);
num++;
}
});
}
}
//点击退出按钮事件处理器
class ButtonAction3 implements ActionListener{
public void actionPerformed(ActionEvent w) {
int result = JOptionPane.showConfirmDialog(null, "是否真要退出?", "确认",JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if(result == JOptionPane.OK_OPTION)
System.exit(0);
}
}
//注册按钮界面的实现类
class FrameRegister extends JFrame{
public List<Student> allstudent= new ArrayList();
public JLabel label1 = new JLabel();
public JLabel label2 = new JLabel();
public JLabel label3 = new JLabel();
public JLabel label4 = new JLabel();
public JLabel label5 = new JLabel();
public JTextField text1 = new JTextField();
public JTextField text2 = new JTextField();
public JTextField text3 = new JTextField();
public JTextField text4 = new JTextField();
public JTextField text5 = new JTextField();
Font font = new Font("宋体",Font.PLAIN,20);
public JButton b1 = new JButton();
public JButton b2 = new JButton();
FrameRegister() throws IOException{
this.setBounds(100, 100, 350, 300);
set();
this.add(label1);
this.setLayout(null);
this.add(label2);
this.add(label3);
this.add(label4);
this.add(label5);
this.add(text1);
this.add(text2);
this.add(text3);
this.add(text4);
this.add(text5);
this.add(b1);
this.add(b2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//新增按钮事件处理
b1.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
String idnumber = text1.getText();
String name = text2.getText();
String sex= text3.getText();
String address=text4.getText();
String hobby=text5.getText();
Student student=new Student();
student.setIdnumber(idnumber);
student.setName(name);
student.setSex(sex);
student.setAddress(address);
student.setHobby(hobby);
allstudent.add(student);
}
});
//保存按钮事件处理
b2.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
String idnumber = text1.getText();
String name = text2.getText();
String sex= text3.getText();
String address=text4.getText();
String hobby=text5.getText();
String result=idnumber + " " + name + " " + sex + " " + address+" "+hobby+"\n";
File file=new File("D:\\student.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException k) {
k.printStackTrace();
}
}
try {
FileWriter fos=new FileWriter(file,true);
fos.write(result);
fos.close();
}catch (IOException m) {
m.printStackTrace();
}
}
});
}
//查询界面初始化
public void set() {
label1.setText("学号:");
label1.setFont(font);
label1.setBounds(30, 15, 50, 20);
label2.setText("姓名:");
label2.setFont(font);
label2.setBounds(30,55, 50, 20);
label3.setText("性别");
label3.setFont(font);
label3.setBounds(30, 95, 50, 20);
label4.setText("地址:");
label4.setFont(font);
label4.setBounds(30, 135, 50, 20);
label5.setText("爱好:");
label5.setFont(font);
label5.setBounds(30, 175, 50, 20);
text1.setBounds(100, 10, 200, 30);
text1.setFont(font);
text2.setBounds(100, 50, 200, 30);
text2.setFont(font);
text3.setBounds(100, 90, 200, 30);
text3.setFont(font);
text4.setBounds(100, 130, 200, 30);
text4.setFont(font);
text5.setBounds(100, 170, 200, 30);
text5.setFont(font);
b1.setText("新增");
b1.setBounds(60, 220, 80, 30);
b1.setFont(font);
b2.setText("保存");
b2.setBounds(200, 220, 80, 30);
b2.setFont(font);
}
}
}
运行结果示例:
其中,在读取已写好的文件时,点击next或是privious按钮有时会出现显示的不是目标行的情况,还望指正。