📄 addproduct.java
字号:
package control.view.product;import control.view.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;import beans.Product;import control.dao.product.ProDAOImpl;public class AddProduct extends JDialog implements ActionListener{ private JButton jb_save=new JButton("保存"); private JButton jb_exit=new JButton("取消"); private JTextField tf_pName=new JTextField(); private JTextField tf_pType=new JTextField(); private JTextField tf_pClassID=new JTextField(); private JTextField tf_pUnit=new JTextField(); private JTextField tf_pPrice=new JTextField(); private JLabel la_pName=new JLabel("产品名称:"); private JLabel la_pType=new JLabel("产品类型:"); private JLabel la_pClassID=new JLabel("产品规格型号:"); private JLabel la_pUnit=new JLabel("产品单位:"); private JLabel la_pPrice=new JLabel("产品单价(必须数值):"); public void actionPerformed(ActionEvent e){ if(e.getSource() instanceof JButton){ JButton b=(JButton)e.getSource(); if(b == jb_save){ try{ if(saveProduct(getProduct())){ JOptionPane.showMessageDialog(this, new String("添加成功")); this.dispose(); ((JFrame)this.getParent()).dispose(); new ShowAllProducts("所有产品"); }else{ JOptionPane.showMessageDialog(this, new String("添加失败,可能产品已经存在")); } }catch(Exception ee){ System.out.println(ee.getMessage()); JOptionPane.showMessageDialog(this, new String("添加失败,请确认输入的数据正确")); } }else if(b == jb_exit){ if( (JOptionPane.showConfirmDialog(this, new String("是否真的退出?")))== JOptionPane.YES_OPTION) { this.dispose(); } } } } private Product getProduct() throws Exception{ Double pPrice=Double.parseDouble(tf_pPrice.getText()); if( pPrice.isNaN()){ JOptionPane.showMessageDialog(this, new String("请确认价格输入是数字")); } return new Product( tf_pName.getText(), tf_pType.getText(), tf_pClassID.getText(), tf_pUnit.getText(), Double.parseDouble(tf_pPrice.getText()) ); } public AddProduct(JFrame fra,String title,boolean model){ super(fra,title,model); setMid(500,400,this); this.setResizable(false); addComponent(); this.show(true); this.pack(); } private void addComponent(){ this.getContentPane().setLayout(null); la_pName.setBounds(100,20,110,30); la_pType.setBounds(100,70,110,30); la_pClassID.setBounds(100,120,110,30); la_pUnit.setBounds(100,170,110,30); la_pPrice.setBounds(80,220,150,30); tf_pName.setBounds(210,20,150,30); tf_pType.setBounds(210,70,150,30); tf_pClassID.setBounds(210,120,150,30); tf_pUnit.setBounds(210,170,100,30); tf_pPrice.setBounds(210,220,100,30); jb_save.setBounds(100,270,100,35); jb_exit.setBounds(220,270,100,35); this.add(la_pName); this.add(tf_pName); this.add(la_pType); this.add(tf_pType); this.add(la_pClassID); this.add(tf_pClassID); this.add(la_pUnit); this.add(tf_pUnit); this.add(la_pPrice); this.add(tf_pPrice); jb_save.addActionListener(this); jb_exit.addActionListener(this); this.add(jb_save); this.add(jb_exit); } private boolean saveProduct(Product pro){ ProDAOImpl pd=new ProDAOImpl(); int rows=pd.insert(pro); if(rows==1) return true; else return false; } public void setMid(int width,int height,java.awt.Window fra){ Dimension ds=Toolkit.getDefaultToolkit().getScreenSize(); int x=new Double( (ds.getWidth()-width)/2 ).intValue(); int y=new Double( (ds.getHeight()-height)/2 ).intValue(); fra.setBounds(x,y,width,height); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -