?? 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("產品規(guī)格型號:"); private JLabel la_pUnit=new JLabel("產品單位:"); private JLabel la_pPrice=new JLabel("產品單價(必須數(shù)值):"); 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("添加失敗,可能產品已經(jīng)存在")); } }catch(Exception ee){ System.out.println(ee.getMessage()); JOptionPane.showMessageDialog(this, new String("添加失敗,請確認輸入的數(shù)據(jù)正確")); } }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("請確認價格輸入是數(shù)字")); } 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 + -