简体   繁体   English

ActionListener(actionPerformed)返回什么?

[英]ActionListener(actionPerformed) return something?

I'm currently trying to learn new things in Java and decided to start GUI programming. 我目前正在尝试学习Java方面的新知识,并决定开始进行GUI编程。 I created a GUI with an ActionListener and with the method "actionPerformed". 我用ActionListener和方法“ actionPerformed”创建了一个GUI。

My question is if I can return anything from this method (actionPerformed) and where will it land? 我的问题是,是否可以从此方法返回任何东西(actionPerformed),它将落在哪里? Because the Method is called when I do something specific in this GUI. 因为当我在此GUI中执行特定操作时会调用Method。

Or how can I give this this actionPerformed method an other parameter? 或如何为该actionPerformed方法提供其他参数?

Thanks in advance ;) 提前致谢 ;)

As you probably already know, the actionPerformed method is declared as returning void or nothing , and so no, you cannot return anything from it, but you can change the state of any mutable field within the scope of this method, for instance the text displayed in a JLabel. 您可能已经知道,actionPerformed方法被声明为返回voidnone ,所以不可以,您无法返回任何内容,但是可以在此方法范围内更改任何可变字段的状态,例如显示的文本在JLabel中。

For example: 例如:

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

public class ActionListenerTest extends JPanel {
    private static final String[] DAYS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    private int dayIndex = 0;
    private JLabel label = new JLabel("", SwingConstants.CENTER);
    private JButton button = new JButton("Press Me Please!");

    public ActionListenerTest() {
        label.setText(DAYS[dayIndex]);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dayIndex++; // advance the index
                dayIndex %= DAYS.length; // if index >= the length of the array, make it 0
                label.setText(DAYS[dayIndex]);
            }
        });

        setLayout(new GridLayout(2, 1));
        add(label);
        add(button);
    }

    private static void createAndShowGui() {
        ActionListenerTest mainPanel = new ActionListenerTest();

        JFrame frame = new JFrame("ActionListenerTest");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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

While actionPerformed returns nothing, it advances the dayIndex by 1, or sets it to 0 if the index is the same as the length of the String array. 虽然actionPerformed不返回任何内容,但它会将dayIndex提前1,如果索引与String数组的长度相同,则将其设置为0。 The method then sets the text of the JLabel using the String array data. 然后,该方法使用String数组数据设置JLabel的文本。

No actionPerformed is callback, so you can handle action event by implementing actionPerformed method. 没有actionPerformed是回调,因此您可以通过实现actionPerformed方法来处理action事件。 Here you can: 1) update some GUI elemenets in your app 2) save results data in private field of your View class to process it in future. 在这里,您可以:1)更新应用程序中的一些GUI元素2)将结果数据保存在View类的私有字段中,以供将来处理。

For example: 例如:

private Map<String,Object> data = ...;

public void showElements(){
    ...
    button1.addActionListener (new ActionListener()
    {
          public void actionPerformed (ActionEvent e){
              Object info = "Somebody clicked on my button!"//or you can use ActionEvent to extract mode information
              data.put("button1",info)
          }
    })
}

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

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