本文介绍了只有在使用数据库进行成功登录验证后才能打开JFrame。使用Eclipse?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试的是从登录屏幕打开主要应用程序,只有在连接的数据库中验证了登录信息之后。

what I'm trying to do is open up a main application from a login screen, only after the login information has been verified in the connected database.

使用Eclipse,到目前为止我已经有了:

Using Eclipse, what I have so far:

database.java :使用UCanAccess连接到MS Access数据库。 (成功)

database.java: connection to MS Access Database using UCanAccess. (Success)

login.java :扩展JFrame的登录窗口。当输入用户名和密码时,会使用数据库进行验证。 (成功)

login.java: A login window that extends JFrame. When a username and password is entered, it is verified with the database. (Success)

Home.java :主应用程序窗口,我只想使用正确的用户名和密码可以访问。不扩展JFrame,但在其中有一个JFrame。

Home.java: The main application window, that I want to only be accessible with a correct username and password. Does not extend JFrame, but has a JFrame within it.

现在,我已经能够设置它,以便输入的用户名和密码正确,一个窗口弹出成功登录。但是,如何设置它,以便登录成功后,它会打开Home.java?

Now, I have been able to set it up so that if the entered username and password are correct, a window pops up saying "Successful login". However, how do I approach setting it up so that after the successful login, it opens up Home.java?

我看过:
- 我已经尝试了setVisible与我的家,但是Eclipse返回一个错误的说法是在家中创建一个setVisible方法...我以为这应该是一个自动控制?尝试创建该方法后,就会出现更多问题。

I have looked at: Open a new JFrame - I have tried the setVisible with my home but Eclipse returns an error saying to create a setVisible method in Home...I thought this is supposed to be an automatic control? After trying to create the method, more issues just arise.

- 这建议添加actionListener,然后将其设置为可见。我已经完成了: public void actionPerformed(ActionEvent e){this.setVisible(false); new Home()。setVisible(true); ,但是Eclipse根本没有打开登录窗口。最初,我认为这可能是因为我的成功消息是在actionListener中,但是即使删除它仍然不起作用。

JFrame Open Another JFrame - which suggests adding actionListener and then setting it visible..which I have done: public void actionPerformed(ActionEvent e) {this.setVisible(false); new Home().setVisible(true); but Eclipse just doesn't open up the login window at all. Initially, I thought it could be because my success message is in the actionListener, however even after removing that it still does not work.

获取Jframe和 - 我唯一的结论是这不工作,因为Home.java不扩展JFrame?但是,我通过其他的资料看到,使用扩展JFrame并不好用?

Call Jframe from Java class and Open window after button click - My only conclusion is that this is not working since Home.java does not extend JFrame? However, I read through other sources that it is not good to use "extends JFrame"?

我想我也不了解扩展JFramevs一个类中的新JFrame?我一直在自己学习java,我是GUI创作的新手。也许我错过了很明显的一些东西,但是我找不到解决方案。

I guess I also don't have an understanding of the difference between "extends JFrame" vs a new JFrame within a class? I have been learning java on my own and I'm new to GUI creation. Maybe I am missing something very obvious, but I just can't find a solution.

任何想法?感谢

为了给出一个想法,我的Home.java开始如下:

To give an idea, my Home.java starts like this:

public class Home {

    private JFrame frame;
    private JTable data;
    private JTextField Column1;
    private JTextField Column2;
    private JTable table;

    // Launch the application.

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Areas window = new Areas();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // Create the application.

    public Home() {
        initialize();
    }

    //Initialize the contents of the frame.

    private void initialize() {


        frame = new JFrame();
        frame.setBounds(100, 100, 697, 518);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


推荐答案

只需在您的Home类中创建一个方法, JFrame可见:

Just create a method in your Home class that sets its JFrame to be visible:

public void setJFrameVisible(boolean visible)
{
    frame.setVisible(visible);
}

然后,假设您的Home类的实例被称为home,所有你必须这样做:

Then, assuming your instance of your Home class is called "home", all you would have to do is:

home.setJFrameVisible(true);

让我增加一点上下文。当您扩展JFrame时,该类继承JFrame类的所有方法/属性。这就是为什么当您扩展JFrame时,您可以调用obj.setVisible(true),因为您的类继承了JFrame类中的setVisible方法。你有一个包含JFrame的类,所以你必须调用内部JFrame而不是类的setVisible方法。

Let me add a bit more context. When you're extending JFrame, the class inherits all the methods/properties of the JFrame class. That's why when you extend JFrame you can just call obj.setVisible(true), because your class inherited the setVisible method from the JFrame class. What you have is a class that contains a JFrame, so you have to call the setVisible method on the internal JFrame, not the class.

这篇关于只有在使用数据库进行成功登录验证后才能打开JFrame。使用Eclipse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 22:49