java怎么实现在一个类中放置监听器,而在另一个类中事件呢?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java怎么实现在一个类中放置监听器,而在另一个类中事件呢?相关的知识,希望对你有一定的参考价值。

比如说我定义了两个类,a和b类,我在a类当中写窗体、组件、监听器等,在b类当中写a类中要用到的事件,比如当a类中的按钮点击时,就调用b类中相应的事件,该怎么写?本人新手,希望说的通俗易懂一些,最好有代码示范,谢谢各位了

在A类中new一个B类...在给A类的按钮添加监听时...添加的是B类的监听...
例如:(简写,希望看得懂)
class A extends JFrame

public A()



X = new B();
…… //省略,假设这里new了各种组件

A的某个组件.addActionListener(X); //给组件添加B的监听,这样这个组件在触发某个事件时,就可以调用B类中的事件了





class B implements ActionListener

public void actionPerformed(ActionEvent e)

…… //这是写触发时说要做的事...当A中添加了B类监听的组件相应触发事件时,就会自动调用这里了




总而言之,就是当你想使用某个类(B)作为对于触发事件的处理时,就让这个类(B)去implements监听,然后在你需要的组件上添加对B的监听(组件.addXXXListener(X))就可以了...
addXXXListener(?),这个函数在触发了事件时,会自动去找?类中相应的方法去处理事件...来自:求助得到的回答
参考技术A //这是A类

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MyFrame extends JFrame
private JButton addButton;
private JTextField resultText;

public static void main(String[] args)
new MyFrame();


public MyFrame()
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
resultText = new JTextField(20);
addButton = new JButton("+1");
addButton.addActionListener(new AddButtonAction(resultText));
add(resultText);
add(addButton);
setVisible(true);


//这是B类
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;

public class AddButtonAction implements ActionListener
private JTextField resultText;

public AddButtonAction(JTextField resultText)
this.resultText = resultText;


public void actionPerformed(ActionEvent e)
try
int value = Integer.parseInt(resultText.getText());
resultText.setText(String.valueOf(value + 1));
catch (NumberFormatException ne)
resultText.setText("your input string is not a number!");


如何在 bean 类本身中放置 @Bean 工厂方法?

【中文标题】如何在 bean 类本身中放置 @Bean 工厂方法?【英文标题】:How to place @Bean factory method in the bean class itself? 【发布时间】:2019-08-08 22:12:30 【问题描述】:

我有一个包含从 JSON 文件反序列化的数据的类。这些数据应该在我的应用程序中可用,所以我想将它绑定为 bean。

为了保持反序列化逻辑和数据结构在一起,我想把@Bean注解的工厂方法放到数据类本身——像这样:

@Configuration
public class MyData 

    // factory method
    @Bean
    public static MyData loadMyData(ResourceLoader resourceLoader) throws IOException 
        try (InputStream input = resourceLoader.getResource("classpath:data.json").getInputStream()) 
            return new ObjectMapper().readValue(input, MyData.class);
         
    

    // data structure
    private Map<String, DataDetail> details;

    // ...

但是这失败了,因为@ComponentScan 现在找到了两个 bean 定义:

org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有“org.example.MyData”类型的合格bean可用:预期的单个匹配bean但找到了2:myData,loadMyData

我也试过用@Component替换@Configuration,但结果是一样的。

我只是错过了类上的正确注释,还是无法将 @Bean 方法放在 bean 类本身中?

【问题讨论】:

看看Best way to load some JSON files into a Spring Boot application 你需要两个相同类型的bean吗?您可以将它们命名为唯一的并Autowire 对应一个 不,我只想要一颗豆子。但到目前为止,我还没有弄清楚如何在所描述的场景中得到这个。 【参考方案1】:

基本上我更喜欢带有 Spring Boot 的分层架构,例如 Configurationmodel 有两个不同的层

app.config

@Configuration
public class MyDataConfig 

// factory method
@Bean
public static MyData loadMyData(ResourceLoader resourceLoader) throws IOException 
    try (InputStream input = resourceLoader.getResource("classpath:data.json").getInputStream()) 
        return new ObjectMapper().readValue(input, MyData.class);
     
 


com.model

public class MyData 

// data structure
private Map<String, DataDetail> details;

    // ...

【讨论】:

【参考方案2】:

正如 Deadpool 回答的那样,您应该将 MyData 类与 @Configurtion 类分开。@Configuration 使用 @Component 进行元注释,因此一旦您使用 @Configuration 注释 MyData,Spring 也将其与普通 bean 关联起来,这就形成了 MyData bean 的双重定义。

【讨论】:

以上是关于java怎么实现在一个类中放置监听器,而在另一个类中事件呢?的主要内容,如果未能解决你的问题,请参考以下文章

可以在另一个 CarouselView 中放置一个 CarouselView 吗?

为啥我们要在 Ruby 的类中放置一个模块?

如何在 Eclipse 的类路径中放置一个 jar? [复制]

如何在 bean 类本身中放置 @Bean 工厂方法?

如何在 <li> 元素中放置多个类?

为啥我不能在类声明中放置“使用”声明?