📄 show.java
字号:
package com.person;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.sql.*;
class Show implements TableModelListener {
static int hit = 0;// 记录点击"修改密码"的次数
static int ID;
static int row = 0;
JInternalFrame internalframe;
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JTable table = null;
MyTable mt = null;
JTableHeader header = null;
TableColumn column[] = new TableColumn[7];
JScrollPane scrollPane = null;
Show() {
hit++;
if (hit == 1) {
internalframe = new JInternalFrame("显示", true, true, false, true);
internalframe.setFrameIcon(new ImageIcon("images/show_title.gif"));
internalframe.setLocation(100, 100);
internalframe.setSize(620, 350);
internalframe.setVisible(true);
internalframe.setResizable(false);
internalframe.addInternalFrameListener(new WinLis());
final Container cp = internalframe.getContentPane();
cp.setLayout(new BorderLayout());
String JDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String conURL = "jdbc:odbc:TestDB";
try {
Class.forName(JDriver);
} catch (java.lang.ClassNotFoundException e) {
System.out.println("ForName:" + e.getMessage());
}
try {
Connection con = DriverManager.getConnection(conURL);
Statement s = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery("select * from person");
if (rs.last()) {
ID = rs.getRow();
rs.beforeFirst();
}
rs.close();
s.close();
con.close();
} catch (SQLException e) {
System.out.println("SQLException:" + e.getMessage());
}
mt = new MyTable();
mt.addTableModelListener(this);
table = new JTable(mt);
header = table.getTableHeader();// 得到表格的表头
header.setBackground(new Color(254, 240, 201));
header.setForeground(Color.MAGENTA);
header.setReorderingAllowed(false);// 设置表头不可拖动
table.setRowHeight(30);
table.setBackground(new Color(240, 255, 255));
table.setSelectionBackground(new Color(240, 248, 255));// 设置选中表格时的背景颜色
table.setSelectionForeground(Color.RED);// 设置选中表格时的前景色
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int i = 0; i < 7; i++)
column[i] = table.getColumnModel().getColumn(i);
column[0].setMinWidth(50);
column[1].setMinWidth(70);
column[2].setMinWidth(110);
column[3].setMinWidth(80);
column[4].setMinWidth(80);
column[5].setMinWidth(100);
column[6].setMinWidth(100);
scrollPane = new JScrollPane(table);
scrollPane
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(600, 290));
panel1.add(scrollPane);
panel1.setBorder(BorderFactory.createEtchedBorder());
panel.add(panel1);
cp.add(panel, BorderLayout.CENTER);
try {
Connection con = DriverManager.getConnection(conURL);
Statement s = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery("select * from person");
while (rs.next()) {
mt.mySetValueAt(new Integer(rs.getInt("id")), row, 0);
mt.mySetValueAt(new Integer(rs.getInt("sleep_time")), row,
1);
mt.mySetValueAt(new Integer(rs.getInt("amusement_time")),
row, 2);
mt
.mySetValueAt(new Integer(rs.getInt("work_time")),
row, 3);
mt.mySetValueAt(new String(rs.getString("food_name")), row,
4);
mt
.mySetValueAt(new ImageIcon(rs
.getString("food_picture")), row, 5);
mt.mySetValueAt(new String(rs.getString("season")), row, 6);
rs.updateRow();
row++;
}
rs.close();
s.close();
con.close();
} catch (SQLException e1) {
System.out.println("SQLException:" + e1.getMessage());
}
Person.desktopPane.add(internalframe);
try {
internalframe.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
System.out.println("PropertyVetoException:" + e.getMessage());
}
} else {
}
}
public void tableChanged(TableModelEvent e) {
}
class WinLis extends InternalFrameAdapter {
public void internalFrameClosed(InternalFrameEvent e) {
hit = 0;
row = 0;
}
}
}
@SuppressWarnings("serial")
class MyTable extends AbstractTableModel {
Object[][] p = new Object[Show.ID][7];
String[] n = { "id", "sleep_time", "amusement_time", "work_time",
"food_name", "food_picture", "season" };
public int getColumnCount() {
return n.length;
}
public int getRowCount() {
return p.length;
}
public String getColumnName(int col) {
return n[col];
}
public Object getValueAt(int row, int col) {
return p[row][col];
}
@SuppressWarnings("unchecked")
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public void setValueAt(Object value, int row, int col) {
p[row][col] = value;
fireTableCellUpdated(row, col);
}
public void mySetValueAt(Object value, int row, int col) {
p[row][col] = value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -