Java Swing - 单击按钮

时间:2016-12-05 08:33:17

标签: java swing user-interface freeze calculation

我创建了一个科学计算器程序。我的程序做的是"标签"这是用户使用按钮输入的字符串并评估表达式。我的程序所做的是,只要单击相等按钮(button_eq),它就会调用splitLabel方法,该方法将标签拆分为数字和运算符的Arraylist。然后调用括号,在ArrayList中搜索括号,并通过调用方法calculate完成子计算。但是,在我的程序处理更多基本计算(加法,减法)之前,它工作正常。但是,当我添加更复杂的计算时,程序会在单击等于按钮时冻结。我想知道问题是什么?

  public class CalculatorFrameA {
      ActionListener button_eqListener = new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent e){
          label2.setText(bracket(splitLabel(label.getText())));
      }
  };

    public ArrayList<String> splitLabel(String val){
        ArrayList<String> label_split = new ArrayList<String>();
        String value = "";
        String op = "";

        for (int i = 0; i< val.length(); i++){
            boolean isDigit = Character.toString(val.charAt(i)).matches("[0123456789.]+");
            boolean isOperator = Character.toString(val.charAt(i)).matches("[+*/^-]+");
            boolean isSin = (Character.toString(val.charAt(i)).equals("s") && Character.toString(val.charAt(i+1)).equals("i") && Character.toString(val.charAt(i+2)).equals("n"));
            boolean isCos = (Character.toString(val.charAt(i)).equals("c") && Character.toString(val.charAt(i+1)).equals("o") && Character.toString(val.charAt(i+2)).equals("s"));
            boolean isTan = (Character.toString(val.charAt(i)).equals("t") && Character.toString(val.charAt(i+1)).equals("a") && Character.toString(val.charAt(i+2)).equals("n"));
            boolean isOpBracket = Character.toString(val.charAt(i)).equals("(");
            boolean isClBracket = Character.toString(val.charAt(i)).equals(")");  

                while (isDigit){
                    value = value + Character.toString(val.charAt(i));
                }

                if (isOperator && !value.equals("")){
                    op = Character.toString(val.charAt(i));
                    label_split.add(value);
                    label_split.add(op);
                    op = "";
                    value = "";
                } else if (isOperator && value.equals("")){
                    if (Character.toString(val.charAt(i)).equals("-")){
                        value = Character.toString(val.charAt(i));
                    }
                } else if (isSin){
                    label_split.add("sin");
                }else if (isCos){
                    label_split.add("cos");
                }else if (isTan){
                    label_split.add("tan");
                } else if (isOpBracket && !value.equals("")){
                    label_split.add(value);
                    label_split.add("(");
                    value = "";
                } else if (isOpBracket && value.equals("")){
                    label_split.add("(");
                } else if (isClBracket && !value.equals("")){
                    label_split.add(value);
                    label_split.add(")");
                    value = "";
                }
            if (i== val.length()-1 && !value.equals("")){
                label_split.add(value);
            } else if (i== val.length()-1 && Character.toString(val.charAt(i)).equals(")")){
                label_split.add(")");
            }
        } return label_split;
    }   
    public String bracket(ArrayList<String> label_split){
        ArrayList<Integer> opBra = new ArrayList<Integer>();
        ArrayList<Integer> clBra = new ArrayList<Integer>();
        ArrayList<String> calculation = new ArrayList<String>();
        int counter = 0;
        int counter1 = 0;

        if (label_split.contains("(") && label_split.contains(")")){
            for (int j=0; j<label_split.size(); j++){
                if (label_split.get(j).equals("(")){
                    counter = counter + 1;
                    opBra.add(j);
                } else if (label_split.get(j).equals(")")){
                    counter1 = counter1 + 1;
                    clBra.add(j);
                }
            } 
            if (counter1 != counter){
                return "error missing bracket";
            } else {
                for (int j=opBra.size(); j>0; j--){
                    int opBraPos = opBra.get(j) + 1; //+1 and -1 so it doesn't include ()
                    int clBraPos = clBra.get(opBra.size()-j) - 1;
                    opBra.remove(j);
                    clBra.remove(opBra.size()-j);

                    for(int t = 0; t < (clBraPos - opBraPos); t++){
                        calculation.add(label_split.get(t+opBraPos));
                    }

                    String value = calculate(calculation);
                    label_split.set(j , value);
                    calculation.clear();

                    for (int n = 0; n < ((clBraPos+1) - opBraPos); n++){
                        label_split.remove(n);
                    }
                }
            }
            return calculate(label_split);
        } else{
            return calculate(label_split);
        } 
    }
    public String calculate(ArrayList<String> calculation){
        double value = 0.0;
        String value1 = "";
        boolean isOperator = calculation.contains("[+*/^-]+");  
        boolean isSin =  calculation.contains("sin");
        boolean isCos = calculation.contains("cos");
        boolean isTan = calculation.contains("tan");
        boolean isOpBracket = calculation.contains("(");
        boolean isClBracket = calculation.contains(")");

        for (int i=0; i < calculation.size(); i++){
            if (calculation.get(i).equals("^") && i < calculation.size() && i < 0){
                boolean isDigit1 = calculation.get(i-1).matches("[0123456789.-]+");
                boolean isDigit2 = calculation.get(i+1).matches("[0123456789.-]+");
                if (isDigit1 && isDigit2){
                    value = Math.pow(Double.parseDouble(calculation.get(i-1)), Double.parseDouble(calculation.get(i+1)));
                    value1 = Double.toString(value);
                    calculation.set(i,value1);
                    calculation.remove(i-1);
                    calculation.remove(i+1);
                }
            }
        }

        for (int a=0; a < calculation.size(); a++){
            if ( (calculation.get(a)).equals("sin") && a < calculation.size() && a < 0){
                boolean isDigit1 = calculation.get(a+1).matches("[0123456789.-]+");
                if (isDigit1){
                    value = Math.sin(Double.parseDouble(calculation.get(a+1)));
                    value1 = Double.toString(value);
                    calculation.set(a,value1);
                    calculation.remove(a+1);
                }
            }
        }

        for (int b=0; b < calculation.size(); b++){
            if ( (calculation.get(b)).equals("cos") && b < calculation.size() && b < 0){
                boolean isDigit1 = calculation.get(b+1).matches("[0123456789.-]+");
                if (isDigit1){
                    value = Math.cos(Double.parseDouble(calculation.get(b+1)));
                    value1 = Double.toString(value);
                    calculation.set(b,value1);
                    calculation.remove(b+1);
                }
            }
        }

        for (int c=0; c < calculation.size(); c++){
            if ( (calculation.get(c)).equals("tan") && c < calculation.size() && c < 0){
                boolean isDigit1 = calculation.get(c+1).matches("[0123456789.-]+");
                if (isDigit1){
                    value = Math.tan(Double.parseDouble(calculation.get(c+1)));
                    value1 = Double.toString(value);
                    calculation.set(c,value1);
                    calculation.remove(c+1);
                }
            }
        }

        for (int d=0; d < calculation.size(); d++){
            if (calculation.get(d).equals("*") && d < calculation.size() && d < 0){
                boolean isDigit1 = calculation.get(d-1).matches("[0123456789.-]+");
                boolean isDigit2 = calculation.get(d+1).matches("[0123456789.-]+");
                if (isDigit1 && isDigit2){
                    value = Double.parseDouble(calculation.get(d-1)) * Double.parseDouble(calculation.get(d+1));
                    value1 = Double.toString(value);
                    calculation.set(d,value1);
                    calculation.remove(d-1);
                    calculation.remove(d+1);
                }
            }
        }   

        for (int e=0; e < calculation.size(); e++){
            if (calculation.get(e).equals("/") && e < calculation.size() && e < 0){
                boolean isDigit1 = calculation.get(e-1).matches("[0123456789.-]+");
                boolean isDigit2 = calculation.get(e+1).matches("[0123456789.-]+");
                if (isDigit1 && isDigit2){
                    value = Double.parseDouble(calculation.get(e-1)) / Double.parseDouble(calculation.get(e+1));
                    value1 = Double.toString(value);
                    calculation.set(e,value1);
                    calculation.remove(e-1);
                    calculation.remove(e+1);
                }
            }
        }   

        for (int f=0; f < calculation.size(); f++){
            if (calculation.get(f).equals("+") && f < calculation.size() && f < 0){
                boolean isDigit1 = calculation.get(f-1).matches("[0123456789.-]+");
                boolean isDigit2 = calculation.get(f+1).matches("[0123456789.-]+");
                if (isDigit1 && isDigit2){
                    value = Double.parseDouble(calculation.get(f-1)) + Double.parseDouble(calculation.get(f+1));
                    value1 = Double.toString(value);
                    calculation.set(f,value1);
                    calculation.remove(f-1);
                    calculation.remove(f+1);
                }
            }
        }   

        for (int g=0; g < calculation.size(); g++){
            if (calculation.get(g).equals("-") && g < calculation.size() && g < 0){
                boolean isDigit1 = calculation.get(g-1).matches("[0123456789.-]+");
                boolean isDigit2 = calculation.get(g+1).matches("[0123456789.-]+");
                if (isDigit1 && isDigit2){
                    value = Double.parseDouble(calculation.get(g-1)) - Double.parseDouble(calculation.get(g+1));
                    value1 = Double.toString(value);
                    calculation.set(g,value1);
                    calculation.remove(g-1);
                    calculation.remove(g+1);
                }
            }
        }   

        for (int h=0; h < calculation.size(); h++){
            boolean isDigit = calculation.get(h).matches("[0123456789.-]+");
            if (isDigit && !isOperator && !isSin && !isCos && !isTan &&!isOpBracket &&!isClBracket){
                double value3 = 0.0;
                value3 = Double.parseDouble(calculation.get(h));
                String value2 = Double.toString(value3);
                calculation.set(h,value2);
            }
        }

        return calculation.get(0);
    }
    public static void main(String[] args) {
        CalculatorFrameA g = new CalculatorFrameA();
    }
}
    public static void main(String[] args) {
        CalculatorFrameA g = new CalculatorFrameA();
    }

1 个答案:

答案 0 :(得分:0)

解决方案是:

  1. 使用单独的Thread并使用SwingUtilities.inovkeLater()在EDT上放置文字字段更新,或

  2. 使用SwingWorker,它具有内置API以支持上述功能。

  3. 阅读Concurrency in Swing上Swing教程中的部分,了解SwingWorker的完整信息和工作示例。