简体   繁体   English

从按钮数组中返回选择

[英]Return the choice from an array of buttons

I've got an array that creates buttons from AZ, but I want to use it in a Method where it returns the button pressed. 我有一个从AZ创建按钮的数组,但是我想在方法中使用它来返回按下的按钮。

this is my original code for the buttons: 这是按钮的原始代码:

String  b[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
for(i = 0; i < buttons.length; i++)
{
    buttons[i] = new JButton(b[i]);
    buttons[i].setSize(80, 80);
    buttons[i].setActionCommand(b[i]);
    buttons[i].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            String choice = e.getActionCommand();
            JOptionPane.showMessageDialog(null, "You have clicked: "+choice);
        }
    });
    panel.add(buttons[i]);
}
  • ActionListener can return (every Listeners in Swing) Object that representing JButton ActionListener可以返回(Swing中的每个Listeners )表示JButton Object

  • from this JButton you can to determine, getActionCommand() or getText() 从此JButton您可以确定getActionCommand()getText()

I wasn't sure exactly what you question was, so I have a few answers: 我不确定您要问的是什么,所以我有几个答案:

  1. If you want to pull the button creation into a method - see the getButton method in the example 如果要将按钮创建拉入方法中,请参见示例中的getButton方法

  2. If you want to access the actual button when it's clicked, you can do that by using the ActionEvent.getSource() method (not shown) or by marking the button as final during declaration (shown in example). 如果要在单击时访问实际的按钮,可以通过使用ActionEvent.getSource()方法(未显示)或在声明期间将按钮标记为最终按钮来实现(如示例所示)。 From there you can do anything you want with the button. 从那里,您可以使用该按钮执行任何所需的操作。

  3. If you question is "How can I create a method which takes in a array of letters and returns to me the last clicked button", you should modify you question to explicitly say that. 如果您的问题是“我如何创建一个包含字母数组并向我返回最后一个单击的按钮的方法”,则应修改您的问题以明确地说出这一点。 I didn't answer that here because unless you have a very special situation, it's probably not a good approach to the problem you're working on. 我在这里没有回答这个问题,因为除非您有非常特殊的情况,否则它可能不是解决您正在解决的问题的好方法。 You could explain why you need to do that, and we can suggest a better alternative. 您可以解释为什么需要这样做,我们可以建议一个更好的选择。

Example: 例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TempProject extends Box{
    /** Label to update with currently pressed keys */
    JLabel output = new JLabel();

    public TempProject(){
        super(BoxLayout.Y_AXIS);
        for(char i = 'A'; i <= 'Z'; i++){
            String buttonText = new Character(i).toString();
            JButton button = getButton(buttonText);
            add(button);
        }
    }

    public JButton getButton(final String text){
        final JButton button = new JButton(text);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "You have clicked: "+text);
                //If you want to do something with the button:
                button.setText("Clicked"); // (can access button because it's marked as final)
            }
        });
        return button;
    }

    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setContentPane(new TempProject());    
                frame.pack();
                frame.setVisible(true);
                new TempProject();
            }
        });
    }   
}

I'm not sure what exactly you want, but what about storing the keys in a queue (eg a Deque<String> ) and any method that needs to poll the buttons that have been pressed queries that queue. 我不确定您到底想要什么,但是如何将密钥存储在队列中(例如Deque<String> )以及需要轮询已按下按钮的任何方法来查询该队列呢? This way you would also get the order of button presses. 这样,您还将获得按钮按下的顺序。

Alternatively, you could register other action listeners on each button (or a central one that dispatches the events) that receive the events in the moment they are fired. 另外,您可以在触发事件的每个按钮上注册其他动作侦听器(或调度事件的中央按钮)。 I'd probably prefer this approach, but it depends on your exact requirements. 我可能更喜欢这种方法,但这取决于您的确切要求。

尝试将Action侦听器更改为此

 JOptionPane.showMessageDialog(null, "You have clicked: "+((JButton)e.getSource()).getText());

1. First when you will be creating the button, please set the text on them from A to Z. 1.首先,当您要创建按钮时,请将其上的文本从A设置为Z。

2. Now when your GUI is all ready, and you click the button, extract the text on the button, and then display the message that you have clicked this button. 2.现在,当您的GUI都准备就绪时,您单击按钮,提取按钮上的文本,然后显示您单击此按钮的消息。

Eg: 例如:

I am showing you, how you gonna extract the name of the button pressed, i am using the getText() method 我正在向您展示,您将如何提取所按下按钮的名称,我正在使用getText()方法

butt.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e)
       {

       JOptionPane.showMessageDialog(null, "You have clicked: "+butt.getText());

       }


   });

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

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