import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import edu.njust.cs.CustomTableModel;
import edu.njust.cs.LayoutUtil;
import edu.njust.cs.SqlUtil;
import java.sql.*;
public class DVDMainApp extends JFrame {
JToolBar toolbar = new JToolBar("影碟基本信息");
JButton newDVD = new JButton("新影碟",new ImageIcon("image/new.gif"));
JButton modifyDVD = new JButton("修改影碟资料",new ImageIcon("image/modify.gif"));
JButton delDVD = new JButton("删除影碟",new ImageIcon("image/delete.gif"));
JButton saveDVD = new JButton("确定保存",new ImageIcon("image/save.gif"));
JButton queryDVD = new JButton("查询影碟",new ImageIcon("image/query.gif"));
JButton exit = new JButton("退出",new ImageIcon("image/exit.gif"));
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true); //拆分窗格
JPanel topPane = new JPanel();
JPanel bottomPane = new JPanel();
JLabel labTopic = new JLabel(" 影片名称 ");
JLabel labKinds = new JLabel(" 影片类型 ");
JLabel labDate= new JLabel(" 发行时间 ");
JLabel labPubArea = new JLabel(" 发行地 ");
JLabel labType = new JLabel(" 影碟类型 ");
JLabel labPrice = new JLabel(" 影碟价格 ");
JTextField tfTopic = new JTextField();
JComboBox cbKinds = new JComboBox();
JTextField tfDate = new JTextField();
JComboBox cbPubArea = new JComboBox();
JComboBox cbType = new JComboBox();
JTextField tfPrice = new JTextField();
JButton btnTimer = new JButton(new ImageIcon("image/timer.gif"));
String[] columnNames = {"影碟编码", "影片名称","影片类型","发行时间","发行地","影碟类型","零售价格"};
Class[] dataType=new Class[] {Long.class,String.class,String.class,Integer.class,String.class,String.class,Double.class};
JTable table;
CustomTableModel model;
Connection con = null;
int movieId = 0;
MovieData movieData = new MovieData();
public DVDMainApp()
{
init();
setTitle("影碟信息管理");
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,(int)d.getWidth(),(int)d.getHeight()-50);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/************************************
*init()用于做界面设计
***********************************/
public void init()
{
setLayout(new BorderLayout());
//---------设置工具条-------------
toolbar.add(newDVD);
toolbar.add(modifyDVD);
toolbar.add(delDVD);
toolbar.add(saveDVD);
toolbar.add(queryDVD);
toolbar.add(exit);
//定义上下拆分的拆分窗格
splitPane.setDividerLocation(0.5);
splitPane.add(topPane,JSplitPane.TOP);
splitPane.add(bottomPane,JSplitPane.BOTTOM);
add(toolbar,BorderLayout.NORTH);
add(splitPane,BorderLayout.CENTER);
//----------设置拆分窗格顶部的影碟信息编辑面板-----------------
tfDate.setDocument(new DigitDocument(DigitDocument.YEAR_FORMAT));
tfPrice.setDocument(new DigitDocument(DigitDocument.MONEY_FORMAT));
topPane.setLayout(new GridBagLayout());
Insets insets = new Insets(10,10,10,10);
LayoutUtil.add(topPane, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 0, 0, 1, 1,labTopic,insets);
LayoutUtil.add(topPane, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1, 0, 1, 0, 1, 1,tfTopic,insets);
LayoutUtil.add(topPane, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 2, 0, 1, 1,labKinds,insets);
LayoutUtil.add(topPane, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1, 0, 3, 0, 1, 1,cbKinds,insets);
LayoutUtil.add(topPane, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 4, 0, 1, 1,labDate,insets);
LayoutUtil.add(topPane, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST,1, 0, 5, 0, 1, 1,tfDate,insets);
LayoutUtil.add(topPane, GridBagConstraints.NONE, GridBagConstraints.WEST,0, 0, 6, 0, 1, 1,btnTimer,insets);
LayoutUtil.add(topPane, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 0, 1, 1, 1,labPubArea,insets);
LayoutUtil.add(topPane, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1, 0, 1, 1, 1, 1,cbPubArea,insets);
LayoutUtil.add(topPane, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 2, 1, 1, 1,labType,insets);
LayoutUtil.add(topPane, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1, 0, 3, 1, 1, 1,cbType,insets);
LayoutUtil.add(topPane, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 4, 1, 1, 1,labPrice,insets);
LayoutUtil.add(topPane, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST,1, 0, 5, 1, 1, 1,tfPrice,insets);
//----------设置拆分窗格底部的影碟信息浏览表格面板-----------------
bottomPane.setLayout(new BorderLayout());
model=new CustomTableModel(0,columnNames.length,columnNames,dataType);
//生成表格对象
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
bottomPane.add(scrollPane, BorderLayout.CENTER);
//从数据库中设置表格数据
try{
con = SqlUtil.acquireConnection(SqlUtil.loadProperty("db.cfg"));
SqlUtil.readDBToTable(con,"select * from 影碟信息表",model,dataType);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "数据提取失败,可能是数据库表不存在了","错误",JOptionPane.ERROR_MESSAGE);
}
initComboBox();
setInfoEditable(false);
//--------注册监听器对象-----------------
ButtonProcessor buttonProcessor = new ButtonProcessor();
newDVD.addActionListener(buttonProcessor);
modifyDVD.addActionListener(buttonProcessor);
delDVD.addActionListener(buttonProcessor);
saveDVD.addActionListener(buttonProcessor);
queryDVD.addActionListener(buttonProcessor);
exit.addActionListener(buttonProcessor);
btnTimer.setActionCommand("getTimer");
btnTimer.addActionListener(buttonProcessor);
}
/*
* 工具条按钮事件监听类
* 主要处理包括影碟信息的增、删、改、查等操作
*/
class ButtonProcessor implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("查询影碟"))
{
QueryDialog query = new QueryDialog(con,model,dataType);
}
else if(ae.getActionCommand().equals("新影碟"))
{
movieId = 0; //代表新影碟记录索引号
tfTopic.setText("");
cbKinds.setSelectedIndex(-1);
tfDate.setText("");
cbPubArea.setSelectedIndex(-1);
cbType.setSelectedIndex(-1);
tfPrice.setText("");
setInfoEditable(true);
}
else if(ae.getActionCommand().equals("修改影碟资料"))
{
int row = table.getSelectedRow();
if(row > 0)
{
movieId = Integer.valueOf(model.getValueAt(row, 0).toString());
tfTopic.setText(model.getValueAt(row, 1).toString());
cbKinds.setSelectedItem(model.getValueAt(row, 2).toString());
tfDate.setText(model.getValueAt(row, 3).toString());
cbPubArea.setSelectedItem(model.getValueAt(row, 4).toString());
cbType.setSelectedItem(model.getValueAt(row, 5).toString());
tfPrice.setText(model.getValueAt(row, 6).toString());
setInfoEditable(true);
}
else
{
JOptionPane.showMessageDialog(null, "请选择需要修改的影碟记录","错误",JOptionPane.ERROR_MESSAGE);
}
}
else if(ae.getActionCommand().equals("删除影碟"))
{
int movie_id = Integer.valueOf(model.getValueAt(table.getSelectedRow(), 0).toString());
if(movie_id <0)
{
JOptionPane.showConfirmDialog(null, "没有选择需要删除的记录,请选中记录并按下修改影碟信息工具栏选项?", "删除"