У меня есть четыре класса: MainFrame
, LoginController
, EnrolledLessonsView
и WindowViewListerner
. Класс MainFrame
имеет каждую переменную экземпляра, которая будет использоваться другим классом. В первый раз, когда запускается LoginController
, создается EnrolledLessonView
с передачей мэйнфрейма через конструктор, EnrolledLessonview
отображает имя ученика с JLabel
и работает правильно.
Проблема начинается, когда я нажимаю на зарегистрированный урок JMenuItem
, который имеет windowViewListerner(mainFrame)
. windowViewListener
обновляет имя учащегося в mainFrame
и создает new enrolledLessonView(mainFrame)
, но новый enrolledLessonView
lablel
все еще содержит предыдущее имя. Я пытался println
оператор внутри класса enrolledLessonView
, и он печатает новое имя, но JLabel
нет.
Я пытался:
repaint()
revalidate()
updateGUI()
paintImidiate(..)
Класс LoginController
public class LoginController implements ActionListener {
private JTextField userIDField;
private Student student;
private final CardLayout cardLayout;
private JPanel mainWindowJPanel;
private EnrolledLessonsView enrolledLessonsView;
private MainFrame mainWindowFrame;
private EnrolledLessonsView enrolledLessonView;
public LoginController(JTextField userIDField, MainFrame mainWindowFrame) {
this.mainWindowFrame=mainWindowFrame;
this.userIDField=userIDField;
this.cardLayout=mainWindowFrame.getCardLayout();
this.mainWindowJPanel=mainWindowFrame.getMainWindowJPanel();
}
@Override
public void actionPerformed(ActionEvent e) {
//StudentTable studentTable=StudentTable.getInstance();
JDialog successMsgJDialog = new JDialog(new JFrame(), "Login successfully");
successMsgJDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
if (mainWindowFrame.getStudentTable().containStudent(userIDField.getText().toUpperCase())) {
mainWindowFrame.setStudent(mainWindowFrame.getStudentTable().getStudent(userIDField.getText().toUpperCase()));
mainWindowFrame.setEnrolledLessonsView(new EnrolledLessonsView(mainWindowFrame));
mainWindowFrame.setEnrolLessonView(new EnrolLessonView(mainWindowFrame));
JOptionPane.showMessageDialog(new JPanel(), "You have successfully login "+mainWindowFrame.getStudent().getFullName(), "Login successful", JOptionPane.INFORMATION_MESSAGE);
mainWindowFrame.getMainWindowJPanel().add(mainWindowFrame.getEnrolledLessonsView(), "enrolledLessonsView");
//mainWindowFrame.getMainWindowJPanel().add(mainWindowFrame.getEnrolLessonView(), "enrolLessonView");
cardLayout.show(mainWindowJPanel, "enrolledLessonsView");
mainWindowFrame.addMenuBar();
}else{
JOptionPane.showMessageDialog(new JFrame(), "The user ID "+userIDField.getText()+" is not found, please enter your right user ID", "Login fail", JOptionPane.WARNING_MESSAGE);
}
}
}
Класс WindowViewListener
class WindowViewsListener implements ActionListener{
private MainFrame mainFrame;
Student student;
public WindowViewsListener(MainFrame mainFrame, Student student){
this.mainFrame=mainFrame;
this.student=student;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==mainFrame.getEnrollLesson()) {
mainFrame.setEnrolLessonView(new EnrolLessonView(mainFrame));
mainFrame.getMainWindowJPanel().add(mainFrame.getEnrolLessonView(), "enrolLessonView");
mainFrame.getCardLayout().show(mainFrame.getMainWindowJPanel(), "enrolLessonView");
}
else if(e.getSource()==mainFrame.getEnrolledLessons()) {
Student newSt = mainFrame.getStudent();
newSt.setFullName("abe sdse");
mainFrame.setStudent(newSt);
JOptionPane.showMessageDialog(mainFrame, mainFrame.getStudent().getFullName());
mainFrame.setEnrolledLessonsView(new EnrolledLessonsView(mainFrame));
mainFrame.getCardLayout().show(mainFrame.getMainWindowJPanel(), "enrolledLessonsView");
}
}
}
Класс EnrolledLessonView
public class EnrolledLessonsView extends JPanel{
private Student student;
private JLabel studentNameJLabel;
private MainFrame mainWindowFrame;
;
JPanel cs ;
public EnrolledLessonsView(MainFrame mainWindowFrame){
this.mainWindowFrame=mainWindowFrame;
System.out.println("Halllooooooooo!!!!!!!!!");
cs = new JPanel();
setLayout(new BorderLayout());
add(cs, BorderLayout.BEFORE_FIRST_LINE);
String name=this.mainWindowFrame.getStudent().getFullName();
System.err.println(name+" the student name for studentLabelName");
cs.add(new JLabel(this.mainWindowFrame.getStudent().getFullName()));
}
}
Класс MainFrame
public class MainFrame extends JFrame implements ActionListener{
JPanel mainWindowJPanel;
private final LoginView loginView ;
private final RegisterView registerView;
private final CardLayout cardLayout;
private Student student;
private Tutor tutor;
private StudentTable studentTable;
private TutorTable tutorTable;
private final JMenuBar menuBar =new JMenuBar();
private JMenu account;
private JMenuItem myInfo;
private JMenuItem signOut;
private JMenu edit;
private JMenuItem personalInfor;
private JMenu lesson;
private JMenuItem searchLesson;
private JMenuItem enrollLesson;
private JMenuItem deleteLesson;
private JMenuItem enrolledLessons;
private EnrolledLessonsView enrolledLessonsView;
private EnrolLessonView enrolLessonView;
private SearchLesson searchForLesson;
private WindowViewsListener windowViewsListener;
//Constructor
public MainFrame(){
super("ITC");
RecordsFile recordsFile = new RecordsFile();
cardLayout=new CardLayout();
mainWindowJPanel = new JPanel();
loginView = new LoginView(this);
registerView = new RegisterView(this);
studentTable = StudentTable.getInstance();
tutorTable=TutorTable.getInstance();
init();
setLocationRelativeTo(null);
setVisible(true);
}
//getters and setters
public JPanel getMainWindowJPanel() {
return mainWindowJPanel;
}
public LoginView getLoginView() {
return loginView;
}
public RegisterView getRegisterView() {
return registerView;
}
public CardLayout getCardLayout() {
return cardLayout;
}
public MainFrame getMainFrame(){
return this;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public EnrolledLessonsView getEnrolledLessonsView() {
return enrolledLessonsView;
}
public void setEnrolledLessonsView(EnrolledLessonsView enrolledLessonsView) {
this.enrolledLessonsView = enrolledLessonsView;
}
public JMenuItem getEnrolledLessons() {
return enrolledLessons;
}
public StudentTable getStudentTable() {
return studentTable;
}
//Register Actionperform listener method
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == LoginView.registerNow){
cardLayout.show(mainWindowJPanel, "registerView");
addMenuBar();
}
}
/**
*This method is used to initialise the and set the top level frame
*/
public final void init(){
setSize(500, 500);
mainWindowJPanel.setLayout(cardLayout);
mainWindowJPanel.setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(mainWindowJPanel);
mainWindowJPanel.add(loginView, "loginView");
mainWindowJPanel.add(registerView, "registerView");
cardLayout.show(mainWindowJPanel, "loginView");
LoginView.registerNow.addActionListener(this);
}
public void addMenuBar(){
lesson=new JMenu("Lesson");
enrollLesson=new JMenuItem("Enrol for lesson");
deleteLesson = new JMenuItem("Delete lesson");
enrolledLessons = new JMenuItem("Enrolled lessons");
lesson.add(enrollLesson);
lesson.add(deleteLesson);
lesson.add(enrolledLessons);
enrolledLessons.addActionListener(new WindowViewsListener(this, this.student));
enrollLesson.addActionListener(new WindowViewsListener(this, this.student));
menuBar.add(lesson);
setJMenuBar(menuBar);
}
public static void main(String[] args) {
MainFrame we = new MainFrame();
we.pack();
we.setVisible(true);
}
}
Класс LoginView
public class LoginView extends JPanel{
GridBagLayout gridBagLayout = new GridBagLayout();
JPanel loginWindowJPanel = new JPanel(new GridBagLayout());
static JButton registerNow = new JButton("Register now");
JPanel welcomeMsgJPanel=new JPanel();
JPanel registerInstructionJPanel = new JPanel();
private final CardLayout cardLayout;
private JPanel mainWindowJPanel;
private MainFrame mainWindowFrame;
JButton loginJButton = new JButton("Login");
JTextField userIDField = new JTextField("");
//Constructor
public LoginView(MainFrame mainWindowFrame) {
this.mainWindowFrame =mainWindowFrame;
this.cardLayout=mainWindowFrame.getCardLayout();
this.mainWindowJPanel=mainWindowFrame.getMainWindowJPanel();
loginJButton.addActionListener(new LoginController(userIDField, mainWindowFrame));
init();
}
public JPanel getLoginWindowJPanel() {
return loginWindowJPanel;
}
public CardLayout getCardLayout() {
return cardLayout;
}
public JPanel getMainWindowJPanel() {
return mainWindowJPanel;
}
public MainFrame getMainWindowFrame() {
return mainWindowFrame;
}
public void init(){
JLabel welcomeMsgLabel = new JLabel("Welcome to ITC, a place where learning is fun");
welcomeMsgJPanel.add(welcomeMsgLabel);
JLabel loginJLabel = new JLabel("login as a student or a tutor");
JPanel loginContainerJPanel = new JPanel(gridBagLayout);
loginContainerJPanel.setSize(200, 200);
loginContainerJPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JLabel userIDJLabel = new JLabel("User ID");
JLabel passwordJLabel = new JLabel("Password");
JPasswordField passwordField = new JPasswordField();
registerInstructionJPanel.setLayout(new BoxLayout(registerInstructionJPanel, BoxLayout.Y_AXIS));
registerInstructionJPanel.add(new Label("If you are a new Student or Tutor, click on the register button to go to the register window."));
registerInstructionJPanel.add(registerNow);
GridBagConstraints gbc = new GridBagConstraints();
GridBagConstraints gbc1 = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 0.5;
gbc.gridx=0;
gbc.gridy=0;
loginContainerJPanel.add(userIDJLabel, gbc);
gbc.gridx=1;
gbc.gridy=0;
gbc.gridwidth = 2;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
loginContainerJPanel.add(userIDField, gbc);
gbc.weightx=0.0;
gbc.fill=GridBagConstraints.NONE;
gbc.gridwidth=2;
gbc.gridx=0;
gbc.gridy=2;
loginContainerJPanel.add(loginJButton, gbc);
gbc1.insets = new Insets(2,2,2,2);
gbc1.gridx=0;
gbc1.gridy=0;
gbc1.anchor=GridBagConstraints.PAGE_START;
gbc1.ipady=150;
gbc.weightx = 1.0;
loginWindowJPanel.add(welcomeMsgJPanel, gbc1);
gbc1.gridx=0;
gbc1.gridy=1;
gbc1.anchor=GridBagConstraints.CENTER;
gbc1.fill = GridBagConstraints.BOTH;
gbc1.ipady=10;
gbc.weightx = 1.0;
loginWindowJPanel.add(new JLabel("Please login with your studentID"), gbc1);
gbc1.gridx=0;
gbc1.gridy=2;
gbc1.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc1.ipady=90;
loginWindowJPanel.add(loginContainerJPanel, gbc1);
gbc1.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc1.gridx=0;
gbc1.gridy=3;
gbc1.ipadx=90;
loginWindowJPanel.add(new JLabel(""), gbc1);
gbc1.ipady=60;
gbc1.gridx=0;
gbc1.gridy=4;
loginWindowJPanel.add(registerInstructionJPanel, gbc1);
add(loginWindowJPanel);
}
}