📄 editexam.java
字号:
/*
* 创建日期 2006-5-25
* 作者:褚廷军
* 项目名称:学生考试系统;
* 项目要求:毕业设计;
* 模块名称:服务器端试题编辑模块
* 模块功能:此模块完成对数据库试题文件的
* 增加(一次只可以增加一道题目)和删除功能(一次只可以删除一套题目)
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package tools;
import java.awt.*;
import java.awt.event.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class EditExam extends JFrame implements ActionListener {
JPanel p;
Container con;
private JLabel snameL;
private JTextField snameF;
private JLabel symbolL;
private JTextField symbolF;
private JLabel topicL;
private JTextArea topicF;
private JLabel optionL;
private JTextArea optionF;
private JLabel keyL;
private JTextField keyF;
private JLabel typeL;
private JTextField typeF;
private JButton addButton;
private PreparedStatement ps;
private JLabel label;
private JLabel deleteL;
private JTextField deleteF;
private JButton deleteButton;
private JLabel lab;
private JPanel p1;
private JPanel p2;
private JLabel tidL;
private JTextField tdiF;
private JTextField tidF;
public EditExam() {
super("更新试题");
Toolkit tool = getToolkit();
Image ima = tool.getImage("tubiao.jpg");
setIconImage(ima);
con = getContentPane();
setPane();
con.add(p1, BorderLayout.WEST);
con.add(p2, BorderLayout.EAST);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
this.setLocation(100, 100);
setSize(600, 500);
setVisible(true);
}
public void setPane() {
p1 = new JPanel();
p2 = new JPanel();
int shut = 30;
Font font = new Font("楷体_GB2312", Font.LAYOUT_RIGHT_TO_LEFT, 26);
label = new JLabel("添加试题");
label.setFont(font);
label.setForeground(Color.blue);
label
.setToolTipText("please input the details of exam you want to add");
Box snamebox = Box.createHorizontalBox();
snameL = new JLabel("科目");
snameF = new JTextField(5);
snamebox.add(snameL);
snamebox.add(Box.createHorizontalStrut(shut));
snamebox.add(snameF);
Box symbolbox = Box.createHorizontalBox();
symbolL = new JLabel("科目代号");
symbolF = new JTextField(5);
symbolbox.add(symbolL);
symbolbox.add(Box.createHorizontalStrut(5));
symbolbox.add(symbolF);
Box tidbox = Box.createHorizontalBox();
tidL = new JLabel("题号");
tidF = new JTextField(5);
symbolbox.add(tidL);
symbolbox.add(Box.createHorizontalStrut(5));
symbolbox.add(tidF);
Box topicbox = Box.createHorizontalBox();
topicL = new JLabel("题目");
topicF = new JTextArea(5, 10);
topicbox.add(topicL);
topicbox.add(Box.createHorizontalStrut(shut));
topicbox.add(new JScrollPane(topicF));
Box optionbox = Box.createHorizontalBox();
optionL = new JLabel("选项");
optionF = new JTextArea(5, 10);
optionF.setText("如果是填空题此处填写“填空题目”");
optionbox.add(optionL);
optionbox.add(Box.createHorizontalStrut(shut));
optionbox.add(new JScrollPane(optionF));
Box keybox = Box.createHorizontalBox();
keyL = new JLabel("答案");
keyF = new JTextField(10);
keybox.add(keyL);
keybox.add(Box.createHorizontalStrut(shut));
keybox.add(keyF);
Box typebox = Box.createHorizontalBox();
typeL = new JLabel("题目类型");
typeF = new JTextField(5);
typebox.add(typeL);
typebox.add(Box.createHorizontalStrut(5));
typebox.add(typeF);
addButton = new JButton("确定添加");
addButton.setToolTipText("add the test to DB");
addButton.addActionListener(this);
Box addBox = Box.createVerticalBox();
addBox.add(label);
addBox.add(snamebox);
addBox.add(Box.createVerticalStrut(5));
addBox.add(symbolbox);
addBox.add(Box.createVerticalStrut(5));
addBox.add(tidbox);
addBox.add(Box.createVerticalStrut(5));
addBox.add(topicbox);
addBox.add(Box.createVerticalStrut(5));
addBox.add(optionbox);
addBox.add(Box.createVerticalStrut(5));
addBox.add(keybox);
addBox.add(Box.createVerticalStrut(5));
addBox.add(typebox);
addBox.add(addButton);
addBox.add(Box.createVerticalStrut(5));
addBox.add(Box.createVerticalGlue());
deleteL = new JLabel("删除试题");
deleteL.setFont(font);
deleteL.setForeground(Color.blue);
deleteF = new JTextField(20);
lab = new JLabel("请输入你要删除的试题代号");
lab.setForeground(Color.red);
lab
.setToolTipText("please input the symbol of exam you want to delete^-^");
deleteButton = new JButton("确定删除");
deleteButton.setToolTipText("remove the test form DB");
deleteButton.addActionListener(this);
Box deletBox = Box.createVerticalBox();
deletBox.add(deleteL);
deletBox.add(Box.createVerticalStrut(30));
deletBox.add(lab);
deletBox.add(deleteF);
deletBox.add(Box.createVerticalStrut(30));
deletBox.add(deleteButton);
p1.add(addBox);
p1.setBorder(new TitledBorder(new EtchedBorder(), "添加试题"));
p2.add(deletBox);
p2.setBorder(new TitledBorder(new EtchedBorder(), "删除试题"));
}
// public static void main(String[] a) {
// JFrame.setDefaultLookAndFeelDecorated(true);
// new EditExam();
// }
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
String str1 = symbolF.getText().trim();
String str2 = topicF.getText().trim();
String str3 = optionF.getText().trim();
String str4 = keyF.getText().trim();
String str5 = snameF.getText().trim();
String str6 = tidF.getText().trim();
String str7 = typeF.getText().trim();
if (e.getSource() == addButton) {
ConnectBean cb = new ConnectBean();
if (!cb.openConnection()) {
System.out.println("连接数据失败");
System.exit(1);
return;
}
try {
ps = cb
.createPreparedStatement("insert into exam values(?,?,?,?,?,?,?)");
ps.setString(1, str1);
ps.setString(2, str2);
ps.setString(3, str3);
ps.setString(4, str4);
ps.setString(5, str5);
ps.setString(6, str6);
ps.setString(7, str7);
ps.executeUpdate();
cb.commit();
JOptionPane.showMessageDialog(this,"添加试题成功","成功提示",JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e1) {
JOptionPane.showMessageDialog(this,"添加试题失败","失败警告",JOptionPane.INFORMATION_MESSAGE);
e1.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
ConnectBean cb = new ConnectBean();
if (!cb.openConnection()) {
System.out.println("连接数据失败");
System.exit(1);
return;
}
try {
ps = cb.createPreparedStatement("delete from exam where tid="
+ deleteF.getText().trim());
ps.executeUpdate();
cb.commit();
JOptionPane.showMessageDialog(this,"删除试题成功","成功提示",JOptionPane.INFORMATION_MESSAGE);
// System.out.println("删除试题成功");
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
JOptionPane.showMessageDialog(this,"删除试题失败","失败警告",JOptionPane.INFORMATION_MESSAGE);
//System.out.println("删除试题失败");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -