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

📄 customerdlg.java

📁 一个简单的JAVA超市系统
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import java.sql.*;
//import java.util.Vector;

class CustomerList extends JDialog implements ActionListener
{
	ConnectionSQL conSql;
	CustomerDlg CusDlg;        //客户添加
	CustomerDel CusDel;        //客户删除	
	JButton add, del, all, print;
	JTable table;
	DefaultTableModel defaultModel = null;
	String numS="", nameS="", addressS="", manS="", photoS="";
	CustomerList(Frame f, String s, boolean b)
	{
		super(f, s, b);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		Container con = getContentPane();
		con.setLayout(new BorderLayout());
		CusDlg = new CustomerDlg(null, "客户管理", true);
		CusDlg.setLocationRelativeTo(null);           //居中显示
		
		
		JPanel pNorth = new JPanel(),
			   pCenter = new JPanel();
		add = new JButton("添加");
		del = new JButton("删除");
		all = new JButton("全部");
		print = new JButton("打印");
		add.addActionListener(this);
		del.addActionListener(this);
		all.addActionListener(this);
		print.addActionListener(this);
		
		pNorth.add(all);
		pNorth.add(print);
		pNorth.add(add);
		pNorth.add(del);
		
		Object a[][] = new Object[0][0];
		Object n[] = {"编号","客户名称","地址","联系人","联系电话"};    //表格的列名
		defaultModel = new DefaultTableModel(a,n);
		table = new JTable(defaultModel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		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())
				{
					del.setEnabled(true);
					int selectedRow = lsm.getMinSelectionIndex();
					numS = (String)defaultModel.getValueAt(selectedRow, 0);
					nameS = (String)defaultModel.getValueAt(selectedRow, 1);
					addressS = (String)defaultModel.getValueAt(selectedRow, 2);
					manS = (String)defaultModel.getValueAt(selectedRow, 3);
					photoS = (String)defaultModel.getValueAt(selectedRow, 4);
					//dataS = (String)defaultModel.getValueAt(selectedRow, 5);
				}
			}
		});
		
		pCenter.add(new JScrollPane(table));
		
		con.add(pNorth, BorderLayout.NORTH);
		con.add(pCenter, BorderLayout.CENTER);
		setSize(600, 300);
	}
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根		
		if(e.getSource()==all)
		{
			int row = defaultModel.getRowCount()-1;  //清空表格***********
			while(row>=0)
			{
				defaultModel.removeRow(row);
				defaultModel.setRowCount(row);
				row--;
			}   //*********************************
			
			Object o[] = {"", "", "", "", ""};
			try {
				conSql = new ConnectionSQL();
				conSql.sql = conSql.con.createStatement();
				conSql.rs = conSql.sql.executeQuery("select * from CURTOMER");
				while(conSql.rs.next())
				{
					o[0] = conSql.rs.getString(1);
					o[1] = conSql.rs.getString(2);
					o[2] = conSql.rs.getString(3);
					o[3] = conSql.rs.getString(4);
					o[4] = conSql.rs.getString(5);
					defaultModel.addRow(o);    //增加一行
				}
				conSql.con.close();
			}
			catch(SQLException ee)
			{
				
			}
		}
		else if(e.getSource()==print)
		{
			JOptionPane.showMessageDialog(this, "无打印机", "提示", JOptionPane.WARNING_MESSAGE);
		}
		else if(e.getSource()==add)
		{
			CusDlg.setVisible(true);
		}
		else if(e.getSource()==del)
		{
			CusDel = new CustomerDel(null, "删除客户", true);
			
			CusDel.numText.setText(numS);			CusDel.nameText.setText(nameS);
			CusDel.addressText.setText(addressS);	CusDel.manText.setText(manS);
			CusDel.photoText.setText(photoS);
			CusDel.setLocationRelativeTo(null);
			CusDel.setVisible(true);
		}
	}
}

class CustomerDel extends JDialog implements ActionListener
{
	ConnectionSQL conSql;
	JButton addbut;
	JTextField numText, nameText, addressText, manText, photoText;
	CustomerDel(Frame f, String s, boolean b)
	{
		super(f, s, b);
		setSize(400, 300);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);   //关闭
		
		Box box1, box2, box3, box;
		box1 = Box.createVerticalBox();
		box2 = Box.createVerticalBox();
		box3 = Box.createHorizontalBox();
		box = Box.createVerticalBox();
		
		addbut = new JButton("添加");
		numText = new JTextField(8);
		nameText = new JTextField(12);
		addressText = new JTextField(30);
		manText = new JTextField(10);
		photoText = new JTextField(10);
		nameText.setEditable(false);
		addressText.setEditable(false);
		manText.setEditable(false);
		photoText.setEditable(false);
		
		box1.add(new JLabel("客户编号:"));	
		box1.add(Box.createVerticalStrut(18));   //垂直支撑
		box1.add(new JLabel("客户名称:"));
		box1.add(Box.createVerticalStrut(18));
		box1.add(new JLabel("客户地址:"));
		box1.add(Box.createVerticalStrut(20));
		box1.add(new JLabel("联系人:"));
		box1.add(Box.createVerticalStrut(21));
		box1.add(new JLabel("联系电话:"));
		
		box2.add(Box.createVerticalStrut(10));
		box2.add(numText);			
		box2.add(Box.createVerticalStrut(10));
		box2.add(nameText);		
		box2.add(Box.createVerticalStrut(10));
		box2.add(addressText);		
		box2.add(Box.createVerticalStrut(10));
		box2.add(manText);		
		box2.add(Box.createVerticalStrut(10));
		box2.add(photoText);
		box2.add(Box.createVerticalStrut(10));
		
		box3.add(Box.createHorizontalStrut(40));  //水平支撑
		box3.add(box1);	
		box3.add(Box.createHorizontalStrut(10));	
		box3.add(box2);
		box3.add(Box.createHorizontalStrut(60));
		box.add(box3);
		box.add(addbut);
		box.add(Box.createVerticalStrut(40));
		addbut.addActionListener(this);
		
		add(box);
	}

	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根
		try {
			conSql = new ConnectionSQL();
			conSql.sql = conSql.con.createStatement();
			String strSql = String.format("select * from CURTOMER where 编号 = '%s'", numText.getText());
			conSql.rs = conSql.sql.executeQuery(strSql);
			while(conSql.rs.next())
			{
				nameText.setText(conSql.rs.getString(2));
				addressText.setText(conSql.rs.getString(3));
				manText.setText(conSql.rs.getString(4));
				photoText.setText(conSql.rs.getString(5));
			}
			conSql.con.close();
		}
		catch(SQLException ee)	{}
	}
	
}

public class CustomerDlg extends JDialog implements ActionListener
{
	ConnectionSQL conSql;
	JButton addbut;
	JTextField numText, nameText, addressText, manText, photoText;
	//DefaultTableModel defaultModel = null;
	//JTable table;
	CustomerDlg(Frame f, String s, boolean b)
	{
		super(f, s, b);
		setSize(400, 300);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);   //关闭
		
		Box box1, box2, box3, box;
		box1 = Box.createVerticalBox();
		box2 = Box.createVerticalBox();
		box3 = Box.createHorizontalBox();
		box = Box.createVerticalBox();
		
		addbut = new JButton("添加");
		numText = new JTextField(8);
		nameText = new JTextField(12);
		addressText = new JTextField(30);
		manText = new JTextField(10);
		photoText = new JTextField(10);
		
		box1.add(new JLabel("客户编号:"));		box1.add(Box.createVerticalStrut(18));   //垂直支撑
		box1.add(new JLabel("客户名称:"));		box1.add(Box.createVerticalStrut(18));
		box1.add(new JLabel("客户地址:"));		box1.add(Box.createVerticalStrut(20));
		box1.add(new JLabel("联系人:"));		box1.add(Box.createVerticalStrut(21));
		box1.add(new JLabel("联系电话:"));
		
		box2.add(Box.createVerticalStrut(10));
		box2.add(numText);				box2.add(Box.createVerticalStrut(10));
		box2.add(nameText);				box2.add(Box.createVerticalStrut(10));
		box2.add(addressText);			box2.add(Box.createVerticalStrut(10));
		box2.add(manText);				box2.add(Box.createVerticalStrut(10));
		box2.add(photoText);			box2.add(Box.createVerticalStrut(10));
		
		box3.add(Box.createHorizontalStrut(40));  //水平支撑
		box3.add(box1);			box3.add(Box.createHorizontalStrut(10));	
		box3.add(box2);			box3.add(Box.createHorizontalStrut(60));
		box.add(box3);
		box.add(addbut);		box.add(Box.createVerticalStrut(40));
		addbut.addActionListener(this);
		
		add(box);
		
	}

	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根		
		if(numText.getText().trim().equals("") || nameText.getText().trim().equals("") ||
				addressText.getText().trim().equals("") || manText.getText().trim().equals("") ||
				photoText.getText().trim().equals(""))
		{
			JOptionPane.showMessageDialog(this, "有空值!", "提示", JOptionPane.WARNING_MESSAGE);
		}
		else
		{
			int message = JOptionPane.showConfirmDialog(this, "确定添加吗?", "确认对话框", JOptionPane.YES_NO_OPTION);
		
			if(message == JOptionPane.YES_OPTION)
			{	
				String stringSql =	String.format("insert into CURTOMER values('%s', '%s', '%s', '%s', '%s')", 
						numText.getText(), nameText.getText(), addressText.getText(), manText.getText(), photoText.getText());
				try {
					conSql = new ConnectionSQL();
					conSql.sql = conSql.con.createStatement();
					conSql.sql.executeUpdate(stringSql);
				}
				catch(SQLException ee)	{}
			}
		}
	}
}

⌨️ 快捷键说明

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