Java如何从子类中调用方法 [英] Java how to call a method from a child class

查看:157
本文介绍了Java如何从子类中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Swing在Java中制作GUI.我的目标是拥有1个mainGUI类来初始化所有内容,并拥有一个控制所有按钮的类.

I'm learning Swing to make GUI in java. My goal is to have 1 mainGUI class to initialize everything and another class that controls all the button.

我现在正在做的是我有一个mainGUI,它具有:

What I'm doing now is I have a mainGUI which has:

public class mainGUI(){
.... (main and initialize things here) ....

protected JButton btnLogin;
public void initialize(){
    btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            _buttonLogin();
        }
    });
}

protected void _buttonLogin(){};
}

然后在我的buttonControl中,我拥有:

Then in my buttonControl I have:

public class buttonControl extends mainGUI{
@Override
protected void _buttonLogin(){
    if (isLogin == true){
        btnLogin.setEnabled(false); 
    } else {
        // somthing else
    }
}
}

该程序实际上可以正常工作,但不像我预期的那样.当我单击登录"按钮时,登录按钮未设置为不可点击.如果我在mainGUI类中没有_buttonLogin方法,则无法从buttonControl类中调用它.

The program actually works but not as i expected. When i click on the "login" button, the login button is not set to unclickable. If i don't have the _buttonLogin method in the mainGUI class then I cannot call it from buttonControl class.

我只是想知道在这种情况下我的方法正确吗?或采用其他巧妙的方法来建立单独的侦听器类?

I'm just wondering is my approach right in this situation? or any other neat way to have a separated listener class?

非常感谢您

推荐答案

如果您需要一个Single类来控制所有按钮,则可以创建一个ButtonControl类,该类可以为其注册和注销按钮并处理其事件.在控件类中.给出了一个简单的示例代码

If you need a Single class for control all the buttons you have you can create a ButtonControl class which can register and de-register buttons to it and handle its events inside the control class. A Simple example code is given

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class MainUI extends JFrame{

ButtonController buttonController;

public MainUI() {
    super();
    buttonController=new ButtonController(this);
    initialize();
}

private void initialize() {

    JTextField userName=new JTextField();
    JPasswordField passwordField=new JPasswordField();

    JButton loginButton=new JButton("Login");
    loginButton.setActionCommand(ButtonController.LOGIN_COMMAND);

    JButton cancelButton=new JButton("Cancel");
    cancelButton.setActionCommand(ButtonController.CANCEL_COMMAND);

    JPanel contentPane=new JPanel();
    contentPane.setLayout(new GridLayout(3,2));
    contentPane.add(new JLabel("Username : "));
    contentPane.add(userName);
    contentPane.add(new JLabel("Password : "));
    contentPane.add(passwordField);
    contentPane.add(loginButton);
    contentPane.add(cancelButton);

    buttonController.registerButton(loginButton);
    buttonController.registerButton(cancelButton);

    setContentPane(contentPane);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    pack();
}


/**
 * @param args
 */
public static void main(String[] args) {
    MainUI ui=new MainUI();
    ui.setVisible(true);
}


}


class ButtonController implements ActionListener
{
private MainUI mainUI;

public static String LOGIN_COMMAND="Login";
public static String CANCEL_COMMAND="Cancel";

public ButtonController(MainUI mainUi ) {
    this.mainUI=mainUi;
}

public void registerButton(JButton button)
{
    button.addActionListener(this);
}

public void deRegisterButton(Button button)
{
    button.removeActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals(LOGIN_COMMAND))
    {
        ((JButton)e.getSource()).setEnabled(false);
    }
    if(e.getActionCommand().equals(CANCEL_COMMAND))
    {
        mainUI.dispose();
    }
}

}

这篇关于Java如何从子类中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆