?? 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 + -