这是原代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.sql.*;
import com.borland.dbswing.*;
import java.util.*;
import java.awt.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ItemPrice extends JFrame implements ActionListener {
JPanel mainP = new JPanel();
JButton okBtn=new JButton("添加");
JButton cancelBtn=new JButton("取消");
JPanel p2=new JPanel();
JLabel cate=new JLabel("用水类别");
JTextField tfCate=new JTextField(10);
JLabel price=new JLabel("单 价");
JTextField tfPrice=new JTextField(10);
public ItemPrice()
{
Container cont=getContentPane();
cont.setLayout(new FlowLayout(FlowLayout.CENTER));
cont.add(new p1());
cont.add(mainP);
p2.setLayout(new GridLayout(3,2));
p2.add(cate);
p2.add(price);
p2.add(tfCate);
p2.add(tfPrice);
p2.add(okBtn);
p2.add(cancelBtn);
mainP.add(p2);
okBtn.addActionListener(this);
cancelBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==okBtn)
{
WaterDB db=new WaterDB();
String sql="insert into category(name,price) values(\'"+tfCate.getText()+"\',\'"+tfPrice.getText()+"\')";
db.executeUpdate(sql);
tfCate.setText("");
tfPrice.setText("");
}
else if(e.getSource()==cancelBtn)
{
tfCate.setText("");
tfPrice.setText("");
}
}
class p1 extends JPanel {
public p1() {
Vector cell;
Vector row = new Vector();
DefaultTableModel tableModel = new DefaultTableModel();
String[] tableHeads = {"用水类别", "单价"};
Vector tableHeadName = new Vector();
for (int i = 0; i <tableHeads.length; i++) {
tableHeadName.add(tableHeads[i]);
}
try {
WaterDB wdb = new WaterDB();
String sql = "select * from category";
ResultSet rs = wdb.executeQuery(sql);
while (rs.next()) {
cell = new Vector();
for (int i = 1; i <= tableHeads.length; i++) {
cell.add(rs.getString);
}
row.add(cell);
}
tableModel.setDataVector(row, tableHeadName);
JTable table = new JTable(tableModel);
table.setFont(new Font("Dialog", 1, 13));
table.setRowHeight(20);
table.getColumn( "用水类别" ).setMaxWidth(120 ) ;
table.getColumn( "用水类别" ).setResizable( false ) ;
table.getColumn( "单价" ).setMaxWidth(80 ) ;
table.getColumn( "单价" ).setResizable( false ) ;
table.setCursor(new Cursor(12));
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setCursor(new Cursor(12));
this.add(scrollPane);
this.setVisible(true);
}
catch (SQLException f) {}
}
}
public static void main(String[] args) {
ItemPrice itemPrice = new ItemPrice();
itemPrice.setSize(280, 300);
itemPrice.show();
}
}