pass.setPreferredSize(size);
name.setPreferredSize(size);
surname.setPreferredSize(size);
email.setPreferredSize(size);
icq.setPreferredSize(size);
group.setPreferredSize(size);
llogin.setPreferredSize(size);
lpass.setPreferredSize(size);
lname.setPreferredSize(size);
lsurnm.setPreferredSize(size);
lemail.setPreferredSize(size);
licq.setPreferredSize(size);
lgroup.setPreferredSize(size);
cancel.setPreferredSize(size);
reg.setPreferredSize(bigsize);
reset.setPreferredSize(size);
flag = false;
//-------------------ACTIONLISTENER------------------------------------
reg.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
try{
FileWriter dtbase = new FileWriter(Consts.pdatabase, true);
dtbase.write(login.getText() + " " + pass.getText() + "\n" + 0 + "\n");
dtbase.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(Consts.pprofiles + login.getText()));
bw.write("Имя : " + name.getText() + "\nФамилия : " + surname.getText() + "\nE-Mail : " + email.getText() + "\nICQ : " + icq.getText() + "\nГруппа : " + group.getText());
flag = true;
bw.close();
dispose ();
} catch (FileNotFoundException ex) {
flag = false;
Logger.getLogger(KReg.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
flag = false;
Logger.getLogger(KReg.class.getName()).log(Level.SEVERE, null, ex);
}
if (flag){
JOptionPane.showMessageDialog(null, "Регистрация завершена");
}
else {
JOptionPane.showMessageDialog(null, "Ошибка регистрации");
}
}
});
reset.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
login.setText("");
pass.setText("");
name.setText("");
surname.setText("");
email.setText("@");
icq.setText("");
group.setText("");
}
});
cancel.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
dispose ();
}
});
//-------------------ADD-----------------------------------------------
this.add(llogin);
this.add(login);
this.add(lpass);
this.add(pass);
this.add(lname);
this.add(name);
this.add(lsurnm);
this.add(surname);
this.add(lemail);
this.add(email);
this.add(licq);
this.add(icq);
this.add(lgroup);
this.add(group);
this.add(reg);
this.add(reset);
this.add(cancel);
this.setResizable(false);
this.setVisible(true);
this.setSize(2 * size.width + 30, size.height * 10 + 60);
}
}
Рис. 3.2.2. Окно регистрации в системе
3. Класс главного окна программы
Задача данного класса – предоставление доступа к остальным функциям системы.
При разных уровнях доступа интерфейс главного окна изменяется, предоставляя разные функции.
Рис. 3.3.1. Алгоритм разделения прав в системе
Исходный код класса:
package KursPackage;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.Date;
import javax.swing.border.TitledBorder;
public class KMainWin extends JFrame implements Consts{
KMainWin f;
JButton starttest;
JButton readl;
JButton newt;
JButton newl;
JButton oplog;
JButton exit;
JButton logout;
JButton contr;
JButton time;
File newlec;
File newtest;
JLabel name;
JLabel surnm;
JLabel email;
JLabel icq;
JLabel group;
JPanel info;
JLabel mark;
JPanel button;
KMainWin (int who, final String login, final String password) throws FileNotFoundException, IOException {
super ("Main window");
f = this;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout ());
final BufferedWriter bd = new BufferedWriter(new FileWriter(Consts.log, true));
bd.write(new Date().getHours() + ":" + new Date ().getMinutes() + " " + new Date ().getDay() + "/" + new Date ().getMonth() + "/" + new Date ().getYear() + "\n");
bd.write(login + " loged in\n");
logout = new JButton ("Log out");
logout.setPreferredSize(size);
exit = new JButton ("Выход");
exit.setPreferredSize(size);
logout.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
try {
bd.write(new Date().getHours() + ":" + new Date ().getMinutes() + " " + new Date ().getDay() + "/" + new Date ().getMonth() + "/" + new Date ().getYear() + "\n");
bd.write(login + " logged out\n");
bd.close();
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
dispose();
new KLogin();
try {
bd.close();
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
exit.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
try {
bd.write(new Date().getHours() + ":" + new Date ().getMinutes() + " " + new Date ().getDay() + "/" + new Date ().getMonth() + "/" + new Date ().getYear() + "\n");
bd.write(login + " exited from program\n");
bd.close();
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
}
});
if (who != 0){
time = new JButton ("Время обучения");
time.setPreferredSize(bigsize);
newt = new JButton ("Новый тест");
newt.setPreferredSize(bigsize);
newl = new JButton ("Новая лекция");
newl.setPreferredSize(bigsize);
oplog = new JButton ("Открыть лог-файл");
oplog.setPreferredSize(bigsize);
contr = new JButton ("Оценки");
contr.setPreferredSize(bigsize);
//-----------------------------ActionListeners---------------------
time.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
JFrame slog = new JFrame ("Время");
JEditorPane je = new JEditorPane ("rtf;Content-Type = windows-1251", "<html><i><center>Подготовка к тестам</center>");
JScrollPane js = new JScrollPane (je);
slog.add(js);
je.setEditable(false);
BufferedReader rd;
try {
rd = new BufferedReader(new FileReader(Consts.time));
je.read(rd, "rtf;Content-Type = windows-1251");
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
slog.setSize (700, 400);
slog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
slog.setVisible (true);
slog.setSize(300, 500);
slog.setVisible(true);
}
});
newt.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser ();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.showDialog(null, "Открыть");
newtest = fc.getSelectedFile();
if (newtest!=null)
{
try {
transfer (curtest, prevtest);
transfer (newtest.getPath(), curtest);
bd.write(new Date().getHours() + ":" + new Date ().getMinutes() + " " + new Date ().getDay() + "/" + new Date ().getMonth() + "/" + new Date ().getYear() + "\n");
bd.write(login + " added new test\n");
} catch (FileNotFoundException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, newlec.getPath());
}
}
});
newl.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser ();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.showDialog(null, "Открыть");
newlec = fc.getSelectedFile();
if (newlec!=null){
try {
transfer (curlec, prevlec);
transfer (newlec.getPath(), curlec);
bd.write(new Date().getHours() + ":" + new Date ().getMinutes() + " " + new Date ().getDay() + "/" + new Date ().getMonth() + "/" + new Date ().getYear() + "\n");
bd.write(login + " added new lec\n");
} catch (FileNotFoundException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, newlec.getPath());
}
}
});
oplog.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
JFrame slog = new JFrame ("Log");
JEditorPane je = new JEditorPane ("rtf;Content-Type = windows-1251", "<html><i><center>Подготовка к тестам</center>");
JScrollPane js = new JScrollPane (je);
slog.add(js);
je.setEditable(false);
BufferedReader rd;
try {
rd = new BufferedReader(new FileReader(Consts.log));
je.read(rd, "rtf;Content-Type = windows-1251");
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
slog.setSize (700, 400);
slog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
slog.setVisible (true);
slog.setSize(300, 500);
slog.setVisible(true);
}
});
contr.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame ("Оценки");
JEditorPane je = new JEditorPane ("text/html;Content-Type = windows-1251", "");
JScrollPane js = new JScrollPane (je);
frame.add(js);
je.setEditable(false);
BufferedReader rd;
try {
rd = new BufferedReader(new FileReader(Consts.marks));
je.read(rd, "text/html;Content-Type = windows-1251");
} catch (IOException ey) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ey);
}
frame.setSize (200, 400);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible (true);
}
});
button = new JPanel ();
button.setLayout(new FlowLayout());
button.setPreferredSize(new Dimension (210, 180));
button.add (newl);
button.add (newt);
button.add (oplog);
button.add (contr);
button.add(time);
button.add (logout);
button.add (exit);
add(button);
setSize (470, 215);
info = new JPanel();
info.setPreferredSize(new Dimension (230, 170));
}
//who == 0
else {
starttest = new JButton ("Начать тестирование");
starttest.setPreferredSize(bigsize);
readl = new JButton ("Прочитать лекцию");
readl.setPreferredSize(bigsize);
starttest.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
try {
KTestWin n = new KTestWin(login, password, f);
bd.write(new Date().getHours() + ":" + new Date ().getMinutes() + " " + new Date ().getDay() + "/" + new Date ().getMonth() + "/" + new Date ().getYear() + "\n");
bd.write(login + " started test\n");
} catch (FileNotFoundException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
readl.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
try {
new KLec(curlec, "Подготовка к тестам", false, login);
bd.write(new Date().getHours() + ":" + new Date().getMinutes() + " " + new Date().getDay() + "/" + new Date().getMonth() + "/" + new Date().getYear() + " ");
bd.write(login + " began read lec\n");
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
button = new JPanel ();
button.setLayout(new FlowLayout());
button.setPreferredSize(new Dimension (210, 125));
button.add(starttest);
button.add(readl);
button.add(logout);
button.add(exit);
add(button);
setSize (470, 170);
info = new JPanel();
info.setPreferredSize(new Dimension (230, 120));
}
name = new JLabel ();
surnm = new JLabel ();
email = new JLabel ();
icq = new JLabel ();
group = new JLabel ();
mark = new JLabel();
try {
BufferedReader rd = new BufferedReader(new FileReader(Consts.pprofiles + login));
name.setText(rd.readLine());
name.setPreferredSize(size);
surnm.setText(rd.readLine());
surnm.setPreferredSize(size);
email.setText(rd.readLine());
email.setPreferredSize(size);
icq.setText(rd.readLine());
icq.setPreferredSize(size);
group.setText(rd.readLine());
group.setPreferredSize(bigsize);
rd.close ();
BufferedReader rdm = new BufferedReader(new FileReader(Consts.pprofiles + login + ".mrk"));
mark.setText("Оценка: " + rdm.readLine());
rdm.close();
mark.setPreferredSize(size);
}catch (IOException et) {
//BufferedWriter bw = new BufferedWriter(new FileWriter(Consts.pprofiles + login));
//bw.write("Имя : \nФамилия : \nE-Mail : \nICQ : \nГруппа : ");
//bw.close();
}
TitledBorder tb = new TitledBorder ("Info");
info.setLayout(new BoxLayout (info, BoxLayout.Y_AXIS));
info.add(name);
info.add(surnm);
info.add(email);
info.add(icq);
info.add(group);
info.add(mark);
info.setBorder(tb);
add (info);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setResizable(false);
//this.setIconImage(image);
setVisible (true);
}
boolean transfer (String from, String to){
boolean result = false;
try {
FileChannel ic = new FileInputStream(from).getChannel();
FileChannel oc = new FileOutputStream(to).getChannel();
ic.transferTo(0, ic.size(), oc);
ic.close();
oc.close();
result = true;
} catch (FileNotFoundException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
result = false;
} catch (IOException ex) {
Logger.getLogger(KMainWin.class.getName()).log(Level.SEVERE, null, ex);
result = false;
}
return result;
}
}
Рис.3.3.2. «Интерфейс для администрирования»
Рис.3.3.3. Интерфейс программы для пользователя
4. Класс проверки знаний пользователя
Задача этого класса – проведение тестирования пользователя, оценка знаний.