简体   繁体   English

我无法将字符串从ActionListener转到Java中的其他类

[英]I can't get the string from ActionListener to different class in Java

I created a JComboBox and JButton to submit information. 我创建了一个JComboBox和JButton来提交信息。 I need the information to be sent to a different class to sort it out with a switch method. 我需要将信息发送到其他类,以便使用switch方法对其进行分类。 But it looks like the string created inside the ActionListener is not recognized by a different class. 但看起来在ActionListener内部创建的字符串无法被其他类识别。

public Main() {
    final JComboBox comboB = new JComboBox(b);          //add int b in here for array
    comboB.setBounds(50, 30, 123, 20);
    contentPane.add(comboB);

    JButton btnTest = new JButton("Test");
    btnTest.setBounds(300, 350, 89, 23);
    contentPane.add(btnTest);

    btnTest.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String s = (String)comboB.getSelectedItem();
        }
    });
 }

How do I make it so that String s can be recognized by other classes? 如何使String可以被其他类识别? I have a separate class that will change action depending on what is selected from ComboBox, but I just can't seem to get this information out. 我有一个单独的类,该类将根据从ComboBox中选择的内容来更改操作,但是我似乎无法获取此信息。 Thank you. 谢谢。

Firstly, other objects need some way to register an ActionListener to the combo box. 首先,其他对象需要某种方式将ActionListener注册到组合框。 I would suggest providing a addActionListener method to your class, this would act as a proxy method and simple pass the call onto comboB 我建议为您的类提供一个addActionListener方法,它将用作代理方法并将调用简单地传递到comboB

Secondly, this means comboB is going to need to be a class instance variable 其次,这意味着comboB将需要是一个类实例变量

Thirdly, the other classes are going to need to determine if the action originiated from the combo box or not, for example. 第三,例如,​​其他类别将需要确定动作是否源自组合框。

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof JComboBox) {
        JComboBox cb = (JComboBox)e.getSource();
        String s = (String)cb.getSelectedItem();
    }
}

Now, there's not a lot of context available to the question, but, personally, I would normally either use a model of some kind that your UI class would update and/or a PropertyChangeListener that other classes could register against and monitor for changes to the "properties" of the your main class. 现在,这个问题没有很多可用的上下文,但是就我个人而言,我通常会使用某种类型的模型来更新您的UI类和/或使用PropertyChangeListener来供其他类注册并监视对类的更改。您的主类的“属性”。

You just need to create a private method and have the combo call that. 您只需要创建一个私有方法并进行组合调用即可。 Then you just navigate to your component/class, and perform the action. 然后,您只需导航到您的组件/类,然后执行操作即可。

    public Main() {
    final JComboBox comboB = new JComboBox(b);          //add int b in here for array
    comboB.setBounds(50, 30, 123, 20);
    contentPane.add(comboB);

    JButton btnTest = new JButton("Test");
    btnTest.setBounds(300, 350, 89, 23);
    contentPane.add(btnTest);

    btnTest.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String s = (String)comboB.getSelectedItem();
            myMethodThatProcessesS(s);

        }
    });
 }

private void myMethodThatProcessesS(String s) {
     contentPane.getSomeOtherComponent().doSOmething(s);
}

Since java swing implements the MVC pattern you can pass the JComboBox's model reference to other objects. 由于java swing实现了MVC模式,因此您可以将JComboBox的模型引用传递给其他对象。

Models implement the observer pattern and therefore the other objects can register themself if they need to get notified immediatly when the model changes. 模型实现观察者模式,因此,如果其他对象需要在模型更改时立即得到通知,则它们可以自己注册。

public class Main {
    public initializeComponent(OtherClass otherClass) {
        ...
        JComboBox comboBox = ...;
        ComboBoxModel comboBoxModel = comboBox.getModel();
        otherClass.setComboBoxModel(comboBoxModel);
    }
}

public class OtherClass {

    private ComboBoxModel comboBoxModel;

    public void setComboBoxModel(ComboBoxModel comboBoxModel) {
        this.comboBoxModel = comboBoxModel;
        ListDataListener listener = ...;
        comboBoxModel.addListDataListener(listener);
    }

    public String getSelectedItem(){
        Object selectedItem = comboBoxModel.getSelectedItem();
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM