java从另一个动作调用动作

发布于 2024-11-07 14:24:27 字数 1648 浏览 0 评论 0原文

countresultsfrom.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) 
                {
                    Color orginalColor = mcoef.getBackground();
                    switch(countresultsfrom.getSelectedIndex())
                    {
                        case 0: // Mech Cnt;
                            mtotal.setBackground(Color.YELLOW); 

                            if(mstatus.getSelectedIndex() == 2)
                            {
                                countresultsfrom.setSelectedIndex(2);
                                // countresultsfrom <----- CALL EVENT ???
                            }

                            etotal.setBackground(orginalColor);
                            ctotal.setBackground(orginalColor);
                        break;
                        case 1: // El Cnt;
                            etotal.setBackground(Color.YELLOW);

                            if(estatus.getSelectedIndex() == 2)
                            {
                                countresultsfrom.setSelectedIndex(2);
                            }

                            mtotal.setBackground(orginalColor);
                            ctotal.setBackground(orginalColor);
                        break;
                        case 2:
                            ctotal.setBackground(Color.YELLOW);

                            etotal.setBackground(orginalColor);
                            mtotal.setBackground(orginalColor);
                        break;
                    }
                }
            });

如何再次调用监听者???

countresultsfrom.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) 
                {
                    Color orginalColor = mcoef.getBackground();
                    switch(countresultsfrom.getSelectedIndex())
                    {
                        case 0: // Mech Cnt;
                            mtotal.setBackground(Color.YELLOW); 

                            if(mstatus.getSelectedIndex() == 2)
                            {
                                countresultsfrom.setSelectedIndex(2);
                                // countresultsfrom <----- CALL EVENT ???
                            }

                            etotal.setBackground(orginalColor);
                            ctotal.setBackground(orginalColor);
                        break;
                        case 1: // El Cnt;
                            etotal.setBackground(Color.YELLOW);

                            if(estatus.getSelectedIndex() == 2)
                            {
                                countresultsfrom.setSelectedIndex(2);
                            }

                            mtotal.setBackground(orginalColor);
                            ctotal.setBackground(orginalColor);
                        break;
                        case 2:
                            ctotal.setBackground(Color.YELLOW);

                            etotal.setBackground(orginalColor);
                            mtotal.setBackground(orginalColor);
                        break;
                    }
                }
            });

how to call listener one more time ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

您的好友蓝忘机已上羡 2024-11-14 14:24:27
  1. 您可以简单地调用 yourListener.actionPerformed(/*some event*/ e)。请注意,它不会作为事件处理,而是作为常规方法调用处理。
  2. #1 是简单的技术方法。不推荐。如果您希望发生某些逻辑,请将其包装在一个方法中并调用该方法,ActionListener 旨在处理 GUI 事件。
  1. You could simply call yourListener.actionPerformed(/*some event*/ e). Note that it will not be handled as an event, but as a regular method call.
  2. #1 is the simple technical way. It is not recommended. If you want some logic to occur, wrap it in a method and call that method, ActionListeners are meant for handling GUI events.
鲜肉鲜肉永远不皱 2024-11-14 14:24:27

首先,将执行操作的方法提取到代码中的单独类中。它可能是您希望调用的同一类中的私有静态类。例如

private static class ColorActionHandler implements ActionListener {
   //implement your method here by looping twice. or create a method for your logic and then call it in a for loop in the actionPerformed method
}

,并在 countresultsfrom 按钮的 addActionListener 中传递此类的实例。

First of all extract your action performed method to a separate class in your code. It could be a private static class in the same class you wish to call. e.g

private static class ColorActionHandler implements ActionListener {
   //implement your method here by looping twice. or create a method for your logic and then call it in a for loop in the actionPerformed method
}

and pass an instance of this class in the addActionListener of your countresultsfrom button.

遗弃M 2024-11-14 14:24:27

你的描述对我来说不清楚,但基本上有两种方法

1/ 创建自己的 Class someName Implements ActionListener

2/ 创建 java.swing.Action 一些示例操作

编辑:

如果您的 countresultsfrom 是 JList,那么我们所有的建议(也许)根本不正确 http://download.oracle.com/javase/tutorial/uiswing/components/list.html

your descriptions isn't clear for me, but there is basicall two ways

1/ create own Class someName implements ActionListener

2/ create java.swing.Action some examples for that on Action

EDIT:

if your countresultsfrom is JList, then all ours advices (maybe) didn't correct at all http://download.oracle.com/javase/tutorial/uiswing/components/list.html

彩虹直至黑白 2024-11-14 14:24:27

您是否希望每个操作都调用 actionPerformed(ActionEvent e) 两次?

如果是这样,请将以 Color orinalColor = mcoef.getBackground() 开头的块包装在 for 循环中。 for (int i=0; i<2; i++) { ... }

Do you wish actionPerformed(ActionEvent e) to be invoked twice on every action?

If so, wrap the block starting with Color orginalColor = mcoef.getBackground() in a for-loop. for (int i=0; i<2; i++) { ... }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文