触发来自不同类的java事件

时间:2018-02-02 03:21:43

标签: java swing

所以我有这个JFrame窗口,它有一个打开不同JFrame的按钮,第二个窗口的目标是在第一个窗口中单击其ok按钮表刷新,但我很难解决问题大约一个小时的搜索后没有找到任何答案(我知道的不是很多,但是时间紧迫了)。所以这是我的代码,我希望你们帮助我。

第一个窗口的代码:

// the event that i want to trigger
public void refreshTable() {
        try {
            ResultSet rs = new dbcon().getResultSet("select * from users", new Vector<Object>());   
            Vector<Vector<Object>> v = new Vector<Vector<Object>>();
            while(rs.next()) {
                Vector<Object> element = new Vector<Object>();
                element.add(rs.getInt("id"));
                element.add(rs.getString("name"));
                element.add(rs.getString("uname"));
                element.add(rs.getString("pwd"));
                element.add(rs.getString("date"));
                element.add(rs.getInt("admin"));
                v.add(element);
            }
            DefaultTableModel md = (DefaultTableModel) table.getModel();
            md.setRowCount(0);
            for(int i=v.size()-1;i>=0;i--) {
                md.addRow(v.elementAt(i));
            }
        }catch(Exception exp) {
            exp.getLocalizedMessage();
        }
    }

  //the event where the second window is opened

  else if(e.getSource() == mod) {
            int id = (int) table.getModel().getValueAt(table.getSelectedRow(),0);
            String user = (String) table.getModel().getValueAt(table.getSelectedRow(),2);
            String pass = (String) table.getModel().getValueAt(table.getSelectedRow(),3);
            new Edits().changeUser(user, pass, id);
            this.refreshTable();
        }

这是第二个窗口的代码:

//the frame of the second window
public void changeUser(String user,String pass, int id) {
        //init frame
        this.setTitle("Change User Login");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame.setDefaultLookAndFeelDecorated(false);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/6-this.getSize().width/2, dim.height/16-this.getSize().height/2);
        this.setSize(400, 290);
        this.setResizable(false);
        this.setVisible(true);
        //change sizes
        Dimension labels = new Dimension(150,25);
        Dimension inputs = new Dimension(200,25);
        Dimension btn = new Dimension(80,35);
        Dimension btn1 = new Dimension(120,35);
        //labels size
        currentUser.setSize(labels);
        currentUser.setMaximumSize(labels);
        currentUser.setMinimumSize(labels);
        currentUser.setPreferredSize(labels);
        changeUser.setSize(labels);
        changeUser.setMaximumSize(labels);
        changeUser.setMinimumSize(labels);
        changeUser.setPreferredSize(labels);
        currentPass.setSize(labels);
        currentPass.setMaximumSize(labels);
        currentPass.setMinimumSize(labels);
        currentPass.setPreferredSize(labels);
        changePass.setSize(labels);
        changePass.setMaximumSize(labels);
        changePass.setMinimumSize(labels);
        changePass.setPreferredSize(labels);
        //inputs size
        oldUser.setSize(inputs);
        oldUser.setMaximumSize(inputs);
        oldUser.setMinimumSize(inputs);
        oldUser.setPreferredSize(inputs);
        newUser.setSize(inputs);
        newUser.setMaximumSize(inputs);
        newUser.setMinimumSize(inputs);
        newUser.setPreferredSize(inputs);
        oldPass.setSize(inputs);
        oldPass.setMaximumSize(inputs);
        oldPass.setMinimumSize(inputs);
        oldPass.setPreferredSize(inputs);
        newPass.setSize(inputs);
        newPass.setMaximumSize(inputs);
        newPass.setMinimumSize(inputs);
        newPass.setPreferredSize(inputs);
        //button sizes
        okUser.setSize(btn1);
        okUser.setMaximumSize(btn1);
        okUser.setMinimumSize(btn1);
        okUser.setPreferredSize(btn1);
        closeUser.setSize(btn);
        closeUser.setMaximumSize(btn);
        closeUser.setMinimumSize(btn);
        closeUser.setPreferredSize(btn);
        //set layout
        this.setLayout(new FlowLayout(FlowLayout.CENTER,10,15));
        //add components
        this.add(currentUser);
        this.add(oldUser);
        this.add(changeUser);
        this.add(newUser);
        this.add(currentPass);
        this.add(oldPass);
        this.add(changePass);
        this.add(newPass);
        this.add(okUser);
        this.add(closeUser);
        //button & input configuration
        oldUser.setEditable(false);
        oldUser.setText(user);
        oldPass.setEditable(false);
        oldPass.setText(pass);
        Id.setValue(id);
        newUser.setText(user);
        newPass.setText(pass);
        okUser.addActionListener(this);
        closeUser.addActionListener(this);
    }

  //the action of the second window

  else if(e.getSource() == okUser) {
            String user = oldUser.getText();
            String pass = oldPass.getText();
            int id = (int) Id.getValue();
            if(!newUser.getText().equals("") && !newPass.getText().equals("")) {
                try {
                    Vector<Object> params = new Vector<Object>();
                    params.add(newUser.getText());
                    params.add(newPass.getText());
                    params.add(id);
                    new dbcon().setUpdates("update users set uname = ?, pwd = ? where id = ?", params);
                    JOptionPane.showMessageDialog(null, "User Informations Updated");
                    this.dispose();
                }catch(Exception exp) {
                    exp.printStackTrace();
                }
            }else {
                JOptionPane.showMessageDialog(null, "Please make sure to fill out all the fields!");
            }
        }

我希望你们能够帮助我,因为这是5个小时内到期的学校项目。谢谢大家的时间:D

0 个答案:

没有答案