简体   繁体   English

在面板之间传递信息吗?

[英]Passing information between panels in swing?

Im using swing in java to make a quiz for my coursework. 我在Java中使用swing做我的课程测验。 I have made my main class, a tabbed frame that holds separate jpanels as tabs for example the login page, and im using sqlite to get my data. 我已经制作了我的主类,一个选项卡式框架,其中包含单独的jpanel作为选项卡,例如登录页面,并且我使用sqlite来获取我的数据。 However Im struggling to see how i can communicate between say my login jpanel and my main class. 但是我正在努力查看我如何在登录jpanel和我的主要班级之间进行交流。

mainJFrame.addWindowListener(new WindowAdapter() {
        public void windowActivated(WindowEvent arg0) {
            LoginPanel loginEntry = new LoginPanel();
            welcomePanel.add(loginEntry);
            welcomePanel.setBounds(0,0,728,390);

//So here i would like an if statement so that on successful login, the other tabs are added and can be seen. //因此在这里,我想要一个if语句,以便成功登录后,其他选项卡将添加并可以看到。 But are not available if you are not logged in. 但如果您未登录,则不可用。

                tabbedPane.addTab("Quick Guide", null, quickGuidePanel, null);
                tabbedPane.addTab("Examples", null, examplePanel, null);
                tabbedPane.addTab("Run Quiz", null, runQuizPanel, null);
                tabbedPane.addTab("Exams", null, examPanel, null);
                tabbedPane.addTab("View Performance", null, viewPerfPanel, null);
                tabbedPane.addTab("Settings", null, settingsPanel, null);

        }
    });

I tried creating a getter and setter and tried to getSuccessfulLogin() but that wasn't working :( 我试图创建一个getter和setter方法,并尝试getSuccessfulLogin(),但是那不起作用:(

thanks 谢谢

I good way to populate your loginInformation in the Application would be a Singleton Pattern. 在应用程序中填充loginInformation的一种好方法是使用Singleton模式。 For example: 例如:

public class AuthService {
   private static AuthService myInstance;

   private User currentUser;
   ...

   private AuthService() {
       // Constructor is private. The Object can only be instanciated by getInstance()
   }

   public static AuthService getInstance() {
      if (myInstance == null) {
         myInstance = new AuthService();
      }

      return myInstance;
   }

   public User getCurrentUser()
   ...


   public void setLoggedInUser(User user)
   ...

   public User login(String userName, String password)
   ...

}

Via the static getInstance()-method you can get an Instance of exact the same object everywhere in your Appliaction without the need to carry the Object trough all constructros or methods. 通过静态getInstance()方法,您可以在Appliaction中的任何位置获得一个完全相同对象的实例,而无需通过所有构造体或方法携带该对象。 You can hold all your authentification-related Data there and even some Methods, that do the login-work. 您可以在其中保存所有与身份验证相关的数据,甚至可以保存一些完成登录工作的方法。 The getCurrentUser() could return null if no succelsful logged in user exists. 如果没有成功的登录用户,则getCurrentUser()可能返回null。

If it is possible to login from more then one client at the same time you will need to hold different UserInformations for different sessions in your AuthService. 如果可以从一个以上的客户端同时登录,则您将需要为AuthService中的不同会话保留不同的UserInformation。

Change the line: 更改行:

LoginPanel loginEntry = new LoginPanel();

to

LoginPanel loginEntry = new LoginPanel(MainJFrame.this);

and inside the login function: 并在登录功能内:

if (success) {
    //add the TabbedPane
    mainJFrame.add(tabbedPane);
} else {
    // do not add the TabbePane
}

For checking if the log in is successful you could create a static variable that holds a Boolean whether the current user is successfully logged in. Static has the meaning that it is accessible throughout the whole program. 为了检查登录是否成功,您可以创建一个静态变量,该变量包含一个布尔值,表明当前用户是否已成功登录。静态具有在整个程序中都可以访问的含义。

static Boolean isLoggedIn

You probably see that you need to set that variable somewhere, and read it somewhere to allow 'communication' 您可能会看到需要在某个位置设置该变量,并在某个位置读取该变量以允许“通信”

For more information it is better to assign methods within the tabs that inform each other. 有关更多信息,最好在选项卡之间分配相互告知的方法。 This would be more event/action based programming where you keep the responsibilities of each part of your program straight. 这将是更多基于事件/动作的编程,您可以直接保持程序各部分的职责。

As @Adel noted, you could link the main frame to the login panel. 正如@Adel指出的那样,您可以将主机链接到登录面板。 This has the advantage that you keep it tighter, but this also might have to problem that if you have more tabs you want to communicate with each other, that you need to inform all panels which panels there are. 这样做的好处是可以使其更加紧密,但是这还可能带来一个问题,即如果您要相互通信的选项卡更多,则需要通知所有面板哪些面板。

you can create a static method. 您可以创建一个静态方法。 hold login info!! 保持登录信息!!

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

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