📄 mainform.java
字号:
package student;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JDesktopPane;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class MainForm {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="14,13"
private JDesktopPane jDesktopPane = null;
private JScrollPane jScrollPane = null;
private JButton jButtonAdd = null;
private JButton jButtonDel = null;
private JButton jButtonMod = null;
private JButton jButtonExit = null;
public static MyTable jTable = null;
private JRadioButton jRadioName = null;
private JRadioButton jRadioNum = null;
private JTextField jTextSearch = null;
private JButton jButtonSearch = null;
ButtonGroup group = new ButtonGroup();
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setTitle("学生信息管理");
jFrame.setSize(new Dimension(567, 218));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setLocation((screenSize.width-jFrame.getWidth())/2,(screenSize.height-jFrame.getHeight())/2);
jFrame.setContentPane(getJDesktopPane());
}
return jFrame;
}
/**
* This method initializes jDesktopPane
*
* @return javax.swing.JDesktopPane
*/
private JDesktopPane getJDesktopPane() {
if (jDesktopPane == null) {
jDesktopPane = new JDesktopPane();
jDesktopPane.add(getJButtonAdd(), null);
jDesktopPane.add(getJButtonDel(), null);
jDesktopPane.add(getJButtonMod(), null);
jDesktopPane.add(getJButtonExit(), null);
jDesktopPane.add(getJButtonSearch(), null);
jDesktopPane.add(getJScrollPane(), null);
jDesktopPane.add(getJRadioName(), null);
jDesktopPane.add(getJRadioNum(), null);
jDesktopPane.add(getJTextSearch(), null);
}
return jDesktopPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButtonAdd() {
if (jButtonAdd == null) {
jButtonAdd = new JButton();
jButtonAdd.setBounds(new Rectangle(460, 10, 80, 22));
jButtonAdd.setText("添加");
jButtonAdd.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
Addnew();
}
private void Addnew() {
AddForm Add = new AddForm();
Add.getJFrame().setVisible(true);
}
});
}
return jButtonAdd;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButtonDel() {
if (jButtonDel == null) {
jButtonDel = new JButton();
jButtonDel.setBounds(new Rectangle(460, 57, 80, 22));
jButtonDel.setText("删除");
jButtonDel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
try {
Del();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
private void Del() throws Exception {
int Mark =jTable.getSelectedRow();
if(Mark==-1)
JOptionPane.showMessageDialog(new JDialog(),"请先选择一条记录!");
else
{
String str = (String) jTable.getValueAt(Mark, 1);
String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String Str="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Info";
Connection con=null;
Statement stmt=null;
Class.forName(DBDriver);
con=DriverManager.getConnection(Str);
stmt=con.createStatement();
stmt.executeUpdate("Delete From Info where Num='"+str+"'");
stmt.close();
con.close();
JOptionPane.showMessageDialog(new JDialog(),"删除成功!");
Reflesh();
}
}
});
}
return jButtonDel;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButtonMod() {
if (jButtonMod == null) {
jButtonMod = new JButton();
jButtonMod.setBounds(new Rectangle(460, 105, 80, 22));
jButtonMod.setText("修改");
jButtonMod.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
int Mark =jTable.getSelectedRow();
if(Mark==-1)
JOptionPane.showMessageDialog(new JDialog(),"请先选择一条记录!");
else
{
Mod();
}
}
private void Mod() {
ModForm Mod = new ModForm();
Mod.getJFrame().setVisible(true);
}
});
}
return jButtonMod;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButtonExit() {
if (jButtonExit == null) {
jButtonExit = new JButton();
jButtonExit.setBounds(new Rectangle(460, 155, 80, 22));
jButtonExit.setText("退出");
jButtonExit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
System.exit(0);
}
});
}
return jButtonExit;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(0, 0, 441, 150));
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private MyTable getJTable() {
if (jTable == null) {
jTable = new MyTable();
jTable.setDefaultRenderer(Object.class, new MyRenderer());
}
return jTable;
}
static void Reflesh() throws Exception
{
String[] Title = {"姓名","学号","英语","高数","计算机"};
DefaultTableModel model = new DefaultTableModel(null,Title);
String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String Str="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Info";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName(DBDriver);
con=DriverManager.getConnection(Str);
stmt=con.createStatement();
rs=stmt.executeQuery("Select * From Info");
while(rs.next())
{
Object rowData[] = {rs.getString("Name"),rs.getString("Num"),rs.getString("Fexam"),rs.getString("Sexam"),rs.getString("Texam")};
model.addRow(rowData);
}
stmt.close();
con.close();
jTable.setModel(model);
jTable.getColumnModel().getColumn(1).setPreferredWidth(150);
for(int i=0;i<5;i++)
jTable.getColumnModel().getColumn(i).setResizable(false);
jTable.getSelectionModel().setSelectionMode(0);
jTable.getTableHeader().setReorderingAllowed(false);
}
/**
* This method initializes jRadioButton
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioName() {
if (jRadioName == null) {
jRadioName = new JRadioButton();
jRadioName.setBounds(new Rectangle(5, 155, 90, 21));
jRadioName.setText("按姓名查询");
group.add(jRadioName);
}
return jRadioName;
}
/**
* This method initializes jRadioButton1
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioNum() {
if (jRadioNum == null) {
jRadioNum = new JRadioButton();
jRadioNum.setBounds(new Rectangle(100, 155, 90, 21));
jRadioNum.setText("按学号查询");
group.add(jRadioNum);
jRadioNum.setSelected(true);
}
return jRadioNum;
}
/**
* This method initializes jTextSearch
*
* @return javax.swing.JTextField
*/
private JTextField getJTextSearch() {
if (jTextSearch == null) {
jTextSearch = new JTextField();
jTextSearch.setBounds(new Rectangle(198, 155, 151, 22));
}
return jTextSearch;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButtonSearch() {
if (jButtonSearch == null) {
jButtonSearch = new JButton();
jButtonSearch.setBounds(new Rectangle(357, 155, 80, 22));
jButtonSearch.setText("查询");
jButtonSearch.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
Search();
}
private void Search() {
int Mark = -1;
if(group.getSelection() == jRadioName.getModel()){
for(int i=0;i<jTable.getRowCount();i++)
if(jTable.getValueAt(i, 0).equals(jTextSearch.getText()))
Mark = i;
}
else
{
for(int i=0;i<jTable.getRowCount();i++)
if(jTable.getValueAt(i, 1).equals(jTextSearch.getText()))
Mark = i;
}
if(Mark==-1)
JOptionPane.showMessageDialog(new JDialog(),"无此记录!");
else
{
jTable.setRowSelectionInterval(Mark, Mark);
Rectangle rect = jTable.getCellRect(Mark, 0, true);
jTable.scrollRectToVisible(rect);
}
}
});
}
return jButtonSearch;
}
class MyRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public MyRenderer(){
super();
this.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
}
}
class MyTable extends JTable
{
public boolean isCellEditable(final int row,final int column)
{
return false;
}
}
public static void main(String[] args) throws Exception {
MainForm MF;
MF = new MainForm();
MF.getJFrame().setVisible(true);
Reflesh();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -