Java - 一个文件中的代码和另一文件中的设计(无法识别的actionPerformed 事件)

发布于 2024-10-16 18:49:30 字数 1865 浏览 3 评论 0原文

首先为我糟糕的英语感到抱歉。我面临一个问题。我正在创建一个新的java应用程序,我想将设计代码放入名为Login_Design.java的类文件中,并将按钮和其他内容的代码放入另一个名为Login.java的类文件中。

这两个文件的工作原理如下:

Login_Design.java:

    public class Login_Design
    {
        public static JButton jbtnlogin;

        public Login_Design()
        {
            initComponents(); 
        }

    public void initComponents()
    {   
        jframelogin = new JFrame();
        (...)

        jbtnlogin = new JButton();
        (...)

        jbtnlogin.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                Login.jbtnloginActionPerformed(event);
            }
        });

        jframelogin.add(jbtnlogin);
    }
}

Login.java:

package unigamex;

import java.awt.event.ActionEvent;

import unigamex.Login_Design;

public class Login
{
    public Login()
    {
        new Login_Design();
    }

    protected void jbtnloginActionPerformed(ActionEvent event) 
    {
        System.exit(0);
    }

    public static void main(String args[]) 
    {
        new Login();
    }

    public void JActionPerformed()
    {       
        Login_Design.jbtnlogin.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                jbtnloginActionPerformed(event);
            }
        });
    }
}

现在的问题是: 1 - 如果我将该代码放在同一个 java 文件中,则所有功能都有效(所有按钮都有效)

2 - 如果我确实像我在这里所做的那样,则会显示所有按钮,但无法使用它们。

3 - 删除 public void JActionPerformed() 并将代码放入 Login_Design 中 initcomponents 阵营,但他向我展示了以下消息:

Cannot make a static reference to the non-static method jbtnsairActionPerformed(ActionEvent) from the type Login

我怎样才能解决这个问题? 提前致谢, 路易斯·达·科斯塔

To start sorry for my bad english. I am confronted to a problem. I am creating a new java application and I want to put the design code in a class file named Login_Design.java and de code of the buttons and other content in another class file named Login.java.

The 2 files work like that :

Login_Design.java:

    public class Login_Design
    {
        public static JButton jbtnlogin;

        public Login_Design()
        {
            initComponents(); 
        }

    public void initComponents()
    {   
        jframelogin = new JFrame();
        (...)

        jbtnlogin = new JButton();
        (...)

        jbtnlogin.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                Login.jbtnloginActionPerformed(event);
            }
        });

        jframelogin.add(jbtnlogin);
    }
}

Login.java:

package unigamex;

import java.awt.event.ActionEvent;

import unigamex.Login_Design;

public class Login
{
    public Login()
    {
        new Login_Design();
    }

    protected void jbtnloginActionPerformed(ActionEvent event) 
    {
        System.exit(0);
    }

    public static void main(String args[]) 
    {
        new Login();
    }

    public void JActionPerformed()
    {       
        Login_Design.jbtnlogin.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                jbtnloginActionPerformed(event);
            }
        });
    }
}

Now the problem is :
1 - If I put that code in the same java file, all works (all the buttons work)

2 - If I do like i am doing right here, all buttons are showed but it is not possible to use them.

3 - Remove the public void JActionPerformed() and putting the code in the Login_Design
initcomponents camp, but he shows me the fallowing message :

Cannot make a static reference to the non-static method jbtnsairActionPerformed(ActionEvent) from the type Login

How can i solve that problem ?
Thanks in advance,
Luis Da Costa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2024-10-23 18:49:30

一种方法是让 Login 实现 ActionListener 并让它向 Login_Design 对象注册。 Login_Design 类会将所有注册者设置为按钮上的 ActionListener

编辑:

Login_Design 中:

public void addActionListenerToButtons(ActionListener listener){
    jbtnlongin.addActionListener(listener);
}

然后在 Login 中将 this 添加到 Login_Design 实例中。

One way to do it would be to make Login implement ActionListener and have it register with the Login_Design object as such. The Login_Design class would setup any registrants as ActionListeners on the buttons.

EDIT:

in Login_Design:

public void addActionListenerToButtons(ActionListener listener){
    jbtnlongin.addActionListener(listener);
}

then in Login add this to the Login_Design instance.

习惯成性 2024-10-23 18:49:30
public void actionPerformed(java.awt.event.ActionEvent event)             
{                
 Login.jbtnloginActionPerformed(event);             
} 

您正在尝试访问只能通过实例变量访问的方法(即它不是静态的)。
你应该做的是类似下面的事情:

public class Login     
{        
  public Login()
  {
     new Login_Design(this);        
  }
   protected void jbtnloginActionPerformed(ActionEvent event)      
   {
       System.exit(0);     
   } 

}



public class Login_Design{ 
  private Login login;
  public Login_Design(Login login){
        this.login = login;
        initComponents();                   
  } 

 public void initComponents()                
 { 
//...
   jbtnlogin = new JButton();                
   jbtnlogin.addActionListener(new java.awt.event.ActionListener()
   {
         public void actionPerformed(java.awt.event.ActionEvent event)
         {
             login.jbtnloginActionPerformed(event);

         }

   }
  )
//...
 }

}
}
public void actionPerformed(java.awt.event.ActionEvent event)             
{                
 Login.jbtnloginActionPerformed(event);             
} 

You are trying to access a method that can only be accessed via an instance variable (i.e. it is not static).
What you should do is something like the following:

public class Login     
{        
  public Login()
  {
     new Login_Design(this);        
  }
   protected void jbtnloginActionPerformed(ActionEvent event)      
   {
       System.exit(0);     
   } 

}



public class Login_Design{ 
  private Login login;
  public Login_Design(Login login){
        this.login = login;
        initComponents();                   
  } 

 public void initComponents()                
 { 
//...
   jbtnlogin = new JButton();                
   jbtnlogin.addActionListener(new java.awt.event.ActionListener()
   {
         public void actionPerformed(java.awt.event.ActionEvent event)
         {
             login.jbtnloginActionPerformed(event);

         }

   }
  )
//...
 }

}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文