📄 mainframe.java
字号:
/*
* MainFrame.java
*
* Created on 2008年1月14日, 上午8:47
*/
/**
*
* @author liufan
*/
import java.sql.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.util.Vector;
import javax.swing.table.*;
import javax.swing.ListSelectionModel;
import javax.swing.event.*;
public class MainFrame extends javax.swing.JFrame {
/** Creates new form MainFrame */
public MainFrame() {
initComponents();
init();
}
private void showAll() {
flag = true;
try {
con = DriverManager.getConnection("jdbc:odbc:book", "", "");
}catch(SQLException e) {
JOptionPane.showMessageDialog(null, "未找到数据库");
System.exit(1);
}
try {
while(jTableBook.getRowCount() > 0)
tableModel.removeRow(0);
sql = con.createStatement();
rs = sql.executeQuery("select * from book");
while(rs.next()) {
Vector row = new Vector();
String name = rs.getString(1); row.add(name);
String author = rs.getString(2); row.add(author);
String press = rs.getString(3); row.add(press);
Date date = rs.getDate(4); row.add(date);
String ISBN = rs.getString(5); row.add(ISBN);
tableModel.addRow(row);
}
con.close();
}catch(SQLException e) {
flag = false;
JOptionPane.showMessageDialog(null, e);
}
flag = false;
}
private void init() {
jTableBook.setModel(tableModel);
jTableBook.setSelectionMode(0);
ListSelectionModel model = jTableBook.getSelectionModel();
model.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//事件处理代码
if(flag) return;
String name, author, press, ISBN;
Date date;
int row = jTableBook.getSelectedRow();
name = (String)tableModel.getValueAt(row, 0); jEditorName.setText(name);
author = (String)tableModel.getValueAt(row, 1); jEditorAuthor.setText(author);
press = (String)tableModel.getValueAt(row, 2); jEditorPress.setText(press);
date = (Date)tableModel.getValueAt(row, 3); jEditorDate.setText(date.toString());
ISBN = (String)tableModel.getValueAt(row, 4); jEditorISBN.setText(ISBN);
}
});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "未找到驱动程序");
}
showAll();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTableBook = new javax.swing.JTable();
jButtonSearch = new javax.swing.JButton();
jButtonAdd = new javax.swing.JButton();
jButtonDel = new javax.swing.JButton();
jButtonModify = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jEditorName = new javax.swing.JEditorPane();
jScrollPane3 = new javax.swing.JScrollPane();
jEditorAuthor = new javax.swing.JEditorPane();
jScrollPane4 = new javax.swing.JScrollPane();
jEditorPress = new javax.swing.JEditorPane();
jScrollPane5 = new javax.swing.JScrollPane();
jEditorDateLower = new javax.swing.JEditorPane();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jScrollPane6 = new javax.swing.JScrollPane();
jEditorDateUpper = new javax.swing.JEditorPane();
jLabel7 = new javax.swing.JLabel();
jScrollPane7 = new javax.swing.JScrollPane();
jEditorDate = new javax.swing.JEditorPane();
jScrollPane8 = new javax.swing.JScrollPane();
jEditorISBN = new javax.swing.JEditorPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("\u4e2a\u4eba\u56fe\u4e66\u7ba1\u7406");
setResizable(false);
jScrollPane1.setFont(new java.awt.Font("宋体", 0, 14));
jTableBook.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTableBook);
jButtonSearch.setText("\u67e5\u8be2");
jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSearchActionPerformed(evt);
}
});
jButtonAdd.setText("\u589e\u52a0");
jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAddActionPerformed(evt);
}
});
jButtonDel.setText("\u5220\u9664");
jButtonDel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDelActionPerformed(evt);
}
});
jButtonModify.setText("\u4fee\u6539");
jButtonModify.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonModifyActionPerformed(evt);
}
});
jLabel1.setText("\u4e66\u540d\uff1a");
jLabel2.setText("\u4f5c\u8005\uff1a");
jLabel3.setText("\u51fa\u7248\u793e\uff1a");
jLabel4.setText("\u51fa\u7248\u65e5\u671f >=");
jScrollPane2.setViewportView(jEditorName);
jScrollPane3.setViewportView(jEditorAuthor);
jScrollPane4.setViewportView(jEditorPress);
jScrollPane5.setViewportView(jEditorDateLower);
jLabel5.setText("\u51fa\u7248\u65e5\u671f <=");
jLabel6.setText("ISBN\uff1a");
jButton1.setText("\u6e05\u7a7a");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jScrollPane6.setViewportView(jEditorDateUpper);
jLabel7.setText("\u51fa\u7248\u65e5\u671f\uff1a");
jScrollPane7.setViewportView(jEditorDate);
jScrollPane8.setViewportView(jEditorISBN);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 636, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(jButtonSearch)
.addGap(74, 74, 74)
.addComponent(jButtonAdd)
.addGap(87, 87, 87)
.addComponent(jButtonDel)
.addGap(83, 83, 83)
.addComponent(jButtonModify)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 191, Short.MAX_VALUE)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -