gpt4 book ai didi

Java Swing - 单击按钮后完成计算时 GUI 卡住

转载 作者:行者123 更新时间:2023-12-01 06:07:49 25 4
gpt4 key购买 nike

我创建了一个科学计算器程序。我的程序所做的是采用“标签”(用户使用按钮输入的字符串)并计算表达式。我的程序所做的是,一旦单击等号按钮 (button_eq),它就会调用 splitLabel 方法,该方法将标签拆分为数字和运算符的 Arraylist。然后调用brackets在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();
}

最佳答案

不要将调用包装在常规线程中,而是使用 Swingutilities。您需要这样做,因为 Swing 不是线程安全的。这样您仍然可以在 EDT 上工作。

 SwingUtilities.invokeLater(() -> {
try {
label2.setText(bracket(splitLabel(label.getText())));
} catch (Exception e) {
e.printStackTrace();
}
});

关于Java Swing - 单击按钮后完成计算时 GUI 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40970045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com