Java - 一个文件中的代码和另一文件中的设计(无法识别的actionPerformed 事件)
首先为我糟糕的英语感到抱歉。我面临一个问题。我正在创建一个新的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是让
Login
实现ActionListener
并让它向Login_Design
对象注册。Login_Design
类会将所有注册者设置为按钮上的ActionListener
。编辑:
在
Login_Design
中:然后在
Login
中将this
添加到Login_Design
实例中。One way to do it would be to make
Login
implementActionListener
and have it register with theLogin_Design
object as such. TheLogin_Design
class would setup any registrants asActionListener
s on the buttons.EDIT:
in
Login_Design
:then in
Login
addthis
to theLogin_Design
instance.您正在尝试访问只能通过实例变量访问的方法(即它不是静态的)。
你应该做的是类似下面的事情:
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: