📄 thirdcatalogpane.java
字号:
package file2;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ThirdCatalogPane extends JPanel implements ActionListener{
//声明数据库连接组件
private DBConnection con=null;
private JLabel tip=null;
private JLabel name=null;
private JLabel cost=null;
private JLabel price=null;
private JLabel limit=null;
private JLabel belongsTo=null;
private JTextField nameTextField=null;
private JTextField costTextField=null;
private JTextField priceTextField=null;
private JTextField limitTextField=null;
private JComboBox comboBox=null;
private JButton insert=null;
private JButton reset=null;
private JPanel tipPane=null;
private JPanel labelAndTextFieldPane=null;
private JPanel belongsToPane=null;
//private JScrollPane scrollPane=null;
private JPanel buttonsPane=null;
public ThirdCatalogPane(){
try{
tip=new JLabel("请您在下面录入具体商品信息:");
name=new JLabel("名称:");
cost=new JLabel("成本:");
price=new JLabel("卖出价:");
limit=new JLabel("最小库存量:");
belongsTo=new JLabel("所属类别:");
nameTextField=new JTextField(10);
costTextField=new JTextField(10);
priceTextField=new JTextField(10);
limitTextField=new JTextField(10);
insert=new JButton("插入");
reset=new JButton("重置");
tipPane=new JPanel();
tipPane.setLayout(new FlowLayout(FlowLayout.LEFT));
tipPane.add(tip);
//声明装载所有静态文本和文本框的面版
labelAndTextFieldPane=new JPanel();
labelAndTextFieldPane.setLayout(new GridLayout(4,2));
labelAndTextFieldPane.add(name);
labelAndTextFieldPane.add(nameTextField);
labelAndTextFieldPane.add(cost);
labelAndTextFieldPane.add(costTextField);
labelAndTextFieldPane.add(price);
labelAndTextFieldPane.add(priceTextField);
labelAndTextFieldPane.add(limit);
labelAndTextFieldPane.add(limitTextField);
belongsToPane=new JPanel();
belongsToPane.setLayout(new FlowLayout(FlowLayout.LEFT));
belongsToPane.add(belongsTo);
con=new DBConnection();
String[] dataOfList=new String[100];
dataOfList[0]="请选择商品明细类:";
String querySql="select* from Second_Catalog";
ResultSet set=con.executeSelect(querySql);
int count=1;
while(set.next()){
String name=set.getString(3);
dataOfList[count++]=name;
}
comboBox=new JComboBox(dataOfList);
comboBox.setSelectedIndex(0);
belongsToPane.add(comboBox);
//scrollPane=new JScrollPane(list);
buttonsPane=new JPanel();
buttonsPane.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
buttonsPane.add(insert);
buttonsPane.add(reset);
this.setLayout(new GridLayout(4,1));
this.add(tipPane);
this.add(labelAndTextFieldPane);
this.add(belongsToPane);
//this.add(scrollPane);
this.add(buttonsPane);
insert.addActionListener(this);
reset.addActionListener(this);
}catch(SQLException sql){
JOptionPane.showMessageDialog(null, "发生SQL错误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
nameTextField.setText("");
costTextField.setText("");
priceTextField.setText("");
limitTextField.setText("");
comboBox.setSelectedIndex(0);
return;
}
if(e.getSource()==insert){
String name=nameTextField.getText().trim();
String cost=costTextField.getText().trim();
String price=priceTextField.getText().trim();
String limit=limitTextField.getText().trim();
String belongsto=(String)comboBox.getSelectedItem();
if(name.equals("")||cost.equals("")||price.equals("")||limit.equals("")||belongsto.equals("请选择商品明细类:")){
JOptionPane.showMessageDialog(null, "所有文本框都不能为空!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}
try{
float costInt=(new Float(cost)).floatValue();
float priceInt=(new Float(price)).floatValue();
int limitInt=(new Integer(limit)).intValue();
String querySql="select* from Third_Catalog where Third_name='"+name+"'";
ResultSet set=con.executeSelect(querySql);
if(set.next()){
JOptionPane.showMessageDialog(null, "该商品名称已经存在!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}
set=null;
String queryID="select Second_ID from Second_Catalog where Second_name='"+belongsto+"'";
set=con.executeSelect(queryID);
if(set.next()){
int ID=set.getInt("Second_ID");
String insertSql="insert into Third_Catalog values("+ID+",'"+name+"',"+
cost+","+price+","+"0,"+limitInt+")";
con.executeDML(insertSql);
JOptionPane.showMessageDialog(null, "插入成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
nameTextField.setText("");
costTextField.setText("");
priceTextField.setText("");
limitTextField.setText("");
comboBox.setSelectedIndex(0);
return;
}
return;
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "成本,卖出价,最小库存容量等都必须为数字,且最小库存量为整数!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}catch(SQLException sql){
JOptionPane.showMessageDialog(null, "发生SQL错误!", "警告", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -