netbeans 如何在swing GUI java文件的class中创建class make的对象

uurv41yg  于 2022-11-10  发布在  Java
关注(0)|答案(1)|浏览(212)

下面是我的www.example.com文件的代码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("");
        }
    }
}

这是登录页面服务的代码:

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;
    }

您能告诉我,首先声明引用对象的私有变量,然后在相应的方法中示例化它以调用LoginPageService类的方法,这种方法正确吗?
我已经添加了这两个文件的代码。现在有人能告诉我哪里有问题吗?为什么运行代码后,它不引导我到主页。

ua4mk5z4

ua4mk5z41#

示例变量(在类内部和方法外部声明)保存在每个对象中,并带有它们自己的引用。除非需要在同一个类的多个方法之间引用示例变量,否则可以单独在本地(需要时在方法内部)声明它。
由于您只使用它一次,并且没有进一步的引用(只要它在代码段中显示),您可以按如下方式在本地移动它:

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);
    }

否则,您只能通过构造函数将其示例化一次(如果您可能不使用它,但如果您将在所有情况下使用它,则不建议这样做)

private LoginPageService service;

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

相关问题