从多个JButtons java swing返回第一次单击的Jbutton的结果

从多个JButtons java swing返回第一次单击的Jbutton的结果,java,swing,jbutton,Java,Swing,Jbutton,基本上我有n个按钮。如果单击其中任何一个,它们将返回一个特定的数字。 我有一个包含每个按钮的菜单,当用户单击一个按钮时,我的菜单方法返回按钮处理程序返回的数字。可能吗 比如: frame.add(button1..) frame.add(button2..) frame.add(button3..) if (button1.isClicked()) { return button1ActionHandler(); } else if (button2.isClicked()) {

基本上我有n个按钮。如果单击其中任何一个,它们将返回一个特定的数字。 我有一个包含每个按钮的菜单,当用户单击一个按钮时,我的菜单方法返回按钮处理程序返回的数字。可能吗

比如:

frame.add(button1..)
frame.add(button2..)
frame.add(button3..)
if (button1.isClicked()) {
    return button1ActionHandler();
} else if (button2.isClicked()) {
       return button2ActionHandler();
} else if (button3.isClicked()) {
             return button3ActionHandler();
}
问题是,代码没有等待我点击按钮,因此它不会进入任何if。如何让程序等待单击?如何检查按钮是否被单击?

首先查看和

记住,GUI是一个事件驱动的环境,也就是说,一些东西,然后你对它做出响应

您需要针对每个按钮注册一个
ActionListener
,当按钮被触发时,您需要采取适当的操作

有多种方法可以实现这一点,您可以使用适当的信息设置按钮的
actionCommand
,以确定单击按钮时应执行的操作。您可以使用
ActionEvent
source
属性来确定事件的来源并采取适当的措施,例如查看和

记住,GUI是一个事件驱动的环境,也就是说,一些东西,然后你对它做出响应

您需要针对每个按钮注册一个
ActionListener
,当按钮被触发时,您需要采取适当的操作


有多种方法可以实现这一点,您可以使用适当的信息设置按钮的
actionCommand
,以确定单击按钮时应执行的操作。您可以使用
ActionEvent
source
属性来确定事件的来源并采取适当的操作,例如听起来您希望向用户提供多个选项,让他选择其中一个选项,然后让他按“提交”按钮将该选项提交给程序。如果是这样,那么我认为最好的选择是使用JRadioButtons,所有这些都添加到ButtonGroup中——这允许在任何时候只选择一个单选按钮,或者使用JComboBox。无论哪种方式,都可以很容易地提取用户所做选择的相关信息。如果使用第一个选项,即使用JRadioButtons、ButtonGroup和“submit”按钮,只需通过调用按钮组的
getSelection()
方法从按钮组中获取所选的按钮模型,然后通过调用
getActionCommand()
从该模型中提取actionCommand字符串。如果您决定使用第二个选项,请使用JComboBox和“提交”按钮,然后在提交按钮的ActionListener中调用JComboBox上的
getSelectedItem()

下面我向您展示这两个选项。请注意,我的提交按钮没有使用ActionListener,而是使用AbstractAction,这有点像类固醇上的ActionListener

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

public class SelectionEg extends JPanel {
    private static final String[] SELECTIONS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    private ButtonGroup buttonGroup = new ButtonGroup(); 
    private JComboBox<String> selectionComboBox = new JComboBox<>(SELECTIONS);

    public SelectionEg() {
        for (String selection : SELECTIONS) {
            JRadioButton radioButton = new JRadioButton(selection);
            radioButton.setActionCommand(selection);
            add(radioButton);
            buttonGroup.add(radioButton);
        }
        add(selectionComboBox);
        add(new JButton(new SubmitAction("Submit")));        
    }

    private class SubmitAction extends AbstractAction {
        public SubmitAction(String name) {
            super(name);
            putValue(MNEMONIC_KEY, (int) name.charAt(0));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ButtonModel model = buttonGroup.getSelection();
            if (model == null) {
                // nothing selected yet, ignore this
                return;
            }
            String message = "The selected radio button is: " + model.getActionCommand();
            System.out.println(message);

            message = "The selection from the combo box is: " + selectionComboBox.getSelectedItem();
            System.out.println(message);
        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Selelection Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new SelectionEg());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
导入java.awt.event.ActionEvent;
导入javax.swing.*;
公共类选择扩展了JPanel{
私有静态最终字符串[]选择={“星期一”、“星期二”、“星期三”、“星期四”、“星期五”};
private ButtonGroup ButtonGroup=新建ButtonGroup();
专用JComboBox selectionComboBox=新JComboBox(选项);
公共选择{
用于(字符串选择:选择){
JRadioButton radioButton=新的JRadioButton(选择);
radioButton.setActionCommand(选择);
添加(单选按钮);
按钮组。添加(单选按钮);
}
添加(选择组合框);
添加(新按钮(新提交(“提交”));
}
私有类SubmitAction扩展了AbstractAction{
公共提交(字符串名称){
超级(姓名);
putValue(助记符键,(int)name.charAt(0));
}
@凌驾
已执行的公共无效操作(操作事件e){
ButtonModel模型=buttonGroup.getSelection();
if(model==null){
//尚未选择任何内容,请忽略此选项
返回;
}
String message=“所选单选按钮为:”+model.getActionCommand();
System.out.println(消息);
message=“组合框中的选择是:”+selectionComboBox.getSelectedItem();
System.out.println(消息);
}
}
私有静态void createAndShowGui(){
JFrame frame=新JFrame(“选择示例”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(新建SelectionEg());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGui();
}
});
}
}

听起来您希望向用户提供多个选项,让用户选择其中一个选项,然后让他按“提交”按钮将该选项提交给程序。如果是这样,那么我认为最好的选择是使用JRadioButtons,所有这些都添加到ButtonGroup中——这允许在任何时候只选择一个单选按钮,或者使用JComboBox。无论哪种方式,都可以很容易地提取用户所做选择的相关信息。如果使用第一个选项,即使用JRadioButtons、ButtonGroup和“submit”按钮,只需通过调用按钮组的
getSelection()
方法从按钮组中获取所选的按钮模型,然后通过调用
getActionCommand()
从该模型中提取actionCommand字符串。如果您决定使用第二个选项,请使用JComboBox和“提交”按钮,然后在提交按钮的ActionListener中调用JComboBox上的
getSelectedItem()

低于我