根据4个JRadioButton的组合执行操作

时间:2018-07-25 07:58:25

标签: java swing

我有一个简单的应用程序要设计。两个文本字段(每个字段用于一个操作数),一个计算按钮(用于计算应用于这两个操作数和4个单选按钮的操作的值),分为两个按钮组:第一组包含按钮A和B,第二组包含按钮C和D

我需要对这4个按钮组合执行某种操作(例如加法)。(A + C =>操作<-加法)每组中只有一个按钮处于活动状态。我必须避免使用IF语句。

这就是我现在所拥有的。我必须向addListeners()函数添加新代码。

package calculator;

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

public class Calculator {
    private JFrame frame;
    private JPanel mainPanel;
    private JTextField firstField;
    private JTextField secondField;
    private JButton computeButton;
    private JTextField resultField;
    private JRadioButton aRadioButton;
    private JRadioButton bRadioButton;
    private JRadioButton cRadioButton;
    private JRadioButton dRadioButton;
    private ButtonGroup firstRadioButtonGroup;
    private ButtonGroup secondRadioButtonGroup;
    private IOperation operation;
    private double firstOperand;
    private double secondOperand;
    private double result;

    public Calculator() {
        initialize();
        adjustWindow();
        applyStyle();
        centerWindow();
        addToFrame();
        addListeners();


    }

    private void addListeners() {

        computeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                firstOperand = Double.parseDouble(firstField.getText());
                secondOperand = Double.parseDouble(secondField.getText());
                result = operation.compute(firstOperand, secondOperand);
                resultField.setText(Double.toString(result));
            }
        });

    }

    private void addToFrame() {
        mainPanel.add(firstField);
        mainPanel.add(secondField);
        mainPanel.add(computeButton);
        mainPanel.add(aRadioButton);
        mainPanel.add(bRadioButton);
        mainPanel.add(cRadioButton);
        mainPanel.add(dRadioButton);        firstRadioButtonGroup.add(aRadioButton);
        firstRadioButtonGroup.add(bRadioButton);
        secondRadioButtonGroup.add(cRadioButton);
        secondRadioButtonGroup.add(dRadioButton);
        mainPanel.add(aRadioButton);
        mainPanel.add(bRadioButton);
        mainPanel.add(cRadioButton);
        mainPanel.add(dRadioButton);
        firstRadioButtonGroup.clearSelection();
        mainPanel.add(resultField);
        frame.add(mainPanel);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void initialize() {
        firstOperand = 0;
        secondOperand = 0;
        frame = new JFrame("Calculator");
        mainPanel = new JPanel();
        firstField = new JTextField();
        secondField = new JTextField();
        computeButton = new JButton("Compute");
        resultField = new JTextField();
        aRadioButton = new JRadioButton("A");
        bRadioButton = new JRadioButton("B");
        cRadioButton = new JRadioButton("C");
        dRadioButton = new JRadioButton("D");
        firstRadioButtonGroup = new ButtonGroup();
        secondRadioButtonGroup = new ButtonGroup();
    }

    private void adjustWindow() {
        frame.setSize(300, 300);
    }

    private void centerWindow() {
        Toolkit tool = Toolkit.getDefaultToolkit();
        Dimension screenSize = tool.getScreenSize();

        double screenHeight = screenSize.height;
        double screenWidth = screenSize.width;

        double currentWindowHeight = frame.getHeight();
        double currentWindowWidth = frame.getWidth();

        int windowHeight = (int) ((screenHeight / 2) - (currentWindowHeight / 2));
        int windowWidth = (int) ((screenWidth / 2) - (currentWindowWidth / 2));

        frame.setLocation(windowWidth, windowHeight);
    }

    private void applyStyle() {
        firstField.setPreferredSize(new Dimension(240, 30));
        secondField.setPreferredSize(new Dimension(240, 30));
        resultField.setPreferredSize(new Dimension(240, 30));
        computeButton.setPreferredSize(new Dimension(240, 30));
    }
}

我想听听有关该设计的更多信息。是否可以将计算器的GUI与操作分开?

2 个答案:

答案 0 :(得分:1)

您可以创建一个映射,其中的键是可能的按钮文本组合,值是相应的操作

IOperation addition = ...
Map<String, IOperation> operations = ...;
operations.put("AC", addition);

在侦听器中,您检查选中了哪些按钮并将它们的文本连接起来。

String operationKey = selectedButton1Text + selectedButton2Text;

有关如何获取单选按钮的文本,请参见this answer。然后执行操作:

operations.get(operationKey).compute(...)

答案 1 :(得分:0)

我已经找到了解决方法。我设置了一个状态机,默认情况下从添加开始,并在有单选按钮更新时进入下一个状态。这是git repo的链接:Code