[英]Java how to call a method from a child class
我正在學習Swing在Java中制作GUI。 我的目標是擁有1個mainGUI類來初始化所有內容,並擁有一個控制所有按鈕的類。
我現在正在做的是有一個mainGUI,它具有:
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中,我有:
public class buttonControl extends mainGUI{
@Override
protected void _buttonLogin(){
if (isLogin == true){
btnLogin.setEnabled(false);
} else {
// somthing else
}
}
}
該程序實際上有效,但沒有達到我的預期。 當我單擊“登錄”按鈕時,登錄按鈕未設置為不可點擊。 如果在mainGUI類中沒有_buttonLogin方法,則無法從buttonControl類中調用它。
我只是想知道在這種情況下我的方法正確嗎? 或任何其他巧妙的方法來擁有一個單獨的偵聽器類?
非常感謝
一方面,您濫用繼承。 您不使用繼承來訪問變量。 為此,您應該使用合成。
例如,ButtonControl(請注意,任何類的第一個字母都應以大寫字母開頭),可以具有MainGui字段,該字段通過其構造函數傳遞給它。 然后,控件類可以調用Gui方法。
class ButtonControl extends AbstractAction {
MainGui gui;
public ButtonControl(MainGui gui, String name, int mnemonic) {
super(name);
putValue(MNEMONIC, mnemmonic);
this.gui = gui;
}
public void actionPerformed(ActionEvent e) {
// ....
}
}
它可以這樣使用:
ButtonControl btnCtrl = new ButtonControl(this, "My Button", KeyEvent.VK_M);
JButton myButton = new JButton(btnCtrl);
如果需要一個Single類來控制所有按鈕,則可以創建一個ButtonControl類,該類可以向其注冊和注銷按鈕,並在控件類中處理其事件。 給出了一個簡單的示例代碼
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();
}
}
}
我已經使用swing很長時間了,但是我們只能通過子類對象類型來調用子類實現:
mainGUI gui = new buttonControl();
如果要讓調用者使用buttonControl中的實現,則應該傳遞buttonControl對象類型實例而不是mainGui實例。
這與重疊的多態有關 。
希望有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.