📄 bookinfojpanel.java
字号:
package view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
import persistence.bookInfoDAOImp;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
import java.util.List;
import java.util.Iterator;
import java.util.Vector;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import bean.bookInfo;
import bean.popedom;
import tools.*;
import view.*;
import view.popedom.popedomJpanel;
public class bookInfoJpanel extends JPanel implements ActionListener,PropertyChangeListener {
private JPanel panelOne; //面板第一部分,用于显示、修改权限具体信息
private JButton buttonOutport; //数据导出
private JButton buttonBack; //退出模块
private JScrollPane spTable; //定义一个又滚动条的面板,存放表组件
private String[] field = {"序号","ISBN","索书号","书名","作者","出版日期","出版社","类型","状态","副本","页数","价格","描述"}; //定义表的显示字段名
private Object[][] ob = null; //初始化表记录为空
private static DefaultTableModel mod; //使用默认的表模式
private JTable table; //定义一个表组件
private CachedRowSet crs;
private GridBagConstraints cons;
private bookInfoDAOImp bidaoimp;
private String fileName;
private String tableName;
public bookInfoJpanel() {
setOne();
setTable();
setThisJPanel();
bidaoimp = new bookInfoDAOImp();
setTableRows(bookInfoDAOImp.getAllBookRows());
}
public void setOne() { //第一部分面板初始化
panelOne = new JPanel();
FlowLayout fl = new FlowLayout();
panelOne.setLayout(fl);
buttonOutport = new JButton("导出");
buttonOutport.addActionListener(this);
buttonBack = new JButton("返回");
buttonBack.addActionListener(this);
panelOne.add(buttonOutport);
panelOne.add(buttonBack);
}
public void setThisJPanel() { //权限面板初始化
this.setSize(300,300);
GridBagLayout gridBag = new GridBagLayout();
this.setLayout(gridBag);
cons = new GridBagConstraints(0,0,30,30,0,0,GridBagConstraints.EAST,GridBagConstraints.HORIZONTAL,new Insets(2,2,2,2),0,0);
gridBag.setConstraints(spTable,cons);
cons = new GridBagConstraints(0,50,1,30,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(2,2,2,2),0,0);
gridBag.setConstraints(panelOne,cons);
this.add(spTable);
this.add(panelOne);
}
public void actionPerformed(ActionEvent e) { //事件处理
if(e.getSource() == buttonBack) {
this.setVisible(false);
} else if(e.getSource() == buttonOutport) {
setSaveFile();
}
}
public void setSaveFile() {
JFileChooser jfc = new JFileChooser("C:/");
jfc.showSaveDialog(null);
fileName = jfc.getSelectedFile().getName();
int result = jfc.SAVE_DIALOG;
if(result == JFileChooser.SAVE_DIALOG) {
System.out.println(fileName);
tableName = "图书信息表";
//try {
//crs = new CachedRowSetImpl();
bidaoimp.getBookRs(fileName,tableName);
//if(crs.next()) {
// System.out.println(crs.getString(2));
//}
//System.out.println("aasuccess");
//CreateXL.resultSetToExcel(bidaoimp.getBookRs(),fileName,tableName);
//System.out.println("success");
//} catch(Exception ex) {
// System.out.println("Error:(saveFile)" + ex.toString());
//}
}
}
public void setTable() { //表面板初始化方法
mod = new DefaultTableModel(ob,field);
table = new JTable(mod);
table.setPreferredScrollableViewportSize(new Dimension(400,350));
table.setEnabled(false);
spTable = new JScrollPane(table);
}
public void setTableRows(List list) { //设置表记录的方法
for(int i = 0;i < mod.getRowCount();i++) { //移除表中原有的记录
mod.removeRow(i);
}
Iterator iter = list.iterator();
int i = 0;
while(iter.hasNext()) { //从List中取得字段,并组织成表Table能接受的格式
Vector<String> vs = new Vector<String>();
bookInfo bi = (bookInfo)iter.next();
vs.add(String.valueOf(++i));
vs.add(bi.getISBN());
vs.add(bi.getBookIndex());
vs.add(bi.getName());
vs.add(bi.getAuthor());
vs.add(bi.getPublishTime());
vs.add(bi.getPc().getName());
vs.add(bi.getBt().getName());
vs.add(bi.getBs().getName());
vs.add(String.valueOf(bi.getEctype()));
vs.add(String.valueOf(bi.getPageNum()));
vs.add(String.valueOf(bi.getPrice()));
vs.add(bi.getDescription());
mod.addRow(vs); //向表中添加新的记录
}
}
public void propertyChange(PropertyChangeEvent e) { //MVC模式,模型数据更新后通知视图更新数据
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame f = new JFrame("测试用");
f.getContentPane().add(new bookInfoJpanel());
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -