![](/img/trans.png)
[英]are multiple action performed methods needed when using multiple JButtons/JRadioButtons?
[英]Performing action depending on a combination of 4 JRadioButtons
我有一個簡單的應用程序來設計。 兩個文本字段(每個字段用於一個操作數),一個計算按鈕(用於計算應用於這兩個操作數和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與操作分開?
您可以創建一個映射,其中的鍵是可能的按鈕文本組合,值是各自的操作
IOperation addition = ...
Map<String, IOperation> operations = ...;
operations.put("AC", addition);
在偵聽器中,您可以檢查選中了哪些按鈕並連接其文本。
String operationKey = selectedButton1Text + selectedButton2Text;
有關如何獲取單選按鈕文本的信息,請參見此答案 。 然后執行操作:
operations.get(operationKey).compute(...)
我已經找到了解決方法。 我設置了一個狀態機,默認情況下從添加開始,並在有單選按鈕更新時進入下一個狀態。 這是git repo的鏈接: 代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.