java - 在主方法中触发另一个类的按钮操作

标签 java button

您好,当我学习教程时,我学会了一种通过单击另一个类中的按钮来触发主类中的响应的方法。

所以我所做的是我有一个 ToolBar 类,其中包含以下代码

private JButton helloButton;
private JButton goodbyeButton;

private StringListener textListener;

public Toolbar() {
    setBorder(BorderFactory.createEtchedBorder());

    helloButton = new JButton("Hello");
    goodbyeButton = new JButton("Goodbye");

    helloButton.addActionListener(this);
    goodbyeButton.addActionListener(this);

    setLayout(new FlowLayout(FlowLayout.LEFT));

    add(helloButton);
    add(goodbyeButton);

}

public void setStringListener(StringListener listener) {
    this.textListener = listener;
}

@Override
public void actionPerformed(ActionEvent e) {
    JButton clicked = (JButton) e.getSource();

    if (clicked == helloButton) {
        if (textListener != null){
            textListener.textEmitted("Hello\n");
        }
        //textPanel.appendText("Hello\n");
    } else {
        if (textListener != null){
            textListener.textEmitted("Goodbye\n");
        //textPanel.appendText("Goodbye\n");
    }
}
}

然后在 StrinListener 接口(interface)中我有

public interface StringListener {

public void textEmitted (String text);

}

最后在 main 中我将两者结合在一起

        toolbar.setStringListener(new StringListener (){

        @Override
        public void textEmitted(String text) {
            textPanel.appendText(text);

        }

    });

我很好奇的是,为什么单击按钮“每次”单击都会触发主方法中的响应?

因此,单击将传递到 StringListener 接口(interface)中的 textemissed 方法,并由 main 方法中的toolbar.setStringListener 接收。但是每当我单击按钮时是什么会调用它一遍又一遍地工作?

除非存在 while 循环或其他某种循环,否则代码不应该只读取一次吗?

谢谢

我的主课

    public MainFrame() {
    super("Hello World");

    setLayout(new BorderLayout());

    textPanel = new TextPanel();
    btn = new JButton("Click Me!");
    toolbar = new Toolbar();
    formPanel = new FormPanel();

    toolbar.setStringListener(new StringListener (){

        @Override
        public void textEmitted(String text) {
            textPanel.appendText(text);

        }

    });

    formPanel.setFormListener(new FormListener(){
        public void formEventOccurred(FormEvent e){
            String name = e.getName();
            String occupation = e.getOccupation();

            textPanel.appendText(name + ": " + occupation + "\n");
        }
    });

    add(toolbar, BorderLayout.NORTH);
    add(textPanel, BorderLayout.CENTER);
    add(formPanel, BorderLayout.WEST);

    setSize(600, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}

最佳答案

它的行为符合预期。

请记住,当您设置 textListener 时,Toolbar 类会保留(textListener 的)实例变量,因此只要您的程序正在运行或直到工具栏对象被销毁,它就会保持 Activity 状态。仅仅因为它是一个匿名内部类并不意味着该对象在运行一次 textEmissed 方法后就会被销毁。

关于java - 在主方法中触发另一个类的按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35574638/

相关文章:

java - 如何在java方法中将数组作为参数传递?

java - 发送 POST 请求时出现 org.springframework.http.converter.HttpMessageNotReadableException

android - onclick 按钮替换按钮位置

android - 如何在 AsyncTask 运行时禁用 Button? (安卓)

c++ - 创建可点击的 "buttons"C++

javascript - 为什么JS函数在IE9下不能正常工作?但在 Chrome 和 Firefox 中运行良好

java - 是否每次都实例化最终变量

java - JLabel 没有出现在 JFrame 中

java - 如何在Retrofit2中添加标题

c# - WPF多行按钮禁用