⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 publishtypemanage.java

📁 简单的书店管理系统,是课设期间教师指导完成的,希望会对大家有帮助!
💻 JAVA
字号:
package book;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class PublishTypeManage extends Mb {
	private JLabel icon,number,name;
	
	private JTextField tnumber,tname;
	
    private ButtonIcon add,del,xiugai,close,save,quxiao;
	
    private JScrollPane scrool;
	
	private ArrayList List = new ArrayList();
	
	private Table table;
	
	private PreparedStatement pstmt = null;
	
	private NowEditableModel dtm;

	PublishTypeManage(String s, int i, int j) throws SQLException {
		super(s, i, j);
        con.setLayout(null);	
		shezhi();
		add();
	}
     private void shezhi() throws SQLException{
    		MainDAO dao = new MainDAO();
    		dtm = (NowEditableModel) dao.getTableModel("select * from chubanshe");
    		table = new Table(dtm);
    		scrool = new JScrollPane(table);
    		scrool.setBounds(15, 60, 365, 200);
    		table.setWidth();
    		
    		ListSelectionModel rowSM = table.getSelectionModel();
    		rowSM.addListSelectionListener(new ListSelectionListener() {
    			public void valueChanged(ListSelectionEvent e) {
    				if (e.getValueIsAdjusting())
    					return;
    				ListSelectionModel lsm = (ListSelectionModel) e.getSource();
    				if (lsm.isSelectionEmpty()) {
    					System.out.println("No rows are selected.");
    				} else {
    					int selectedRow = lsm.getMinSelectionIndex();
    					List = table.getSelectRow(lsm.getMinSelectionIndex(), table
    							.getColumnCount());
    					if (!(List.size() == 0)) {
    						setSelect();
    					}
    					System.out.println("Row " + selectedRow
    							+ " is now selected.");
    				}
    			}
    			
    		});
    		
    		setLabel();
    		setTextField();
    		setButton();
     }
     
     private void setButton() {
    	 xiugai=new ButtonIcon("修改");
    	 xiugai.setBounds(15,310,70,25);
    	 xiugai.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				if(judge()){
						MainDAO dao;
						try {
							dao = new MainDAO();
							dao.update("update chubanshe set 出版社名称='"+tname.getText()+"' where ISBN="+tnumber.getText());
							table.Renovate("select * from chubanshe");
							JOptionPane.showMessageDialog(table, "修改成功!!");
						} catch (SQLException e) {
							e.printStackTrace();
						}
						
				}
				
			}
    	 });
    	 
    	 add=new ButtonIcon("添加");
    	 add.setBounds(115,310,70,25);
    	 add.addActionListener(new ActionListener(){

 			public void actionPerformed(ActionEvent arg0) {
 				tnumber.setText("");
 				tname.setText("");
 				xiugai.setEnabled(false);
 				del.setVisible(false);
 				save.setVisible(true);
 				add.setEnabled(false);
 				quxiao.setVisible(true);
 				close.setVisible(false);
 				tnumber.setEnabled(true);
	
 			}
     		 
     	 });
    	 
    	 save=new ButtonIcon("保存");
    	 save.setVisible(false);
    	 save.setBounds(215,310,70,25);
    	 save.addActionListener(new ActionListener(){

 			public void actionPerformed(ActionEvent arg0) {
 				
 				try {
 					MainDAO dao=new MainDAO();
					if(addJudge()&&judge()){
						String str = "insert into chubanshe values(?,?)";
						pstmt = dao.getPreparedStatement(str);
						pstmt.setInt(1, Integer.parseInt(tnumber.getText()));
						pstmt.setString(2, tname.getText());
						pstmt.executeUpdate();
						pstmt.close();
						dao.close();
						table.Renovate("select * from chubanshe");
						xiugai.setEnabled(true);
						add.setEnabled(true);
						del.setVisible(true);
						save.setVisible(false);
						quxiao.setVisible(false);
						close.setVisible(true);
						tnumber.setEnabled(false);
						JOptionPane.showMessageDialog(table, "添加新类别成功!!!!!!!!");	
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
 				
 			}
     	 });
    	 
    	 del=new ButtonIcon("删除");
    	 del.setBounds(215,310,70,25);
    	 del.addActionListener(new ActionListener(){

 			public void actionPerformed(ActionEvent arg0) {
 				
 				if (tnumber.getText().equals("")) {
 					JOptionPane.showMessageDialog(table, "没选中任何用户!!!!");
 				} else {
 					int n = JOptionPane.showConfirmDialog(table, "确认删除此条记录??删除后将不可以恢复!!",
 							"退出", JOptionPane.YES_NO_OPTION);
 					if (n == 0) {
 						MainDAO dao;
 						try {
 							dao = new MainDAO();
 							String str;
 							str = "delete from chubanshe where ISBN="
 								+ tnumber.getText();
 							dao.del(str);
 							table.Renovate("select * from chubanshe");
 						} catch (SQLException e1) {
 							e1.printStackTrace();
 						}
 					}
 				}	
 			}	 
     	 });
    	 
    	 close=new ButtonIcon("关闭");
    	 close.setBounds(315,310,70,25);
    	 close.addActionListener(new ActionListener(){

 			public void actionPerformed(ActionEvent arg0) {
 				
 				close();
 			}
     		 

     	 });
    	 
    	 quxiao=new ButtonIcon("取消");
    	 quxiao.setBounds(315,310,70,25);
    	 quxiao.setVisible(false);
    	 quxiao.addActionListener(new ActionListener(){

 			public void actionPerformed(ActionEvent arg0) {
 				xiugai.setEnabled(true);
 				del.setVisible(true);
 				save.setVisible(false);
 				add.setEnabled(true);
 				quxiao.setVisible(false);
 				close.setVisible(true);
 				tnumber.setEnabled(false);
 				table.getSelectionModel().setSelectionInterval(0, 0);
 			}
     	 });
		
	}
	private void setTextField() {
		tnumber=new JTextField();
		tnumber.setBounds(80,270,110,18);
		tnumber.setEnabled(false);
		tname=new JTextField();
		tname.setBounds(270,270,110,18);
	}
	private void setLabel() {
		
		icon=new JLabel(new ImageIcon("./data/image/heard/chubanshe.jpg"));
		icon.setBounds(0,5,400,50);
		
	     number=new JLabel("ISBN:");
	     number.setBounds(15,270,80,18);
	     name=new JLabel("出版社名:");	
	     name.setBounds(205,270,80,18);
	}
	
	private void setSelect(){
		tnumber.setText(List.get(0).toString());
		tname.setText(List.get(1).toString());
	}
	
	private boolean judge(){
		boolean boo=true;
		if(tnumber.getText().equals("")||tname.getText().equals("")){
			JOptionPane.showMessageDialog(table, "ISBN或出版社名不能为空!!");
			boo=false;
		}
		return boo;
	}
	
	private boolean addJudge() throws SQLException {
		boolean boo=true;
		MainDAO dao=new MainDAO();
		char b[] = tnumber.getText().toCharArray();
		for (int i = 0; i < b.length; i++) {
			if (!(Character.isDigit(b[i]))) {
				boo = false;
				JOptionPane.showMessageDialog(table, "ISBN只能是数字!!");
			}
			else
				if(dao.Judge("select ISBN from chubanshe", tnumber.getText(),
			"ISBN")){
					boo=false;
					JOptionPane.showMessageDialog(table, "此ISBN已存在,请重新输入!!");
				}
		}    
		return boo;
	}
	
	private void add(){
		con.add(icon);
    	 con.add(scrool);
    	 con.add(number);
    	 con.add(tnumber);
    	 con.add(name);
    	 con.add(tname);
    	 
    	 con.add(xiugai);
    	 con.add(add);
    	 con.add(save);
    	 con.add(del);
    	 con.add(close);
         con.add(quxiao);
    	 
    	 con.validate();
    	 table.getSelectionModel().setSelectionInterval(0, 0);
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -