java - 使用循环执行actionPerformed(在单击按钮上)

标签 java swing

我正在尝试在我的计算器程序上实现此 actionPerformed 方法。我在创建一个循环时遇到问题,该循环将显示按钮并在按下时执行计算。顺便说一句,如果我使用 if 语句执行此操作,我可以解决我的问题,但我想让我的代码更干净。 这是我的代码:

     String expr1 = "";

         private static final String[] bText = {"7", "8", "9", "+", "4", "5", "6", "- ", "1", "2", "3", "*", "0", ".", "=", "/", "(", ")", "C", "CE"};
         Button[] buttons = new Button[ bText.length ];

         public void actionPerformed( ActionEvent arg0 ) {

         String input = textField.getText();

         // Trying to get the first 12 items for testing 
         for( int index = 0; index <=12; index++)
         {
             if(arg0.getSource()==buttons[index])
             {

                 if(input.contains("1,2,3,4,5,6,7,8,9,0"))
                 {
                 textField.setText(input + bText[index]);
                 expr1 = expr1 + index;
                 }
                if(input.contains("+-*/")){
                     expr1 = expr1 + bText[index];
              textField.setText("");
                 }
             }

         }    
            for( int i=13; i <=16; i++){
              if(arg0.getSource()==buttons[i]){
             expr1 = expr1 + bText[i];
              textField.setText("");

            }
          }
         /**
           If I do this one by one using if statements I can fix my problem but I want to make my code cleaner. 

          */


          //For CE button
          if (arg0.getSource() == buttons[18]) {
             String s = textField.getText().toString();
             s = s.substring(0, s.length() - 1);
             textField.setText(s);

          }
                else if(arg0.getSource()==buttons[17]) {
                textField.setText("");
                expr1 = "";    
            } 
         // This will calculate expressins. This is a "=" button 
         else if (arg0.getSource() == buttons[19]) {
               System.out.println(expr1);
                textField.setText("" + Integer.toString(calculatorEvaluator.eval(expr1)));
            }


      }

最佳答案

我可以想到很多方法来解决这个问题,使用 Actions API 可能是我最喜欢的,但可能超出了此处的要求范围。

我认为您不需要使用循环,我认为它们不会给您带来任何优于其他方法的优势。

例如,您可以利用内置的正则表达式支持中的String并简单地检查按钮的文本。

这至少可以让您确定文本是数字、运算符还是其他命令,例如...

private final String[] bText = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "(", ")", "C", "CE"};
private JButton[] buttons = new JButton[bText.length];

public TestPane() {
    setLayout(new GridLayout(0, 3));
    for (int index = 0; index < bText.length; index++) {
        String text = bText[index];
        JButton btn = new JButton(text);
        buttons[index] = btn;
        btn.addActionListener(this);
        add(btn);
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source instanceof JButton) {
        JButton btn = (JButton)source;
        String text = btn.getText();
        if (text.matches("^[0-9]")) {
            System.out.println(text + " is a number");
        } else if (text.matches("^[=/\\(\\)*=\\-\\+]")) {
            System.out.println(text + " is an operator");
        } else {
            System.out.println(text + " is some other command");
        }
    }
}

关于java - 使用循环执行actionPerformed(在单击按钮上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216223/

相关文章:

java - 如何检查两个drawString()字符串是否相交?

java - 将 3 个列表从 asynctask 类发送到另一个类

java - 在给定整数的叉积中生成一行

Eclipse 调试 View 中未显示 Java 源代码

java - 显式转换为相同类型

java - JPopupMenu 错误,未选择或关闭

java - 在 JLabel 上的 gif 和 png 之间切换

java - 我应该扩展 Swing JCheckBox 吗?

java - 如何根据双击 JTable 的位置生成新面板?

Java堆中最大对象大小