打开JFrame,只有在数据库登录验证成功后.使用 Eclipse?

Open JFrame, only after successfull login verification with database. Using Eclipse?(打开JFrame,只有在数据库登录验证成功后.使用 Eclipse?)

本文介绍了打开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?

我看过:打开一个新的 JFrame - 我已经在家里尝试了 setVisible 但 Eclipse 返回一个错误说在 Home 中创建一个 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.

JFrame 打开另一个 JFrame - 建议添加 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.

从 Java 类调用 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"?

我想我也不了解扩展 JFrame"与类中的新 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:打开JFrame,只有在数据库登录验证成功后.使用 Eclipse?

基础教程推荐