简体   繁体   English

如何在swing GUI java文件的类中创建类的对象

[英]How to create the object of class made in the class of swing GUI java file

Here is the code of my HomePage.java file in which I have created the object of LoginPageService class to call its methods.这是我的 HomePage.java 文件的代码,我在其中创建了 LoginPageService 类的对象来调用它的方法。

public class HomePage extends javax.swing.JFrame {
    CardLayout cl;
    private LoginPageService service;

    /**
    * Creates new form trial
    */
    public HomePage() {
        initComponents();
        cl = (CardLayout) (jPanel6.getLayout());
        service = new LoginPageService();
        JTable jTable = service.getScheduledLectureList(jTable1, ScheduleLecture.class);
        jTable.setRowHeight(45);
        jTable.getColumnModel().getColumn(0).setPreferredWidth(1);
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setBackground(new java.awt.Color(119, 124, 168));
        tableHeader.setForeground(Color.black);
        Font headerFont = new Font("Verdana", Font.PLAIN, 19);
        tableHeader.setFont(headerFont);
    }

    private void jLabel26MouseClicked(java.awt.event.MouseEvent evt) {                                    
        service = new LoginPageService();
        String id = getId1().getText();
        char ch[] = getPassword1().getPassword();
        String password = new String(ch);
        String value = (String) jComboBox2.getSelectedItem();

        Boolean result = service.checkCredential(id, password, value);
        if (result == true) {
            JOptionPane.showMessageDialog(this, "Welcome " + id);
            if ("Student".equals(value)) {
                new SignInAsStudent().setVisible(true);
                dispose();
            }
            if ("Instructor".equals(value)) {
                new main.java.com.lecture_backup.view.SignInAsInstructor().setVisible(true);
                dispose();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Invalid Id or Password");
            getPassword1().setText("");
            getId1().setText("");
        }
    }
}

this is the code of LoginPageService:这是 LoginPageService 的代码:

public class LoginPageService {
   private HomePage hm;
   
    public LoginPageService(){
         hm = new HomePage();
    }
    
    public JTable getScheduledLectureList(JTable jTable1, Class sdl) {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.openSession();
        Date date = new Date();
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        hm.getPassword1().setText("");
        hm.getjLabel13().setText("" + df.format(date));
        Criteria crit1 = session.createCriteria(sdl);
        Criteria crit = session.createCriteria(ScheduleLecture.class);
        crit.add(Restrictions.ge("date", df.format(date)));
        List<ScheduleLecture> data = crit.list();
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        dtm.setRowCount(0);
        for (ScheduleLecture sl : data) {
            Object obj[] = {sl.getSerialNo(), sl.getSubject(), sl.getTopic(), sl.getName(), sl.getDate(), sl.getTime()};
            dtm.addRow(obj);

        }
        return jTable1;
    }

Can you please tell me is this the right way to declare the reference object first private variable, then instantiate it in the respective methods to call the methods of LoginPageService class?你能告诉我这是先声明引用对象的私有变量,然后在各自的方法中实例化它以调用 LoginPageService 类的方法的正确方法吗?

I have added the code of both the files.我已经添加了这两个文件的代码。 can someone tell me now where s the problem.有人现在可以告诉我问题出在哪里。 Why after running the code it is not directing me to HomePage.为什么在运行代码后它没有将我定向到主页。

Instance variables (declared inside the class and outside methods) are kept in each object with their own reference.实例变量(在类内部和外部方法中声明)保存在每个对象中,并带有自己的引用。 Unless you need reference for the instance variable across multiple methods of the same class, you can declare it locally (inside method when needed) alone.除非您需要跨同一类的多个方法引用实例变量,否则您可以单独在本地(需要时在方法内部)声明它。

Since you are using it only once and have no further reference to it (as far as it is show in the snippet) you may move it locally as follows:由于您只使用它一次并且没有进一步引用它(就片段中显示的而言),您可以将其移动到本地,如下所示:

    public class HomePage extends javax.swing.JFrame {
    ...
    // private LoginPageService service; // remove this
    ...
    //service = new LoginPageService(); // remove this
    ...
    JTable jTable = new LoginPageService().getScheduledLectureList(jTable1, ScheduleLecture.class);
   
    ...                                     
    //service = new LoginPageService(); // remove this
    ...
    Boolean result = new LoginPageService().checkCredential(id, password, value);
    }

Else you can instantiate it only once via the constructor (not reccommended if you may not use it but if you will use it in every case, then proceed with this)否则,您只能通过构造函数将其实例化一次(如果您可能不使用它,则不建议使用它,但如果您将在每种情况下都使用它,则继续执行此操作)

private LoginPageService service;

HomePage() {
    service = new LoginPageService();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM