简体   繁体   English

Java中的GUI接口类?

[英]Interface class with gui in java?

My question is about using interface class with graphical user interface in java. 我的问题是关于在Java中将接口类与图形用户界面一起使用。

Now i have interface class which have methods like this: 现在我有接口类,它具有这样的方法:

public interface management
{
    public void loginActionPerformed(java.awt.event.ActionEvent evt);
}

then i create a class that implements the interface but this class is written with gui design and has a button that will implement the method login 然后我创建一个实现该接口的类,但是该类是使用gui设计编写的,并且具有一个将实现方法login的按钮

the problem: the button method is private and cannot be converted to public because iam using the drag and drop to make the design. 问题:按钮方法是私有的,不能转换为公共按钮,因为iam使用拖放进行设计。

how to fix this problem or how to use interface class with gui design? 如何解决此问题或如何在GUI设计中使用接口类?

code of the class that has the design: 具有设计的类的代码:

public class employee extends javax.swing.JFrame implements management
{
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
   // implementation 
   }
}

Java doesn't allow private methods in interfaces . Java不允许在接口中使用私有方法

A possible workaround solution, is to use another method name in the interface and delegate in the implementation: 一个可能的解决方法是在接口中使用另一个方法名称,并在实现中委托:

public class Employee extends javax.swing.JFrame implements Management
{
   @Override
   public void myLoginActionPerformed(java.awt.event.ActionEvent evt) {
       loginActionPerformed(evt);
   }

   // @javax.annotation.Generated 
   private void loginActionPerformed(java.awt.event.ActionEvent evt) {
       // implementation 
   }
}

with

public interface Management
{
    void myLoginActionPerformed(java.awt.event.ActionEvent evt);
}

Finally i found the solution for my question: 终于我找到了我的问题的解决方案:

Interface class like this: 接口类如下:

public interface Management
{
     public void login(Account a); // object from a class instead of gui frame.
}

The class implemented the interface: 该类实现了接口:

public class Employee extends javax.swing.JFrame implements Management
{
    private void loginActionPerformed(java.awt.event.ActionEvent evt) {
    Account a = new Account();
    login(a); 
    // button method that should clicked on.
   }
}

 public void login(Account a)
 {
     // implementation of what button should do when clicked on.
 }

Special thanks to @RC :) 特别感谢@RC :)

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

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