ActionListener 不适用于其他 class

ActionListener doesn't work from other class

这里我有测试应用程序,我不明白为什么 Test class 中的 ActionListener 不起作用。每一次帮助都会让我开心。谢谢 :) 这是代码;

package com.company;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {
Main(){
    GUI m = new GUI();
    this.getContentPane().add(m.panel);
    this.setDefaultCloseOperation(3);
    this.setBounds(0,0,400,200);
    this.setLocationRelativeTo(null);
    this.requestFocus();
    this.setVisible(true);
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Main();
            new Test();
        }
    });
}
}

class GUI{
GUI(){
    initComp();
    btn();
}
JPanel panel = new JPanel();
JButton button1;
JTextField textField;
private void initComp(){
    textField = new JTextField(10);
    button1 = new JButton("TEST");
    panel.add(textField);
    panel.add(button1);
}
public String getEnteredText(){
    return textField.getText().trim(); //trim dodatkowy
}

public void btn(){
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            System.out.println("Test from GUI-class");
        }
    });
    }
  }

class Test{
Test(){
    buttonGetter();

}
GUI m = new GUI();
String kol;
public void buttonGetter(){
    m.button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            System.out.println("Test from Test-class");
        }
    });
}
}

Test 不会向任何 components/windows 添加任何内容,因此它在屏幕上永远不可见,只有在 Main 中创建的 GUI 的实例可见。

一个可能更好的解决方案是使用 class 继承来扩展 GUI 并覆盖 btn 方法以提供您的自定义实现,这可能看起来像。 ..

public class Test extends GUI {

    @Override
    public void btn() {
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                System.out.println("Test from Test-class");
            }
        });
    }
}

然后,不是在 Main 方法中创建 GUI 的实例,而是创建 Test 的实例,例如;

GUI m = new Test();

可运行示例

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    Main() {
        GUI m = new Test();
        this.getContentPane().add(m.panel);
        this.setDefaultCloseOperation(3);
        this.setBounds(0, 0, 400, 200);
        this.setLocationRelativeTo(null);
        this.requestFocus();
        this.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });
    }

    public class GUI {

        GUI() {
            initComp();
            btn();
        }
        JPanel panel = new JPanel();
        JButton button1;
        JTextField textField;

        private void initComp() {
            textField = new JTextField(10);
            button1 = new JButton("TEST");
            panel.add(textField);
            panel.add(button1);
        }

        public String getEnteredText() {
            return textField.getText().trim(); //trim dodatkowy
        }

        public void btn() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent e) {
                    System.out.println("Test from GUI-class");
                }
            });
        }
    }

    public class Test extends GUI {

        @Override
        public void btn() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent e) {
                    System.out.println("Test from Test-class");
                }
            });
        }
    }
}