回 帖 发 新 帖 刷新版面

主题:[原创]二手交易系统,怎么添加一个功能按钮

是一个二手交易系统,源代码如下,怎么样新增一个"商品查询"的按钮,可以查询商品的信息啊~~
import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class Ex10_2 implements ActionListener {
        JFrame f=null ;
        public Ex10_2(){
            f=new JFrame("二手交易系统");
            Container contentPane=f.getContentPane();
            JPanel buttonPanel = new JPanel();
            
            JButton b=new JButton("商品登记");
            b.addActionListener(this);
            buttonPanel.add(b);

                        b=new JButton("商品查询");
            b.addActionListener(this);
            buttonPanel.add(b);
            
            b=new JButton("退出系统");
            b.addActionListener(this);
            buttonPanel.add(b);
            
            buttonPanel.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createLineBorder(Color.blue,2),"二手交易系统",
            TitledBorder.CENTER,TitledBorder.TOP));
            
            contentPane.add(buttonPanel,BorderLayout.CENTER);
            
            JMenuBar mBar=new JMenuBar();
            JMenu  selection=new JMenu("选项");
            JMenuItem  regist=new JMenuItem("商品登记");
            JMenuItem  sum=new JMenuItem("统计");
            selection.add(regist);
            selection.add(sum);
            
            JMenu  sys=new JMenu("系统");
            JMenuItem  exit=new JMenuItem("退出系统");
            sys.add(exit);
            
            mBar.add(selection);
            mBar.add(sys);
            
            f.setJMenuBar(mBar);
            regist.addActionListener(this);
            sum.addActionListener(this);
            exit.addActionListener(this);
            
            f.pack();
            f.setVisible(true);
            
            f.addWindowListener( new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                        System.exit(0);
                    }
                
                });
            
            
        }
        public void actionPerformed(ActionEvent e){
                String cmd=e.getActionCommand();
                if(cmd.equals("商品登记")){
                        new RegistSystem(f);
                    }else if(cmd.equals("退出系统")){
                        System.exit(0);
                    }else if(cmd.equals("统计")){
                        try{
                                //System.out.println("test by xiabing");
                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                Connection con=DriverManager.getConnection("jdbc:odbc:PIMS","test","1234");
                                Statement stmt=con.createStatement();
                                ResultSet rs=stmt.executeQuery("select * from Person");
                                int i=0;
                                while(rs.next()) i=i+1;
                                JOptionPane.showMessageDialog(f,"共有"+i+"件商品");
                                stmt.close();
                                con.close();
                        }catch (Exception ex){}
                    }
            }
        
        public static void main(String args[]){
                new Ex10_2();
            }
        
    }
    
    class RegistSystem implements ActionListener {
            JDialog Dialog;
            JTextField tF1=new JTextField();
            JTextField tF2=new JTextField();
            JTextField tF3=new JTextField();
            JTextField tF4=new JTextField();
            JTextField tF5=new JTextField();
            JTextField tF6=new JTextField();
            RegistSystem(JFrame f){
                Dialog = new JDialog(f,"商品登记",true);
                Container dialogPane=Dialog.getContentPane();
                dialogPane.setLayout(new GridLayout(7,2));
                
                dialogPane.add(new JLabel("商品编号:",SwingConstants.CENTER));
                dialogPane.add(tF1);
                dialogPane.add(new JLabel("商品名称:",SwingConstants.CENTER));
                dialogPane.add(tF2);
                dialogPane.add(new JLabel("商品类别:",SwingConstants.CENTER));
                dialogPane.add(tF3);
                dialogPane.add(new JLabel("价格:",SwingConstants.CENTER));
                dialogPane.add(tF4);
                dialogPane.add(new JLabel("联系方式:",SwingConstants.CENTER));
                dialogPane.add(tF5);
                dialogPane.add(new JLabel("备注:",SwingConstants.CENTER));
                dialogPane.add(tF6);
                JButton b1=new JButton("确定");
                dialogPane.add(b1);
                JButton b2=new JButton("取消");
                dialogPane.add(b2);
                b1.addActionListener(this);
                b2.addActionListener(this);
                //Dialog.setBounds(200,150,400,130);
                Dialog.setBounds(400,450,400,430);
                Dialog.show();
                
                
                }
            public void actionPerformed(ActionEvent e){
                    String cmd=e.getActionCommand();
                    if(cmd.equals("确定")){
                            try{
                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                Connection con=DriverManager.getConnection("jdbc:odbc:PIMS","test","1234");
                                Statement stmt=con.createStatement();
                                int ID=Integer.parseInt(tF1.getText());
                                String name=tF2.getText();
                                int DepID=Integer.parseInt(tF3.getText());
                                String Occupation=tF4.getText();
                                int salary=Integer.parseInt(tF5.getText());
                                int EduID=Integer.parseInt(tF5.getText());
                                String SQLOrder="Insert Into Person Values ("+ID+",'"+name+"',"+DepID+",'"+Occupation+"',"+salary+","+EduID+")";
                                stmt.executeUpdate(SQLOrder);
                                stmt.close();
                                con.close();
                            }catch(Exception em){}
                        }else if (cmd.equals("取消")){
                                Dialog.dispose();
                            }
                }
        }

回复列表 (共1个回复)

沙发

你现在的代码应该不会有问题了
,不过如果出了问题 ,就要检查你的按钮的监听器,有没写对....
actionPerformed()里

我来回复

您尚未登录,请登录后再回复。点此登录或注册