如何创建在挥杆gui java文件类中制作的类的对象
这是我的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("");
}
}
}
这是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;
}
您能告诉我这是声明参考对象的第一个私人变量的正确方法,然后以相应的方法对其进行实例化来调用loginpageservice类的方法?
我添加了两个文件的代码。有人可以告诉我问题在哪里。为什么在运行代码后,它不会引导我进入主页。
Here is the code of my HomePage.java file in which I have created the object of LoginPageService class to call its methods.
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:
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实例变量(在类和外部方法中声明)保存在每个对象中,并使用自己的参考。除非您需要跨同一类的多个方法引用实例变量,否则您可以单独将其声明(在需要时内部)声明。
由于您仅使用一次,并且没有进一步的参考(就摘要中的显示),因此您可以在本地移动它:
否则您只能通过构造函数实例化一次(如果您可能不这样可能,则不愿意。使用它,但是如果您在每种情况下都使用它,请继续此操作)
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:
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)