繁体   English   中英

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

[英]Returning result from the first clicked Jbutton from multiple JButtons java swing

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

就像是:

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 ,这些信息可以用来确定单击按钮时应执行的操作。 您可以使用ActionEventsource属性来确定事件的来源并采取适当的措施,例如

听起来您想向用户显示几个选项,让他选择一个选项,然后让他按下“提交”按钮以将该选项提交给程序。 如果是这样,那么我认为最好的选择是使用全部添加到ButtonGroup的JRadioButtons-这允许随时仅选择一个单选按钮,或者使用JComboBox。 无论哪种方式,都将很容易提取有关用户做出选择的信息。 如果使用第一个选项,则使用JRadioButtons,ButtonGroup和“提交”按钮,只需调用ButtonGroup的getSelection()方法即可从ButtonGroup中获取所选的ButtonModel,然后通过调用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();
            }
        });
    }
}

暂无
暂无

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

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