正在运行swing中声明的方法.Jframe类



我创建了两个swing.JFrame s。登录GUI和用户GUI。我想要的是,当它从登录gui切换到用户gui时,用户gui中有一个Jlabel,需要更改为("you're logged in as" + username);

我在userjframe源代码中尝试了这个代码。

`loggedInAsLable.setText("you're logged in as" + username);` 

在一个方法中,它在用户CCD_ 5的主方法中被调用。但由于某些原因它不起作用。

当Jframe变得可见时,我如何运行一些方法?

public class CustomerServiceOfficerUI extends javax.swing.JFrame {
private static Statement st;
ResultSet rs;
Connection con = null;
Login loginUI = new Login(); // gets current user Id
Employee cso = new CustomerServiceOfficer();  //creates new customer service officer object
/**
 * Creates new form CustomerServiceOfficer
 */
public CustomerServiceOfficerUI() {
    initComponents();
}
public void getCSOdetails() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/flyingcarsdb", "root", "");
        System.out.println("database connected");
    } catch (ClassNotFoundException | SQLException ex) {
        System.out.println("Error: " + ex);
    }
    try {
        // Retrieve customer service officer details
        st = con.createStatement();
        String query = "select * FROM customerserviceofficer WHERE Id = '" + loginUI.getCurrentUserId() + "'";
        rs = st.executeQuery(query);
        while (rs.next()) {
            //Assign the details with setters
            cso.setFname(rs.getString("Fname"));
            cso.setEmail(rs.getString("Email"));
        }
    } catch (Exception ex) {
        System.out.println("Error : " + ex);
    }
loggedInAsLable.setText("you're logged in as : " + cso.getId()); 
//this is where LABLE is changed, 'cso.getId()' returns the user ID
   }

如果您真的需要在JFrame可见时更新它(正如您的最后一句话所建议的那样),您可以使用WindowListener来调用您的getCSODetails()方法。

public CustomerServiceOfficerUI() {
    initComponents();
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent e)
        {
            this.getCSODetails();
        }
        @Override
        public void windowDeiconified(WindowEvent e)
        {
            this.getCSODetails();
        }
        @Override
        public void windowActivated(WindowEvent e)
        {
            this.getCSODetails();
        }
    });
}

我已经包含了三个激活事件——开启、激活和解除提示;您可以删除其中的任何一个,以将更新限制为适合您需要的特定事件。如果只需要在打开窗口后更新标签,请删除方法windowDeiconified()windowActivated()。但是,请注意,getCSODetails()方法设计得很差,每当窗口变得可见/聚焦时调用它都会导致性能损失,并且GUI的响应性将严重影响数据库的性能。我想您显示的客户详细信息在登录会话期间不会更改,因此更适合执行一次查询,缓存详细信息,然后从缓存中显示。

试试这个:

    public class CustomerServiceOfficerUI extends javax.swing.JFrame {
    private static Statement st;
    ResultSet rs;
    Connection con = null;
    Login loginUI = new Login(); // gets current user Id
    Employee cso = new CustomerServiceOfficer();  //creates new customer service officer object
    /**
     * Creates new form CustomerServiceOfficer
     */
    public CustomerServiceOfficerUI() {
        initComponents();
    }
    public void getCSOdetails() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/flyingcarsdb", "root", "");
            System.out.println("database connected");


            // Retrieve customer service officer details
            st = con.createStatement();
            String query = "select * FROM customerserviceofficer WHERE Id = '" + loginUI.getCurrentUserId() + "'";
            rs = st.executeQuery(query);
            while (rs.next()) {
                //Assign the details with setters
                cso.setFname(rs.getString("Fname"));
                cso.setEmail(rs.getString("Email"));
            }
SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                loggedInAsLable.setText("you're logged in as : " + cso.getId()); 
                loggedInAsLable.repaint();
            }
        });
        } catch (Throwable ex) {
            System.out.println("Error : " + ex);
SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                loggedInAsLable.setText("There is a problem with your code : " + ex); 
                loggedInAsLable.repaint();
            }
        });
        } finally {
        }
    //this is where LABLE is changed, 'cso.getId()' returns the user ID
       }

最新更新