java中的子类如何调用,Java如何从子类中调用方法

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.

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(){};

}

Then in my buttonControl I have:

public class buttonControl extends mainGUI{

@Override

protected void _buttonLogin(){

if (isLogin == true){

btnLogin.setEnabled(false);

} else {

// somthing else

}

}

}

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?

Thank you so much

解决方案

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值